在更改选择值时通过ajax更新DB值

时间:2014-03-23 07:09:40

标签: php jquery mysql ajax

我在尝试更改select时更新数据库中的值。但代码不起作用,无法找到原因。客户端和服务器端代码如下。

客户端 - js

$("select.changeStatus").change(function(){

  var project_status = $(this).children("option:selected").attr("value");
  var reference_number = $(this).parent("td").parent("tr").find("td.project_reference_number").text();

  if (project_status == "complete") {
  var confirmation_status = confirm("You really need to mark this project as complete?");
    if (confirmation_status == true) {
    $.ajax({
        type: "POST",
        url: "../../includes/changestatus.php",
        data: 'status=' + project_status + '&ref=' + reference_number,
        success: function() {
            $("body").fadeOut(250, function(){
            window.location.reload(true);
                });
        }
    });
    return false;
  }
  if (confirmation_status == false) {
     $(this).val("");
  }
}

服务器端 - php

$reference_number = $_POST["ref"];
$status = $_POST["status"];

$update_command = "UPDATE projects ";
$update_command = "SET status = '$status' ";
$update_command .= "WHERE id = '$reference_number'";
$update_projects_to_db = mysqli_query($connection, $update_command);
if (!$update_projects_to_db) {
    die ("Database query faild");
}

对此有何帮助?

1 个答案:

答案 0 :(得分:0)

只是一些语法错误。编辑了查询,它的工作原理。从数值中删除了引号:)