我的问题是如何从php文件到jquery ajax脚本获取数据库信息(在我的例子中仅指一个数字),所以这是我的jquery:
function rate_down(id) {
var id = id;
//submit data to php script
var data = {
"id": id,
};
$.ajax({
type: "POST",
url: "rating.php",
data: data,
success: function(response) {
var currentValue = /* here i want to put the db number */
var newValue = +currentValue - 1;
$("#points_"+id).text(newValue);
},
error: function(jqXHR, textStatus, errorThrown){
alert(errorThrown);
}
});
};
我想要我的raiting.php。我不确定是否应该发布它,因为它没用,但这是我在raiting.php中的mysql查询:
$pic_id = (int) $_REQUEST['id'];
mysql_query = mysql_query"SELECT points FROM `photos` WHERE `id` = '$pic_id'";
答案 0 :(得分:0)
问题在于你的php脚本。请不要使用mysql_query
。使用PDO
。
$pic_id = (int) $_REQUEST['id'];
$db = new PDO("...");
$q = $db->prepare("SELECT points FROM `photos` WHERE `id` = :pic_id");
$q->bindValue(':pic_id', $pic_id);
$q->execute();
if ($q->rowCount() > 0){
$check = $q->fetch(PDO::FETCH_ASSOC);
$points = $check['points'];
}else{
$points = 0;
}
echo $points;
然后在你的ajax函数中:
$.ajax({
type: "POST",
url: "rating.php",
data: data,
success: function(response) {
if(response != 0) {
var currentValue = response;
var newValue = +currentValue - 1;
$("#points_"+id).text(newValue);
}
},
error: function(jqXHR, textStatus, errorThrown){
alert(errorThrown);
}
});