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

时间:2018-01-14 09:40:36

标签: php mysql prepared-statement bindparam

我将更新用户个人资料详细信息(姓名,密码,contact_no,电子邮件)。 请帮我查一下如何改进代码,以便我能够成功更新。

这是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:

updateProfile-error

Table Structure

2 个答案:

答案 0 :(得分:0)

有两件事,我注意到了

  1. 您的SQL中有分号 Date: Sun, 14 Jan 2018 09:35:09 GMT Server: WSGIServer/0.1 Python/2.7.14+ X-Frame-Options: SAMEORIGIN Content-Type: text/html; charset=utf-8 Content-Length: 146 大括号 ;,这不应该是其中的一部分。它应该是

    ()
  2. 由于,"UPDATE `mydb.User` SET `name` = ?, `password`= ? , `contact_no`= ?,`email` = ? WHERE `user_id` = ?" useridcontact_no。然后它应该是integer而不是"ssisi"

  3. 另外,我建议使用"sssss"代替User

答案 1 :(得分:0)

$ ./toupper argv[0] = ./toupper String in upper case : SOME STRING $ ./tolower argv[0] = ./tolower String in lower case : some string 语句的update条款没有围绕它的括号。只需删除它们就可以了:

set