2

我正在尝试通过 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));
    }
4

1 回答 1

3

您不能将数组直接插入 mysql,因为 mysql 不理解 php 数据类型。Mysql 只理解 SQL。因此,要将这个数组插入 mysql 数据库,您必须将其转换为 sql 语句。您可以将您的优点缺点值更改为逗号分隔的字符串或任何任何类型​​的字符串。

于 2013-09-12T21:28:40.447 回答