我有一个小问题,就是当我将准备好的语句添加到PHP文件中时,Ajax停止工作并给我一个500错误,但当我删除该语句时,它就像一个魅力。
这是我的PHP文件:
<?php
include ('db_connect.php');
include ('functions.php');
$datad = $_POST['superstr'];
$id = 1;
$stmt = $mysqli->prepare("UPDATE `song` SET `lyrtext`=? WHERE `id`=?");
$stmt->bind_param("si", $datad, $id);
$status = $stmt->execute();
echo $datad;
?>
我的Ajax看起来像这样:
$.ajax({
url: 'includes/sendlyrics.php',
type: 'POST',
data: {superstr: 'pelle'},
success: function(data) {
//called when successful
var hello = data;
//prompt(data);
console.log("The data is:");
console.log(data);
console.log("The variable which should keep the data has this content:");
console.log(hello);
},
error: function(e) {
//called when there is an error
console.log(e.message);
prompt(e.message);
//alert(e.message);
}
});
问题是什么?
答案 0 :(得分:0)
$stmt = $mysqli->prepare("UPDATE `song` SET `lyrtext`=? WHERE `id`=?");
$stmt->bind_param("si", $datad, $id);
$status = $stmt->execute();
错了。修改为:
$stmt = $mysqli->prepare("UPDATE `song` SET `lyrtext`=? WHERE `id`=?");
$stmt->bind_param("si", $datad, $id);
$stmt->execute();
如果您正在寻找成功执行查询的确认,可以通过以下方式进行测试:
if($stmt->execute()){
//returned true - statement executed successfully
} else{
//returned false
}
此外,您应该将Ajax调用修改为类似于:
$.ajax({
url : "your/url/here.php"
type : "POST",
data : {superstr: 'pelle'},
dataType : "json",
success : function(data){
//do something with the response
},
error : function(jqXHR, textStatus, errorThrown){
//handle the error
}
});
通过添加dataType
json,您应该将.php文件的echo
语句修改为:
$response = Array();
array_push($response, $datad);
echo json_encode($response);
我确定您已经在线阅读了该文档,但可以找到Ajax here和准备好的语句here。
这一切都写着警告你的数据库连接是有效的...它似乎给出了你的问题的措辞,以及你之前尝试解决问题。