-2

I know this might look like a duplicate question. Have had a look at different question and answers, but struggle to get result. So basically my edit profile is working great, before i decide to had more field and still field somehow causing problem. so am getting this error

Fatal error: Call to a member function bind_param() on boolean.

Am going to show you my php code below

       <?php

    // start session
    session_start();

    error_reporting(E_ALL); ini_set('display_errors', 1);

    // i user is not log in redirect back to index
    if(!isset($_SESSION['user_type'])){
      header('Location: index.php');
    }

    // include data based connection config
    include_once('include/connection.php');

    // incluude header ttile page
    $title  = "Edit User Profile";

    // set user session variables
    $userId = $_SESSION['user_id'];
    //$userName = $_SESSION['user_name'];
    $temp = $_SESSION['user_name'];

    // if user update the data
      if(isset($_POST['update'])){                  


                ///////////////////// USER PROFILE IMAGE
                 // instantiate user profile image
            $Destination = 'userfiles/avatars';
            if(!isset($_FILES['ImageFile']) || !is_uploaded_file($_FILES['ImageFile']['tmp_name'])){
                $NewImageName = 'default.png';
                move_uploaded_file($_FILES['ImageFile']['tmp_name'], "$Destination/$NewImageName");
            }
            else{
                $RandomNum   = rand(0, 9999999999);
                $ImageName = str_replace(' ','-',strtolower($_FILES['ImageFile']['name']));
                $ImageType = $_FILES['ImageFile']['type'];
                $ImageExt = substr($ImageName, strrpos($ImageName, '.'));
                $ImageExt = str_replace('.','',$ImageExt);
                $ImageName = preg_replace("/\.[^.\s]{3,4}$/", "", $ImageName);
                $NewImageName = $ImageName.'-'.$RandomNum.'.'.$ImageExt;
                move_uploaded_file($_FILES['ImageFile']['tmp_name'], "$Destination/$NewImageName");
            }


        /***** Multiple query ******/


                    $firstname = trim($_POST['firstname']);
                    $lastname = trim($_POST['lastname']);
                    $email = trim($_POST['email']);
                    $user_name = trim($_POST['user_name']);
                    $profession = trim($_POST['profession']);
                    $phone = trim($_POST['phone']);
                        $postcode = trim($_POST['postcode']);
                    $address = trim($_POST['address']);
                    $bio = trim($_POST['bio']);  
                    $dob = trim($_POST['dob']);
                    $gender = trim($_POST['gender']);
                    $country = trim($_POST['country']);

    if(!($stmt = $con->prepare("SELECT * FROM user WHERE user_name = ?"))){
        die("Prepare failed: (" . $con->errno . ") " . $con->error);
    } 
    if(!$stmt->bind_param('s', $temp)){
        die("Binding parameters failed: (" . $stmt->errno . ") " . $stmt->error);
    }
    if($stmt->execute()){
        $stmt->store_result();
        $num_rows = $stmt->num_rows;
        $stmt->close();

        if($num_rows){
            if(!empty($_FILES['ImageFile']['name'])){
                if(!($stmt = $con->prepare("UPDATE user SET avatar = ? WHERE user_name = ?"))){
                    die("Prepare failed: (" . $con->errno . ") " . $con->error);
                } 
                if(!$stmt->bind_param('ss', $NewImageName, $temp)){
                    die("Binding parameters failed: (" . $stmt->errno . ") " . $stmt->error);
                }
                if($stmt->execute()){
                    $stmt->close();
                    header("location:edit-profile.php?user_name=" . $temp);
                    exit();
                }else{
                    die("Execute failed: (" . $stmt->errno . ") " . $stmt->error);
                }
            }
        }else{
            if(!($stmt = $con->prepare("INSERT INTO user (avatar) VALUES (?)"))){
                die("Prepare failed: (" . $con->errno . ") " . $con->error);
            } 
            if(!$stmt->bind_param('s', $NewImageName)){
                die("Binding parameters failed: (" . $stmt->errno . ") " . $stmt->error);
            } 
            if($stmt->execute()){
                $stmt->close();
                header("location:edit-profile.php?user_name=" . $temp);
                exit();
            }else{
                die("Execute failed: (" . $stmt->errno . ") " . $stmt->error);
            }
        }
    }else{
        die("Execute failed: (" . $stmt->errno . ") " . $stmt->error);
    }



  // ************* UPDATE PROFILE INFORMATION ************************//
  if(!($stmt = $con->prepare("UPDATE user SET firstname = ?, lastname = ?, skills = ?, profession = ?, 
  user_name = ?, phone = ?, address = ?, email = ?, bio = ?,
  gender = ?, dob = ?, country = ? WHERE id = ?"))) {
      echo "Prepare failed: (" . $con->errno . ")" . $con->error;
  }
  if(!$stmt->bind_param('sssssissssssi', $firstname, $lastname, $skills, $profession, 
                        $user_name, $phone, $address, $email, $bio, 
                        $gender, $dob, $country, $userId)){
    echo "Binding paramaters failed:(" . $stmt->errno . ")" . $stmt->error;
          }
  if(!$stmt->execute()){
           echo "Execute failed: (" . $stmt->errno .")" . $stmt->error;
          }


          if($stmt) {
              $_SESSION['main_notice'] = "Successfully Updated!";
                           header('Location: profile.php');
                              exit;

          }else{
                  $_SESSION['main_notice'] = "Some error, try again";
                  header('Location: '.$_SERVER['PHP_SELF']);
              }

   $stmt->close();

} // }

    //mysqli_close($con);
            ?>

            <?php
              //include header layout
                require_once('include/header.php');
              ?>

            <!--------------------------- Redirect based on user type ----------------------->
              <?php
                  if($_SESSION['user_type'] == 'admin' || $_SESSION['user_type'] == 'leader'){

                        // include admin header layout
                      require_once('include/admin-header.php');

                      }elseif($_SESSION['user_type'] == 'member'){

                       // include  member header layout
                      include_once('include/member-header.php');

                      }else{    

                        session_destroy();
                        header('Location: index.php');
                      }
            ?>
            <!--  check if userId is found, if not throw error ---->
              <?php 

     $stmt = $con->prepare("SELECT firstname, lastname, skills, user_name, avatar, profession, email, dob, gender, country, phone, bio, address, created_at FROM user WHERE id = ?");
    $stmt->bind_param('s', $userId);
    $stmt->execute();
    $stmt->store_result();  
    if($stmt->num_rows == 0) {  
        echo 'No Data Found for this user';
    }else {
        $stmt->bind_result($firstname, $lastname, $skills, $user_name, $avatar, $profession, $email, $dob, $gender, $country, $phone, $bio, $address, $created_at);
        $stmt->fetch();

      $stmt->close();

    }
                ?>

And here is my HTML code.

                            <form name="update" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data" id="UploadForm">

                  <table>
                        <label for="">Avatar</label>
                        <input name="ImageFile" type="file" id="uploadFile"/>
    <!--                     <div>
                            <div class="shortpreview">
                                <label for="">Previous Avatar </label>
                                <br> 
                                <img src="userfiles/avatars/<?php //echo $avatar;?>" width='150' height='150' >
                            </div>
                        </div> -->
                        <div class="col-md-6">
                            <div class="shortpreview" id="uploadImagePreview">
                                <label for="">Current Uploaded Avatar </label>
                                <br> 
                                <div id="imagePreview"></div>
                            </div>
                        </div>
                     </table>


                              <table>
                              <tr>
                                <td></td>
                                <td></td>
                                <td>Update</td>
                              </tr>
                              <tr>
                              <td><label for = "">First Name</label></td>
                              <td><input type="text" id="firstname" name="firstname" value="<?php echo $firstname; ?>"></td>
                              </tr>

                              <tr>
                              <td><label for = "">Last Name</label></td>
                              <td><input type="text" id="lastname" name="lastname"  value ="<?php echo $lastname; ?>"></td>
                              </tr>

                              <tr>
                              <td><label for = "">Postcode</label></td>
                              <td><input type="postcode" id="postcode" name="postcode" value="<?php echo $postcode; ?>"></td>
                              </tr>

                              <tr>
                              <td><label for = "">User Nmae</label></td>
                              <td><input type="user_name" id="user_name" name="user_name" value="<?php echo $user_name; ?>"></td>
                              </tr>

                              <tr>
                              <td><label for = "">Profession Name</label></td>
                              <td><input type="text" id="profession" name="profession" value="<?php echo $profession; ?>"></td>
                              </tr>                            

                              <tr>
                              <td><label for = "">Phone</label></td>
                              <td><input type="text" id="phone" name="phone" value="<?php echo $phone; ?>"></td>
                              </tr>                           

                               <tr>
                              <td><label for = "">Email</label></td>
                              <td><input type="text" id="email" name="email" value="<?php echo $email; ?>"></td>
                              </tr>

                              <tr>
                              <td><label for = "">Gender</label></td>
                              <td><input type="text" id="gender" name="gender" value="<?php echo $gender; ?>"></td>
                              </tr>

                              <tr>
                              <td><label for = "">Date Of Birth</label></td>
                              <td><input type="text" id="dob" name="dob" value="<?php echo $dob; ?>"></td>
                              </tr>


                              <tr>
                              <td><label for = "">Addres</label></td>
                              <td><input type="text" id="address" name="address" value="<?php echo $address; ?>"></td>

                                </tr>


                              <tr>
                              <td><label for = "">Country</label></td>
                              <td><input type="text" id="country" name="country" value="<?php echo $country; ?>"></td>
                              </tr>  


                              <tr>
                              <td><label for = "">Bio</label></td>
                              <td><input type="text" id="bio" name="bio" value="<?php echo $bio; ?>"></td>
                              </tr>

                              <tr>
                              <td></td>             
                              <td><input type="submit" id="update" name="update" value="Update"></td>
                              </tr>
                              </table>
                            </form>     
                        </div>

What notice when i take out the $postcode field i just added it works fine, which left me baffled on what the problem is. As have mention earler have had a through the previous questions and answer no luck.

olaskee
  • 15
  • 5
  • what changed in your code since it last worked? that's what you need to find out and revert to what you last used and using a copy of that file in order to add / use what you're trying to do now. Which codes did you add and for which line is the error on? – Funk Forty Niner Oct 14 '16 at 12:07
  • make sure you reload everything and have cleared the cache, and have chosen the right table(s) and database. Check your connection also and add `exit;` after every header. – Funk Forty Niner Oct 14 '16 at 12:40
  • @Fred-ii- you suggestion 40min ago is very true... i will take that on board and see what outcome is. – olaskee Oct 14 '16 at 12:49

1 Answers1

-1

Your problem is here:

   if(!($stmt = $con->prepare("UPDATE user SET firstname = ?, lastname = ?, profession = ?, user_name = ?, phone = ?, address = ?, email = ?, bio = ?,
      gender = ?, postcode = ?, dob = ?, country = ? WHERE id = ?"))) {
          echo "Prepare failed: (" . $con->errno . ")" . $con->error;
      }
      if(!$stmt->bind_param('ssssissssssi', $firstname, $lastname, $profession, $user_name, $phone, $postcode, $address, $email, $bio, $gender, $dob, $country, $userId)){
        echo "Binding paramaters failed:(" . $stmt->errno . ")" . $stmt->error;
              }

You placed a bind param with $postcode but you did not place postcode = ?, in the $con->prepare()

replace it with this:

  if(!($stmt = $con->prepare("UPDATE user SET firstname = ?, lastname = ?, profession = ?, user_name = ?, phone = ?, postcode = ?, address = ?, email = ?, bio = ?,
      gender = ?, postcode = ?, dob = ?, country = ? WHERE id = ?"))) {
          echo "Prepare failed: (" . $con->errno . ")" . $con->error;
      }
      if(!$stmt->bind_param('ssssissssssi', $firstname, $lastname, $profession, $user_name, $phone, $postcode, $address, $email, $bio, $gender, $dob, $country, $userId)){
        echo "Binding paramaters failed:(" . $stmt->errno . ")" . $stmt->error;
              }
kerv
  • 315
  • 1
  • 2
  • 13
  • Huh? It's in there. I can't see the difference here; what am I not grasping? – Funk Forty Niner Oct 14 '16 at 11:57
  • No its not, look at the bind param and the prepare statement in the first bit of code i copied. In the bind_param there is $postcode but in the $con->prepare there is no postcode = ? – kerv Oct 14 '16 at 11:59
  • @kerv... i think Fred-ii- is right is there.... have used your suggestion though, but is not working because is there – olaskee Oct 14 '16 at 12:00
  • `if(!($stmt = $con->prepare("UPDATE user SET firstname = ?, lastname = ?, profession = ?, user_name = ?, phone = ?, address = ?, email = ?, bio = ?, gender = ?, postcode = ?, dob = ?, country = ? WHERE id = ?"))) { echo "Prepare failed: (" . $con->errno . ")" . $con->error; }` - `postcode = ?` is there. Again; what am I not getting here? – Funk Forty Niner Oct 14 '16 at 12:00
  • ok sorry, your right but then you have placed the $postcode in the wrong order – kerv Oct 14 '16 at 12:01
  • you have placed $postcode between $phone and $adress – kerv Oct 14 '16 at 12:02
  • @Fred-ii- sorry i don't understand what you meant... that's the update query – olaskee Oct 14 '16 at 12:02
  • Ok, it's the ordering. @kerv I see it now. I had to paste that in my editor to see what you meant. You should have specified that in the answer ;-) That way I'd of gotten it right away *lol* – Funk Forty Niner Oct 14 '16 at 12:03
  • @Fred-ii- Sorry for my ignorant am new to this, but is neccessary which order they are, those that affect the query in anyway? – olaskee Oct 14 '16 at 12:04
  • I see you got a downvote, it's not mine @kerv but I have a good guess as to who. – Funk Forty Niner Oct 14 '16 at 12:04
  • @olaskee Yes it is important and the column's types with the `i` and `s`'s need to all match. – Funk Forty Niner Oct 14 '16 at 12:04
  • this is not the case because error in question is related to prepare, not bind_param call – Your Common Sense Oct 14 '16 at 12:05
  • have now fix that... but the same still coming up, can take a look at the second query from this line `$stmt = $con->prepare("SELECT firstname, lastname, postcode` do you see anything wrong there? – olaskee Oct 14 '16 at 12:23
  • 1
    No, you should probably follow @YourCommonSense advice and turn on error reporting http://stackoverflow.com/questions/22662488/how-to-get-mysqli-error-in-different-environments – kerv Oct 14 '16 at 12:25
  • @olaskee Look at your type `type="postcode"` that isn't a valid type in HTML or HTML5, use `text` and the same for `type="user_name"` that's why you're getting errors. and this answer needs to reflect all that I have said here and not have people rely on comments to see what the real issues were. – Funk Forty Niner Oct 14 '16 at 12:27
  • @kerv The OP does have error reporting and error checking on the queries. – Funk Forty Niner Oct 14 '16 at 12:31
  • The problem here is, even with error reporting and error checking on, it is failing silently because of the invalid input types. I've reopened the question. – Funk Forty Niner Oct 14 '16 at 12:33
  • @Fred-ii- so have change that accordingly, but no luck. – olaskee Oct 14 '16 at 12:35
  • Right! Missed that. – kerv Oct 14 '16 at 12:37
  • @olaskee Make sure all column types are correct & are long enough to hold the data The order is important as is the `i` and `s`. I suggest you make a copy of your problematic files, take out the one that originally gave you problems. Once you've gotten that to work, add the one(s) that gave you trouble and debug from there, while using `var_dump($x_VAR);` and look at your HTML source. Since I've reopened the question, someone else may see something that we didn't. You should also edit your question that contains the db schema and example values. Make sure sessions all contain values, etc. – Funk Forty Niner Oct 14 '16 at 12:39
  • @Fred-ii- thanks for the contribution, that's exactly what am doing now. Also you mention you reopen the question can i see the link for please – olaskee Oct 14 '16 at 12:44
  • @Fred-ii- @kerv... have now solve the problem now, but am now getting this problem again `Execute failed: (1062) Duplicate entry '' for key 'email'` i don't have any duplicate entry in my database. – olaskee Oct 14 '16 at 14:03