为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
小怪兽爱吃肉 2023-10-22 21:17:09
我正在尝试使用 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<?phpinclude("../database.php");// AJAXif( 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';        }    }}
查看完整描述

1 回答

?
白板的微信

TA贡献1883条经验 获得超3个赞

我正在尝试使用 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';

        }

    }

}


查看完整回答
反对 回复 2023-10-22
  • 1 回答
  • 0 关注
  • 58 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信