Reputation: 161
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+"¬es="+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));
}
Upvotes: 0
Views: 224
Reputation: 978
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.
Upvotes: 0
Reputation: 35973
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']));
}
Upvotes: 1