0

I have a failing PDO Prepared DELETE statement inside a PHP file called with AJAX.

My AJAX call looks like this:

var data = {action: "deleteTask", value: "1"};

$.ajax({    
        type: "POST",
        dataType: "json",
        url: "ajax.php", 
        data: data,
        success: function(data) {                
            alert(data);
        },
        error: function() {
            alert("Something's wrong!");                
        }
});

My ajax.php looks like this:

<?php 
...   
function delete(){
            $return = "something must be right...";

            require('connect.php');                            

            $sql = "DELETE FROM 'tasks' WHERE 'task_id' = ?";
            $stmt = $dbcon->prepare($sql);             
            $stmt->execute(array($_POST["value"]));

            $return["json"] = json_encode($return);
            echo json_encode($return);
        } 
?> 

Here is a sample of connect.php:

<?php

// Configuration
$username = 'me';
$password = '1234';
$server = 'localhost';
$database = 'mydb';

try {
    $dbcon = new PDO("mysql:host=$server;dbname=$database",$username,$password);
    $dbcon->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
} catch(PDOException $er) {
    echo 'ERROR: ' . $er->getMessage();
}

?>

A few things that I have already established:
1) My ajax call works fine if I comment out everything related to the PDO statement;
2) My connect.php file works, because I have tested it with standard HTML forms (as opposed to calling it from ajax);
3) there are no typos in the names of the table and the field of the DB;
4) The $_POST["value"] gives the the right value corresponding to the record I am trying to delete.
5) When I set brakepoints on each line related to the PDO, when I am at the line: $stmt->execute(array($_POST["value"])); and I click "Step Into", it jumps straight to the alert("Something's wrong!"); in the Ajax (nothing gets executed in the ajax.php after that line)

I have spent many hours trying to figure out what is wrong with this and read all the posts that exist on the subject, but my problem seems particular, so any help would be much appreciated! Thanks.

Neo_999
  • 151
  • 1
  • 1
  • 9
  • 2
    you have syntax errors but you're not checking for errors. the problem is obvious here – Funk Forty Niner Jul 23 '15 at 04:12
  • The other question is not about a prepared statement.. I also seem to have a problem particularly because the ajax.php is called externally from Ajax. It works if I call it with a simple HTML form and POST method. – Neo_999 Jul 23 '15 at 04:29

2 Answers2

3

You should look into the browser's the Net responce. It should show a SQL error. If you use MySQL your query should look:

DELETE FROM `tasks` WHERE `task_id` = ?

And if it is PostgreSQL:

DELETE FROM "tasks" WHERE "task_id" = ?
koredalin
  • 446
  • 3
  • 11
0

Seems like you forgot to add the code:

delete();

Once you will add it, you will actually call the function and not only declare it.

Another option, try and set PDO to print error. http://php.net/manual/en/pdo.errorinfo.php Then you will the error printed back in the console.

BenB
  • 2,747
  • 3
  • 29
  • 54
  • Hello, batz! Thanks for the suggestion! Unfortunately, that is not it. I call the function in the "..." part. There is some verification stuff in that part to check if the call is an Ajax call, and to call the function if it is. But, I know the function gets called, because I get to the first breakpoints on the first lines of the PDO statement. – Neo_999 Jul 23 '15 at 04:11
  • Have you tried to print the PDO error? http://php.net/manual/en/pdo.errorinfo.php – BenB Jul 23 '15 at 04:14
  • I just tried it. No problem with the $stmt apparently. The problem really seams to be related to the connection to the DB not being established and $dbcon remaining not initiated even after the required connect.php runs. – Neo_999 Jul 23 '15 at 05:15
  • Maybe you calling the $dbcon from out of the fanction without calling GLOBAL before? – BenB Jul 23 '15 at 05:47