我无法获取此 AJAX 代码来更新我的数据库。该代码是onClick
将运行命令以更新数据库的图像
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代码:
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代码:
<?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);
?>
主文件
<?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);
?>
有谁知道它为什么不更新数据库以及为什么“选项”没有获取变量的数据?