我正在尝试通过 jQuery 和 AJAX 更新数据库条目。我正在检查我发送的值是否正确 - 但我不确定如何检查数据库未更新的原因。
我的代码如下:
$(document).on("click", ".approve", function(){
var classes = $(this).parents('div:eq(0)'); // this gets the parent classes.
i = 0;
var pros = [];
classes.find(".prosncons .pros ul li").each(function(){
pros.push($(this).text());
});
var cons = [];
classes.find(".prosncons .cons ul li").each(function(){
cons.push($(this).text());
});
var notes = classes.find(".notes").text();
var id = classes.find(".id").text();
$.ajax({
type: "POST",
url: "../scripts/upload.php",
data: { method: "approve", pros: pros, cons:cons, notes:notes, id:id },
success: $(this).closest(".approval").remove()
});
});
PHP::
require("connection.php");
$title = $_POST['title'];
$content = $_POST['content'];
$pros = $_POST['pros'];
$cons = $_POST['cons'];
$notes = $_POST['notes'];
$method = $_POST['method'];
$id = $_POST['id'];
if($method == "approve"){
$sql = "UPDATE `approval` SET approved = 1 WHERE pros=:pros AND cons=:cons AND notes=:notes AND id=:id";
$statement = $conn->prepare($sql);
$statement->execute(array(':pros' => $pros, ':cons' => $cons, ':notes' => $notes, ':id'=> $id));
echo $conn->error;
}
else{
$sql = "INSERT INTO `approval` VALUES(null, :title, :pros, :cons, :notes, 0)";
$statement = $conn->prepare($sql);
$statement->execute(array(':title' => $title, ':pros' => $pros, ':cons' => $cons, ':notes' => $notes));
}