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

Posted

技术标签:

【中文标题】“致命错误:在 ajax 中尝试更新数据库时调用 boolean() 上的成员函数 execute()”【英文标题】:"Fatal error: Call to a member function execute() on boolean() "when trying to update database in ajax 【发布时间】:2019-07-24 06:57:27 【问题描述】:

下面是我在网上找到的 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...';
    

?>

【问题讨论】:

【参考方案1】:

你应该使用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...';
   

【讨论】:

非常感谢您的帮助。它工作得很好:)【参考方案2】:

您必须将参数绑定到 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...';
 

【讨论】:

【参考方案3】:

您必须自己打印错误才能查看实际原因。 做这个。在 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...';
 
 

免费询问并让我知道反馈。

【讨论】:

我已复制此内容并替换为之前的内容,现在它显示“第 32 行解析错误语法错误,其中执行方法必须起作用。现在要做什么 第 32 行是哪一行? 我以为你说的是​​ PDO。将 bindParam 更改为 bind_param("s", $firstname)

以上是关于“致命错误:在 ajax 中尝试更新数据库时调用 boolean() 上的成员函数 execute()”的主要内容,如果未能解决你的问题,请参考以下文章