Reputation: 479
I want there to be a SQL query to take place when a link is clicked on, so far this is what I have:
mainpage.php:
function deleteImage1() {
$.post("delete_image.php", { num:1, id:<?php echo $id; ?> }, function(data,status){
alert("Data: " + data + "\nStatus: " + status);
});
return false;
}
<a href="#" onclick="deleteImage1()">Delete Image</a>
delete_image.php:
<?php
// $connect stuff here
$num = $_GET['num'];
$id = $_GET['id'];
if ($num === '1') {
$image_num == '';
} else {
$image_num == $num;
}
$sqlCommand = mysql_query("UPDATE alpacas1 SET image$image_num='' WHERE id=$id");
if (!$sqlCommand) {
die('Invalid query: ' . mysql_error());
} else {
echo "Updated successfully!";
}
?>
Now, when I click on the "Delete Image" link, it try's to run the function, and it does, but returns as this in the popup:
Data: Invalid query: You have an error in your SQL syntax; check the manual that corresponds with your MySQL server version for the right syntax to use near " at line 1.
Status: success
But when I reload the page, (or look in the database), the image field has not been changed to '' (or null).
Also, I have tested the SQL query so it is not that. I have done so by visiting delete_image.php in my browser with the id & num variables being "posted" (ex. delete_image.php?num=1&id=20) and by visiting it IN BROWSER, it SUCCESSFULLY deletes the image and gives me the message "Updated successfully!".
I'm guessing it's something with my Javascript as I am a beginner! Thanks for your help!
Upvotes: 0
Views: 2238
Reputation: 1962
I should've noticed this earlier...
You're using the jQuery .post()
, yet you're accessing the variables as if they are sent via GET
. You should either use $_POST
or $_REQUEST
to pull in your data:
$num = $_POST['num'];
$id = $_POST['id'];
Without the proper variables, your query reads:
UPDATE alpacas1 SET image='' WHERE id="
Thus the MySQL error is returned.
Upvotes: 2