我想在更新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;
}
?>
答案 0 :(得分:1)
在 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrlA as ctrl">
<div class="form-group required">
<label for="Permissions">Permissions</label>
<div>
<label ng-repeat="permission in ctrl.user.permissionVoList">
Label {{permission.permissionName}}
<input type="checkbox" ng-model="permission.isCheckd">
</label>
</div>
</div>
and 'user' object in the controller is as follows {{ctrl.user| json}}
</div>
</div>
之后使用mysqli_affected_rows
。这将为您提供更新行的计数。所以你可以相应地检查
答案 1 :(得分:1)
您当前逻辑的问题是您发出的查询不会生成错误,只是不会更新任何行。
如果您想确保错误,请尝试进行查询,以便根本不进行编译。
同样,当这些语句返回对象OR false
时,最好使用false
而不是===
if($var)
进行测试
<?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);
}
?>
答案 2 :(得分:1)
请使用以下代码
if($statement->affected_rows > 0){
print 'Success! record updated';
}else{
print 'Error : ('. $mysqli->errno .') '. $mysqli->error;
}
答案 3 :(得分:1)
我通常做的就是这样。
此外,您需要确保您拥有此记录所特有的字段或内容。基本上,它总是会按照它的方式插入,因为我们只是检查一个值(生日)
这是一个例子 $ conn = new mysqli(&#39; localhost&#39;,&#39;用户名&#39;,&#39; pwd&#39;,&#39; db&#39;);
// 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')");
}
检查其他示例here