如何修复购物车中的删除

时间:2019-03-25 06:17:59

标签: php web

当我从购物车中删除商品时,会显示此错误;

Warning: mysqli_query() expects parameter 1 to be mysqli, string given in C:\xampp\htdocs\FinalsActivity1\delete.php on line 5

Warning: mysqli_error() expects parameter 1 to be mysqli, string given in C:\xampp\htdocs\FinalsActivity1\delete.php on line 5

**代码

 <?php
    require("config.php");
    require('header.php');
   ***LINE 5*** $itemsql = "SELECT * FROM orderitems WHERE id = ". $_GET['id'] . ";";
    $itemres = mysqli_query($dbdatabase, $itemsql) or die(mysqli_error($itemsql));
    $numrows = mysqli_num_rows($itemres);
    if($numrows == 0) {
    header("Location: showcart.php");
    }
 . 

1 个答案:

答案 0 :(得分:0)

将变量用引号引起来。在您的示例中,它将是:

$itemsql = "SELECT * FROM orderitems WHERE id = '". $_GET['id'] . "';";

$prodsql = "SELECT price FROM products WHERE id = '" . $itemrow['product_id'] . "';";

这是因为,假设您的ID实际上是int,因此,当您将它们放在查询中而不加引号时,它会将它们作为int处理。通过将它们用引号引起来,它会强制查询将其解释为字符串。