0

I am going to update the user profile details(name, password, contact_no, email). Please help me check how can I improve the code so that I can update successfully.

Here is 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;
            }
    }       

Here is 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);

And the error message:

`<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 />`

Line 49 is :$stmt->bind_param("sssss", $name, $password, $contact_no, $email, $user_id);

And I am using an API developer to POST:

updateProfile-error

Table Structure

Ravi
  • 30,829
  • 42
  • 119
  • 173

2 Answers2

0

There are two things, which I noticed

  1. There is semicolon ; and braces () in your SQL, which shouldn't be part of it. And it should be

    "UPDATE `mydb.User` SET `name` = ?, `password`= ? , `contact_no`= ?,`email` = ? WHERE  `user_id` = ?"
    
  2. Since, userid and contact_no is integer. Then it should be "ssisi" instead of "sssss"

  3. Also, I would suggest to use User instead of mydb.User.

Ravi
  • 30,829
  • 42
  • 119
  • 173
  • Thanks for replying. As for point 2, I change it to 'ssisi' since the contact is int as well. But it gives me the same error. – NRZDYZXLUTMD Jan 14 '18 at 09:55
  • @NRZDYZXLUTMD please note, that was my assumption (from screenshot posted), I don't know your table structure.So, are you saying after apply point 1, you are getting same exception ? – Ravi Jan 14 '18 at 09:55
  • @NRZDYZXLUTMD you haven't applied all my suggestion, there is still semicolon. Also, yes, I think, you should use `ssisi` instead. – Ravi Jan 14 '18 at 10:00
  • @NRZDYZXLUTMD can you just write `User` instead of `mydb.User` ? Also, you need to make sure latest file are deployed at server. You could also try executing same SQL phpadmin and see, if SQL executes. I can't see any other issue here. – Ravi Jan 14 '18 at 10:05
  • Oh it works! Thanks so much. I think the table name makes the difference. Thanks again~ – NRZDYZXLUTMD Jan 14 '18 at 10:07
0

An update statement's set clause doesn't have parentheses around it. Just remove them and you should be fine:

$stmt = $this->con->prepare("UPDATE `mydb.User` SET `name` = ?, `password`= ? , `contact_no`= ?,`email` = ? WHERE  `user_id` = ? ;");
# Parentheses removed ------------------------------^------------------------------------------------------^
Mureinik
  • 297,002
  • 52
  • 306
  • 350