为什么MYSQLi不会更新数据库记录,但是会提供成功的消息。当然,显示以下消息:0条记录已成功更新,并且未对数据库进行任何更改。
我的索引php文件代码:
<?php
include 'connect.php';
$work = $_GET["work"];
if($work == "select"){
$query = "SELECT * FROM login ORDER BY City DESC";
$result = $connect->prepare($query);
$result ->execute();
$out = array();
while ($row = $result->fetch(PDO::FETCH_ASSOC)){
$record = array();
$record["InsID"] = $row["InsID"];
$record["Password"] = $row["Password"];
$record["Name"] = $row["Name"];
$record["City"] = $row["City"];
array_push($out,$record);
}
echo json_encode($out);
} elseif($work == "update"){
$name2 = $_REQUEST["Ali"];
$code2 = $_REQUEST["4779"];
$city2 = $_REQUEST["teh"];
$pass2 = $_REQUEST["123"];
$query2 = "UPDATE login SET Password='$pass2',Name='$name2',City='$city2' WHERE InsID = '$code2'";
$result2 = $connect->prepare($query2);
$result2 ->execute();
}
?>
我真的不知道我的编码哪里出错了。请帮忙。
答案 0 :(得分:0)
我不明白为什么您要更新InsID并同时使用'where InsID like'
查询中还有其他;
您可以尝试
$query2 = "UPDATE login SET Password='$pass2',Name='$name2',City='$city2' WHERE InsID like '$code2'";
重要=首先清除输入数据**
答案 1 :(得分:0)
如果我了解您要完成的工作,那么:
即这是您需要的行:
$query2 = "UPDATE login SET Password='$pass2',Name='$name2',City='$city2' WHERE InsID = '$code2';";
还请参阅Nico Haase的评论,它是超级正确的!您必须提高代码安全性,请参见:http://php.net/manual/en/security.database.sql-injection.php
答案 2 :(得分:-2)
尝试此代码 可能有用
$query2 = "UPDATE login SET Password='$pass2',Name='$name2',City='$city2' WHERE InsID = '$code2';
if(mysqli_affected_rows($connect)==1){
echo "updated successfully";
}
else{
echo "failed";
}