0

I am trying to update a database entry through jQuery and AJAX. I am checking that the values i send over is correct - but I am not sure how to check why the database is not updated.

My code is as follows:

$(document).on("click", ".approve", function(){
    var classes = $(this).parents('div:eq(0)'); // this gets the parent classes.
    i = 0;
    var pros = [];
    classes.find(".prosncons .pros ul li").each(function(){
        pros.push($(this).text());
    });
    var cons = [];
    classes.find(".prosncons .cons ul li").each(function(){
        cons.push($(this).text());
    });
    var notes = classes.find(".notes").text();
    var id = classes.find(".id").text();
    var data = "method=approve&pros="+pros+"&cons="+cons+"&notes="+notes+"&id="+id;
    $.ajax({
      type: "POST",
      url: "../scripts/upload.php",
      data: data,
      success: $(this).closest(".approval").remove(),
    });
});

PHP::

if($method == "approve"){
        $sql = "UPDATE `approval` SET approved = 1 WHERE pros=:pros, cons=:cons, notes=:notes, id=:id";
        $statement = $conn->prepare($sql);
        $statement->execute(array(':pros' => $pros, ':cons' => $cons, ':notes' => $notes, ':id'=> $id));
    }
2
  • You're not checking the result of $statement->execute(...) is it working? Commented Sep 12, 2013 at 20:13
  • Also, you are setting up those variables from the $_POST array are you? They do not exist automatically as separate variables. Commented Sep 12, 2013 at 20:16

2 Answers 2

1

You are t sending in the right way your data to the php file

Change your ajax request with this:

$.ajax({
      type: "POST",
      url: "../scripts/upload.php",
      data: { method: "approve", pros: pros, cons:cons, note:notes, id:id },
      success: $(this).closest(".approval").remove(),
    });

To get your variable into the php file you can retireve that with $_POST['var_name'] In your php try this to check method:

if($_POST['method'] == "approve"){
        $sql = "UPDATE `approval` SET approved = 1 WHERE pros=:pros, cons=:cons, notes=:notes, id=:id";
        $statement = $conn->prepare($sql);
        $statement->execute(array(':pros' => $_POST['pros'], ':cons' => $_POST['cons'], ':notes' => $_POST['notes'], ':id'=> $_POST['id']));
    }
Sign up to request clarification or add additional context in comments.

3 Comments

If the problem is the lack of variable initialisation, (with regard to your $_POST usage), then the other variables probably need that too.
I have the variable initialized further up my PHP script ^^ But thanks for the AJAX help :)
Unfortunately this does still not work - My best guess would be the php update string? Do i need it to be AND or "," in between the WHERE conditions?
0

You can check $conn->error for the last error. This should tell you if you have an error.

I would normally check if there is an error and if there is I would return a status of error so my JS code knows there was a problem, then handle it.

2 Comments

Well I can't really do that - as I am sending the info via jQuery I am never really seeing the page where this would be output.
You can see the response page via the Chrome Dev Tools or the Firebug in FF. And in your success: you can check for errors. success: function(result) { if(result === 'error') { /*do something*/} else {/*do the remove*/} }. You get the picture.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.