我将更新用户个人资料详细信息(姓名、密码、联系人号码、电子邮件)。 请帮我检查如何改进代码以便我能够成功更新。
这是 DbOperations.php updateUser 类:
public function updateUser($user_id, $name, $password,$contact_no,$email){
$stmt = $this->con->prepare("UPDATE `mydb.User` SET `name` = ?, `password`= ? , `contact_no`= ?,`email` = ? WHERE `user_id` = ? ;");
$stmt->bind_param("ssisi", $name, $password, $contact_no, $email, $user_id);
if($stmt->execute()){
return 1;
}else{
return 2;
}
}
这是 updateProfile.php
<?php
require_once'../includes/DbOperations.php';
$response =array();
if($_SERVER['REQUEST_METHOD']=='POST'){
if(isset($_POST['user_id'])and
isset($_POST['name'])and
isset($_POST['password'])and
isset($_POST['contact_no'])and
isset($_POST['email']))
{//operate the data further
$db = new DbOperations();
$result=$db->updateUser(
$_POST['user_id'],
$_POST['name'],
$_POST['password'],
$_POST['contact_no'],
$_POST['email']
);
if($result == 1){
$response['error']= false;
$response['message'] = "updated successfully";
}elseif($result == 2){
$response['error']=true;
$response['message'] = "updated failed";
}
}
}else{
$response['error']=true;
$response['message'] = "empty fileds";
}
echo json_encode($response);
错误消息:
`<br />
<b>Fatal error</b>: Call to a member function bind_param() on a non-object in
<b>C:\xampp\htdocs\Android\includes\DbOperations.php</b> on line
<b>49</b>
<br />`
第 49 行是:$stmt->bind_param("sssss", $name, $password, $contact_no, $email, $user_id);
我正在使用 API 开发人员进行 POST:
最佳答案
我注意到有两件事
SQL 中不应该有分号
;
和大括号()
成为其中的一部分。而且应该是"UPDATE `mydb.User` SET `name` = ?, `password`= ? , `contact_no`= ?,`email` = ? WHERE `user_id` = ?"
因为,
userid
和contact_no
是integer
。那么它应该是"ssisi"
而不是"sssss"
此外,我建议使用
User
而不是mydb.User
。
关于php - 如何解决执行update语句时bind_param()中的 fatal error ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48248301/