如何从php文件中获取jquery函数中的信息
我的问题是如何从php文件到jquery ajax脚本中获取db信息(在我的例子中,仅指一个数字),因此我的jquery:如何从php文件中获取jquery函数中的信息,php,jquery,mysql,ajax,json,Php,Jquery,Mysql,Ajax,Json,我的问题是如何从php文件到jquery ajax脚本中获取db信息(在我的例子中,仅指一个数字),因此我的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,
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'";
问题出在php脚本中。请不要使用
mysql\u查询
。使用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);
}
});
安全注意:像这样使用MySQL查询很容易出现SQL注入。在您的系统的生产环境中非常不受鼓励;)