我正在尝试创建一个删除具有特定设备ID和产品ID的条目的函数。该函数删除该行但运行带有错误的else语句。 mysqli_affected_rows()期望参数1为mysql
该功能还必须重定向到dashboard.php,但不是。任何帮助将不胜感激。
if(isset($_POST['remove_product'])){
//recieve input
$DeviceID = mysqli_real_escape_string($db,$_POST['DeviceID']);
$productID = mysqli_real_escape_string($db,$_POST['productID']);
$name = $_SESSION['username'];
$sql="DELETE FROM products WHERE deviceid = '$DeviceID' AND productid = '$productID' AND username = '$name'";
$resultsql = mysqli_query($db,$sql);
if($resultsql != FALSE && mysqli_affected_rows($resultsql) == 1){
echo "Device/Product is deleted!";
header('Location: dashboard.php');
exit;
}
else{
echo "Device/Product not registered";
}
}
答案 0 :(得分:3)
更好的想法是使用prepared statement,bind the $DeviceID
, $productID
and $name
parameters然后检查$stmt->affected_rows
// Exceptions are much easier to deal with
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$stmt = $db->prepare('DELETE FROM products WHERE deviceid = ? AND productid = ? AND username = ?');
$stmt->bind_param('iis', $_POST['DeviceID'], $_POST['productID'], $_SESSION['username']);
$stmt->execute();
if ($stmt->affected_rows === 1) {
// and so on.
答案 1 :(得分:2)
mysqli_affected_rows()应该引用连接而不是查询。 reference
此外,您无法在 echo 语句php docs
之后使用标题查看更新的代码(未测试)
if(isset($_POST['remove_product'])){
//recieve input
$DeviceID = mysqli_real_escape_string($db,$_POST['DeviceID']);
$productID = mysqli_real_escape_string($db,$_POST['productID']);
$name = $_SESSION['username'];
$sql="DELETE FROM products WHERE deviceid = '$DeviceID' AND productid = '$productID' AND username = '$name'";
$resultsql = mysqli_query($db,$sql);
if($resultsql && mysqli_affected_rows($db) == 1){
header('Location: dashboard.php');
exit;
}
else{
echo "Device/Product not registered";
}
}
答案 2 :(得分:-1)
尝试替换
mysqli_affected_rows($resultsql) to mysqli_affected_rows($db)