执行更新语句时如何解决 bind_param() 中的致命错误
How to resolve fatal error in bind_param() when executing update statement
我要更新用户个人资料详细信息(姓名、密码、contact_no、电子邮件)。
请帮我检查如何改进代码,以便我可以成功更新。
这里是 DbOperations.php updateUser class:
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
。
update
语句的 set
子句没有括号。只需删除它们,您应该没问题:
$stmt = $this->con->prepare("UPDATE `mydb.User` SET `name` = ?, `password`= ? , `contact_no`= ?,`email` = ? WHERE `user_id` = ? ;");
# Parentheses removed ------------------------------^------------------------------------------------------^
我要更新用户个人资料详细信息(姓名、密码、contact_no、电子邮件)。 请帮我检查如何改进代码,以便我可以成功更新。
这里是 DbOperations.php updateUser class:
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
。
update
语句的 set
子句没有括号。只需删除它们,您应该没问题:
$stmt = $this->con->prepare("UPDATE `mydb.User` SET `name` = ?, `password`= ? , `contact_no`= ?,`email` = ? WHERE `user_id` = ? ;");
# Parentheses removed ------------------------------^------------------------------------------------------^