嗨,我有一个 php 文件,当我的 ajax/jquery 代码调用它时,它会在 mySQL 表中插入一行。但是我想要某种反馈来了解插入是否成功。这是我当前的代码:
阿贾克斯/jQuery:
$.ajax({
url: "update.php",
success: function(){
alert("success");
},
error:function(){
alert("failure");
}
});
PHP:
$conn = "";
try {
$conn = new PDO( "mysql:host=XXX;dbname=XXX", "XXX", "XXX");
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
} catch ( PDOException $e ) {
echo "Cannot connect to database, try again later";
}
$stmt = $conn->prepare("INSERT INTO data (price) VALUES (:price)");
$stmt->bindParam(":price", $price);
$stmt->execute();
$conn=null;