“致命错误:在尝试更新ajax中的数据库时,调用成员函数execute()上的boolean()”

时间:2019-03-02 11:55:31

标签: php jquery ajax validation

下面是我的index.php文件,其中包含我在互联网上找到的内容。

我已经编写了所有代码,除了未提交表单且未显示“注册成功完成”的警告消息外,其他所有程序都工作正常。

相反,我收到此错误:

  

致命错误:在线上的boolean()上调用成员函数execute()   25

我还在名为“ charu”的数据库中创建了一个tbl_register表,该表包含5个具有空值的列。

HTML:

<html>  
    <head>  
        <title>PHP Form Validation using Parsleys.js Library</title>  
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />  
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
  <script src="http://parsleyjs.org/dist/parsley.js"></script>
    </head>

    <body>  
        <div class="container">  
            <br />  
            <br />
   <br />
   <div class="table-responsive">  
    <h3 align="center">PHP Form Validation using Parsleys.js Library</h3><br />
    <div class="box">
     <form id="validate_form">
      <div class="row">
       <div class="col-xs-6">
        <div class="form-group">
         <label>First Name</label>
         <input type="text" name="first_name" id="first_name" placeholder="Enter First Name" required data-parsley-pattern="^[a-zA-Z]+$" data-parsley-trigger="keyup" class="form-control" />
        </div>
       </div>
       <div class="col-xs-6">
        <div class="form-group">
         <label>Last Name</label>
         <input type="text" name="last_name" id="last_name" placeholder="Last Name" required data-parsley-pattern="^[a-zA-Z ]+$" data-parsley-trigger="keyup" class="form-control" />
        </div>
       </div>
      </div>
      <div class="form-group">
       <label for="email">Email</label>
       <input type="text" name="email" id="email" placeholder="Email" required data-parsley-type="email" data-parsley-trigger="keyup" class="form-control" />
      </div>
      <div class="form-group">
       <label for="password">Password</label>
       <input type="password" name="password" id="password" placeholder="Password" required data-parsley-length="[8, 16]" data-parsley-trigger="keyup" class="form-control" />
      </div>
      <div class="form-group">
       <label for="cpassword">Confirm Password</label>
       <input type="password" name="confirm_password" id="confirm_password" placeholder="Confirm Password"data-parsley-equalto="#password" data-parsley-trigger="keyup" required class="form-control" />
      </div>
      <div class="form-group">
       <label for="cpassword">Website</label>
       <input type="text" id="website" name="website" placeholder="Website URL" data-parsley-type="url" data-parsley-trigger="keyup" class="form-control" />
      </div>
      <div class="form-group">
       <div class="checkbox">
        <label><input type="checkbox" id="check_rules" name="check_rules" required /> I Accept the Terms & Conditions</label>
       </div>
      </div>
      <div class="form-group">
       <input type="submit" id="submit" name="submit" value="Submit" class="btn btn-success" />
      </div>
     </form>
    </div>
   </div>  
  </div>
    </body>  
</html>  

JQuery:

<script>  
$(document).ready(function(){  
    $('#validate_form').parsley();

    $('#validate_form').on('submit', function(event){
        event.preventDefault();
        if($('#validate_form').parsley().isValid())
        {
            $.ajax({
                url:"action.php",
                method:"POST",
                data:$(this).serialize(),
                beforeSend:function(){
                    $('#submit').attr('disabled','disabled');
                    $('#submit').val('Submitting...');
                },
                success:function(data)
                {
                    $('#validate_form')[0].reset();
                    $('#validate_form').parsley().reset();
                    $('#submit').attr('disabled',false);
                    $('#submit').val('Submit');
                    alert(data);
                }
            });
        }
    });
});  
</script>

action.php文件:

<?php

sleep(5);

if(isset($_POST['first_name']))
{
    $connect = new mysqli("localhost", "root", "","charu");

    $data = array(
        ':first_name'  => $_POST['first_name'],
        ':last_name'  => $_POST['last_name'],
        ':email'   => $_POST['email'],
        ':password'   => $_POST['password'],
        ':website'   => $_POST['website']
    );

    $query = "
       INSERT INTO tbl_register 
       (first_name, last_name, email, password, website) 
       VALUES (:first_name, :last_name, :email, :password, :website)
    ";
    $statement = $connect->prepare($query);
    if($statement->execute($data))
    {
        echo 'Registration Completed Successfully...';
    }
}
?>

3 个答案:

答案 0 :(得分:0)

您必须自己打印错误才能查看实际原因。 做这个。在if语句之后

else{
echo $connect->error;
echo $statement->error;
}

您可以复制和使用代码。我已经修改了。可以。

if(isset($_POST['first_name']))
{
 $connect = new mysqli("localhost", "root", "","charu");


  $first_name  = $_POST['first_name'];
  $last_name  = $_POST['last_name'];
  $email   = $_POST['email'];
  $password   = $_POST['password'];
  $website   = $_POST['website'];

 $query = "
 INSERT INTO tbl_register 
 (first_name, last_name, email, password, website) 
 VALUES (?, ?, ?, ?, ?)
 ";
 if(!$statement = $connect->prepare($query)){
echo $connect->error;
echo $statement->error;

}
else{
$statement->bind_param("sssss", $first_name, $lastname, $email, $password, $website);
If(statement->execute()){
  echo 'Registration Completed Successfully...';
 }
} }

免费询问费用,让lemme知道反馈。

答案 1 :(得分:0)

您必须将参数绑定到SQL查询,并告诉数据库参数是什么。

// prepare sql and bind parameters
$query = "
 INSERT INTO tbl_register 
 (first_name, last_name, email, password, website) 
 VALUES (:first_name, :last_name, :email, :password, :website)
 ";

$statement = $connect->prepare($query);

$statement->bindParam(':first_name', $_POST['first_name']);
$statement->bindParam(':last_name', $_POST['last_name']);
$statement->bindParam(':email', $_POST['email']);
$statement->bindParam(':password', $_POST['password']);
$statement->bindParam(':website', $_POST['website']);

 if($statement->execute())
 {
    echo 'Registration Completed Successfully...';
 }

答案 2 :(得分:0)

您应该使用bind_params();

替换此

  $data = array(
    ':first_name'  => $_POST['first_name'],
    ':last_name'  => $_POST['last_name'],
    ':email'   => $_POST['email'],
    ':password'   => $_POST['password'],
    ':website'   => $_POST['website']
   );

   $query = "
   INSERT INTO tbl_register 
   (first_name, last_name, email, password, website) 
   VALUES (:first_name, :last_name, :email, :password, :website)
   ";
   $statement = $connect->prepare($query);

   if($statement->execute($data))
   {
    echo 'Registration Completed Successfully...';
   }


  with


  $query = "
   INSERT INTO tbl_register 
   (first_name, last_name, email, password, website) 
   VALUES (?,?,?,?,?)
   ";
   $statement = $connect->prepare($query);

   $statement->bind_param('sssss',$_POST['first_name'],$_POST['last_name'],$_POST['email'],$_POST['password'],$_POST['website']);

   //here 's' in sssss stands for string and all your parameters are string so five 's' are added 

   if($statement->execute())
   {
    echo 'Registration Completed Successfully...';
   }