亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

為什么 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 關注
  • 156 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號