为什么 PHP jQuery Ajax 会出现执行错误?

我正在尝试使用 jQuery Ajax 删除 PHP 帖子,但由于某种原因我遇到了 2 种不同类型的错误。


console.log 显示以下内容:


警告 PDO::prepare() 期望参数 1 为字符串,即 ajax_functions.php 第 41 行中给出的对象


致命错误未捕获错误:在 ajax_functions.php:43 中调用 bool 上的成员函数execute()


有谁知道是什么原因造成的?


脚本.JS


 $('.post-remove-btn').on('click', function(e) {


                e.preventDefault();  

                var entered_name = $('.name-input').val();


                $.ajax({

                        method: "POST",

                        url: 'functions/ajax_functions.php',  

                        data: { table1: table, tableNr1: tableNr,  'action': 'remove' },

                        success: function(data) {

                                if(data){

                                     console.log(data);

                                    

                                }else{

                                    alert('error');

                                }

                                

                                

                        },

                        error: function(requestObject, error, errorThrow) {

                                alert('error');

                        }

                });


 });    

AJAX_FUNCTIONS.PHP


<?php



include("../database.php");



// AJAX

if( isset($_POST['action']) ) {

  

    if($_POST['action'] == 'remove'){

        


        $table1 = $_POST['table1'];     

        $tableNr1 = $_POST['tableNr1'];  


        if($table1 == 'post'){


            $sql = $pdo->query('DELETE FROM posts WHERE id = ' . $tableNr1);   

            

            $stmt2 = $pdo->prepare($sql); // PDO::prepare() expects parameter 1 to be string

         

            $result = $stmt2->execute();  // Uncaught Error: Call to a member function execute() on bool


            if($result==true){  

                 echo 'post deleted';

            }


        }elseif ($table1 == 'thread') {

            echo 'thread';

        }

    }

}


小怪兽爱吃肉
浏览 98回答 1
1回答

白板的微信

我正在尝试使用 jQuery Ajax 删除 PHP 帖子,但由于某种原因我遇到了 2 种不同类型的错误。console.log 显示以下内容:警告 PDO::prepare() 期望参数 1 为字符串,即 ajax_functions.php 第 41 行中给出的对象致命错误未捕获错误:在 ajax_functions.php:43 中调用 bool 上的成员函数execute()有谁知道是什么原因造成的?脚本.JS&nbsp;$('.post-remove-btn').on('click', function(e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.preventDefault();&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var entered_name = $('.name-input').val();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $.ajax({&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; method: "POST",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; url: 'functions/ajax_functions.php',&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data: { table1: table, tableNr1: tableNr,&nbsp; 'action': 'remove' },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; success: function(data) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(data){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;console.log(data);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alert('error');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; error: function(requestObject, error, errorThrow) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alert('error');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp;});&nbsp; &nbsp;&nbsp;AJAX_FUNCTIONS.PHP<?phpinclude("../database.php");// AJAXif( isset($_POST['action']) ) {&nbsp;&nbsp;&nbsp; &nbsp; if($_POST['action'] == 'remove'){&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $table1 = $_POST['table1'];&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $tableNr1 = $_POST['tableNr1'];&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; if($table1 == 'post'){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $sql = $pdo->query('DELETE FROM posts WHERE id = ' . $tableNr1);&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $stmt2 = $pdo->prepare($sql); // PDO::prepare() expects parameter 1 to be string&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $result = $stmt2->execute();&nbsp; // Uncaught Error: Call to a member function execute() on bool&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if($result==true){&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;echo 'post deleted';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }elseif ($table1 == 'thread') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo 'thread';&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP