如何解决执行update语句时bind_param()中的致命错误

hm2xizp9  于 2021-06-25  发布在  Mysql
关注(0)|答案(2)|浏览(310)

我将更新用户配置文件的详细信息(姓名、密码、联系方式、电子邮件)。请帮助我检查如何改进代码,以便成功更新。
下面是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开发人员发布:

qhhrdooz

qhhrdooz1#

有两件事,我注意到了
有分号 ; 和支架 () 在sql中,这不应该是它的一部分。应该是的

"UPDATE `mydb.User` SET `name` = ?, `password`= ? , `contact_no`= ?,`email` = ? WHERE  `user_id` = ?"

从那以后, userid 以及 contact_nointeger . 那应该是 "ssisi" 而不是 "sssss" 另外,我建议使用 User 而不是 mydb.User .

guz6ccqo

guz6ccqo2#

update 对账单 set 子句周围没有括号。只要把它们取下来,你就没事了:

$stmt = $this->con->prepare("UPDATE `mydb.User` SET `name` = ?, `password`= ? , `contact_no`= ?,`email` = ? WHERE  `user_id` = ? ;");

# Parentheses removed ------------------------------^------------------------------------------------------^

相关问题