dongyong2906 2013-09-12 21:18
浏览 72
已采纳

PHP和jQuery与MySQL

I am trying to update a database entry through jQuery and AJAX. I am checking that the values i send over is correct - but I am not sure how to check why the database is not updated.

My code is as follows:

$(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));
    }
  • 写回答

1条回答 默认 最新

  • dongxuan2577 2013-09-12 21:28
    关注

    You can not insert a array directly to mysql as mysql doesn't understand php data types.Mysql only understands SQL. So to insert this array into a mysql database you have to convert it to an sql statement.You canchange your pros and cons values to comma separated string or any any type of string.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记