如何使用mysqli使用update语句


how to use update statement using mysqli

我想在更新statement时没有结果时显示一个错误,所以我尝试在不存在ID的情况下使用此代码。

但是这个代码总是给我success!然而,我试图用不存在的错误id来测试它!你能告诉我这个代码出了什么问题吗。

<?PHP
include("config.php"); // For connection
$product_name = '52 inch TV';
$product_code = '9879798';
$find_id = 188; // id not exist should gives Error
$statement = $mysqli->prepare("UPDATE products SET product_name=?, product_code=? WHERE ID=?");
$statement->bind_param('ssi', $product_name, $product_code, $find_id);
$results =  $statement->execute();
if($results){
    print 'Success! record updated';
}else{
    print 'Error : ('. $mysqli->errno .') '. $mysqli->error;
}
?>

请使用以下代码

if($statement->affected_rows > 0){
    print 'Success! record updated';
}else{
    print 'Error : ('. $mysqli->errno .') '. $mysqli->error;
}

$results = $statement->execute();之后使用mysqli_affected_rows。这将为您提供更新的行数。因此,您可以相应地检查

当前逻辑的问题是,您发出的查询不会生成错误,只是不会更新任何行。

如果您想确保出现错误,请尝试进行查询,使其根本不会编译。

此外,由于这些语句返回对象OR false,因此最好使用===而不是if($var) 专门测试false

<?php
include("config.php"); // For connection
$product_name = '52 inch TV';
$product_code = '9879798';
$find_id = 188; // id not exist should gives Error
$statement = $mysqli->prepare("UPDATE products 
                                  SET product_name=?, 
                                      product_code = ? 
                                WHERE IDXX = ?");    // <-- generate error
$statement->bind_param('ssi', $product_name, $product_code, $find_id);
$results =  $statement->execute();
if($results === false){
    print 'Error : ('. $mysqli->errno .') '. $mysqli->error;
}else{
    print 'Success! record updated';
}
?>

否则,如果你真的想知道查询是否真的修改了什么,你必须检查受影响的行计数

<?php
include("config.php"); // For connection
$product_name = '52 inch TV';
$product_code = '9879798';
$find_id = 188; // id not exist should gives Error
$statement = $mysqli->prepare("UPDATE products 
                                  SET product_name=?, 
                                      product_code = ? 
                                WHERE ID = ?");
$statement->bind_param('ssi', $product_name, $product_code, $find_id);
$results =  $statement->execute();

if($results === false){
    print 'Error : ('. $mysqli->errno .') '. $mysqli->error;
}else{
    print 'Success! record updated';
    echo sprintf('Query changed %d rows', $mysqli->affected_rows);
}
?>

我通常会做这样的事情。

此外,您需要确保您有一个字段或该记录独有的内容。基本上,它总是插入它的编写方式,因为我们只检查一个值(生日)

下面是一个例子
$conn=新的mysqli('localhost','username','pwd','db');

        // check connection
        if (mysqli_connect_errno()) {
          exit('Connect failed: '. mysqli_connect_error());
        }          
                // check to see if the value you are entering is already there      
                $result = $conn->query("SELECT * FROM birthday WHERE name='Joe'");
                if ($result->num_rows > 0){ 
                    // this person already has a b-day saved, update it
                    $conn->query("UPDATE birthday SET birthday = '$birthday' WHERE name = 'Joe'");
                }else{
                    // this person is not in the DB, create a new ecord
                    $conn->query("INSERT INTO `birthday` (`birthday`,`name`) VALUES ('$birthday','Joe')");
                }  

查看此处的附加示例