苦苦挣扎调试成员函数bind_param()

时间:2016-10-14 11:47:19

标签: php debugging mysqli prepared-statement

我知道这看起来像是一个重复的问题。看过不同的问题和答案,但很难得到结果。所以基本上我的编辑配置文件工作得很好,在我决定有更多的字段和仍然字段以某种方式导致问题。所以我得到了这个错误

  

致命错误:在布尔值上调用成员函数bind_param()。

我将向您展示我的 php代码

       <?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();

    }
                ?>

这是我的HTML代码。

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

当我拿出$postcode字段时,我刚刚添加它时会发生什么通知它运行正常,这让我感到困惑的是问题所在。至于提到勒勒已经通过以前的问题,并回答没有运气。

1 个答案:

答案 0 :(得分:-1)

你的问题在这里:

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

您在$ con&gt; prepare()

中放置了一个带有$ postcode的绑定参数,但没有将postcode =?

替换为:

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