我正在使用phpMyadmin和innoDb引擎。我已经完成了插入和删除。但我不能更新任何东西。我无法理解错误。我从updatestudent.php调用了updatestudent。转到函数updatestudent但它返回错误并且更新不成功。
updateStudent.php
require_once '../includes/DbOperations.php';
$response = array();
if($_SERVER['REQUEST_METHOD']=='POST'){
if(isset($_POST['name']) and isset($_POST['mobileno']) and isset($_POST['sem']) and isset($_POST['section']) and isset($_POST['usn'])){
$db = new DbOperations();
if($db->updateStudent($_POST['mobileno'], $_POST['sem'], $_POST['section'], $_POST['usn'])){
$user = $db->getStudentByUsername($_POST['name']);
$response['error'] = false;
$response['name'] = $user['name'];
$response['email'] = $user['email'];
$response['mobileno'] = $user['mobileno'];
$response['dept'] = $user['dept'];
$response['sem'] = $user['sem'];
$response['usn'] = $user['usn'];
$response['section']=$user['section'];
}else{
$response['error'] = true;
$response['message'] = "Update was not successful";
}
}else{
$response['error'] = true;
$response['message'] = "Required fields are missing";
}
}
echo json_encode($response);
updateStudent函数
public function updateStudent($mobileno, $sem, $section, $usn){
$stmt = $this->con->prepare("UPDATE `student` SET mobileno = ? AND sem = ? AND section = ? WHERE usn = ? ");
$stmt->bind_param("ssss",$mobileno, $sem, $section, $usn);
$stmt->execute();
return $stmt->get_result();
}
答案 0 :(得分:1)
你的sql语句不正确:
UPDATE `student` SET mobileno = ? AND sem = ? AND section = ? WHERE usn = ?
应该是
UPDATE `student` SET mobileno = ? , sem = ?, section = ? WHERE usn = ?
答案 1 :(得分:0)
return $stmt->get_result();
不是判断UPDATE
是否成功的正确方法。 mysqli_stmt::get_result()
的文档说:
返回成功SELECT查询的结果集,或其他DML查询或失败时返回FALSE。 mysqli_errno()函数可用于区分两种类型的失败。
因此,对于UPDATE
查询,它始终会返回FALSE
。您需要致电mysqli_errno()
以确定它是否真的成功:
return $this->conn->errno == 0;
您还需要更正UPDATE
查询的语法,它应该是:
UPDATE `student` SET mobileno = ? , sem = ?, section = ? WHERE usn = ?
由于查询的语法不正确,prepare()
失败,因此$stmt
为FALSE
,当您致电$stmt->bind_param()
时,您应该收到错误。