Reputation: 11
I'm having trouble getting this AJAX code to update my database. The code is a image that onClick
will run the command to update the database
HTML:
<a>
<img class = "heart"
src = "images/heart.png"
onClick = "favUpdate(0,1)"
onMouseover = "this.src='images/heart_mo.png'"
onMouseout = "this.src='images/heart.png'"/>
</a>
Javascript code:
function favUpdate(fav_up, id_up) {
$.ajax({
type: 'post',
url: 'includes/fav_update.php',
data: {favorite: fav_up, id: id_up},
success: function(output) {
alert('success, server says '
+ output
+ 'Variables passed are '+fav_up+' '+id_up);
},
error: function() {
alert('something went wrong, Favorite update failed');
}
});
}
PHP code:
<?php
require_once('../Connections/main.php');
$fav_update = mysql_real_escape_string($_POST['favorite']);
$fav_id = mysql_real_escape_string($_POST['id']);
$query = "UPDATE projects SET favorite = $fav_update WHERE id = $fav_id";
mysql_query($query, $main);
?>
main.php
<?php
$hostname_main = "localhost";
$database_main = "test";
$username_main = "root";
$password_main = "";
$main = mysql_pconnect($hostname_main, $username_main, $password_main) or trigger_error(mysql_error(),E_USER_ERROR);
?>
Does anyone know why it's not updating the database and why the "option" isn't getting data for the variable?
Upvotes: 0
Views: 515
Reputation: 11
Changed php script to:
<?php
require_once('../Connections/main.php');
$fav_update = mysql_real_escape_string($_POST['favorite']);
$fav_id = mysql_real_escape_string($_POST['id']);
$updateSQL = sprintf("UPDATE projects
SET favorite=%s
WHERE id=%s",
$fav_update,
$fav_id);
mysql_select_db($database_main, $main);
$Result1 = mysql_query($updateSQL, $main) or die(mysql_error());
?>
hope that helps if anyone runs into the same issue
Upvotes: 0
Reputation: 3704
Please try to debug your PHP (server side code first) :-
<?php
require_once('../Connections/main.php');
$fav_update = mysql_real_escape_string($_REQUEST['favorite']);
$fav_id = mysql_real_escape_string($_REQUEST['id']);
$query = "UPDATE projects SET favorite = $fav_update WHERE id = $fav_id";
echo $query;
mysql_query($query, $main);
?>
Use $_REQUEST instead of $_POST, and call this api directly from the browser, by creating its url like http://localhost/filename.php?favorite=somevalue1&id=somevalue2
And check whether you got the insert in DB or not, and check the query by printing it too.
And after checking the API, please change the $_REQUEST, back to $_POST
Upvotes: 0
Reputation: 30488
you have to put single quote around $fav_update
if its datatype is string VARCHAR,TEXT
$query = "UPDATE projects SET favorite = '$fav_update' WHERE id = $fav_id";
^ ^
Remove $main
from here and try
mysql_query($query);
Upvotes: 0
Reputation: 1331
Try this
<?php
require_once('../Connections/main.php');
$fav_update = mysql_real_escape_string($_POST['favorite']);
$fav_id = mysql_real_escape_string($_POST['id']);
$query = "UPDATE projects SET favorite = '".$fav_update."' WHERE id = '".$fav_id."'";
mysql_query($query, $main);
?>
Upvotes: 1