ajax报告成功但数据库没有变化

时间:2015-03-08 11:11:45

标签: javascript php jquery mysql ajax

首先,谢谢你的阅读。这是我的代码

脚本/ complete_backorder.php

<?php

if(!isset($_GET['order_id'])) {
    exit();
} else {

    $db = new PDO("CONNECTION INFO");

    $order = $db->prepare("UPDATE `scs_order` SET `order_complete`= 1 WHERE `order_id` = :var");

    $order->bindValue( ':var',$_GET['order_id'] );

 if ( $order->execute() ) {
        echo "DONE";
    };
};

?>

JS / tabs.js

/*
[#]===============================================================================[#]
MODAL: "complete_backorder_Modal"
USAGE: modal to confirm whether or not user want to complete a backorder.
[#]===============================================================================[#]
*/

$(function(){

    $(" .remove_record ").click(function( event ){
        event.preventDefault();

        var rows = $(this).parent().parent().parent().parent().find("tr:last").index() + 1;
        var order = $(this).attr("href");

        var dataString = 'order_id='+order;

        $( '#complete_backorder_Modal' ).modal({
            keyboard: false,
            backdrop: 'static'
        });

        $( '#complete_backorder_Modal #modal-yes' ).click(function(e){
            $.ajax({
                type: "POST",
                url: "../scripts/complete_backorder.php",
                data: dataString,
                success:  function(data){
                alert("Settings has been updated successfully.");
                }
            });
        });


    });

});

所以我知道php代码正在运行,因为我一遍又一遍地手动测试它。但是当我点击&#34; .remove_record&#34;按下模态显示,然后单击模式中的“是”按钮,警告框显示它已成功,但是当我查看数据库时,没有任何更改。

任何想法?

1 个答案:

答案 0 :(得分:0)

你的SQL永远不会运行,因为没有$ _GET变量,你正在使用$ _POST但即使你这样做,你也没有正确地通过order_id。变化:

var postData = {'order_id ' : order}; // instead of  var dataString = 'order_id='+order;

$.ajax({
        type: "POST",
        url: "../scripts/complete_backorder.php",
        data: postData, // instead of dataString
        success:  function(data){
            alert("Settings has been updated successfully.");
        }
 });

在PHP更改中:

if(!isset($_POST['order_id'])) { // Insetad of $_GET

$order->bindValue( ':var',$_POST['order_id'] );