通过ajax调用从JSON数组插入数据库中的null值

时间:2013-02-06 19:30:19

标签: php jquery ajax json

我使用数组来存储值,然后使用ajax调用将数据发送到anther页面以将它们插入到数据库中: 这是带有ajax调用的发送页面:

$(function () {
    $(".sch_button").click(function () {
        var element = $(this);
        var cou = $("#course2").val();
        var place = $("#place").val();
        var tutor = $("#tutor").val();
        var cost = $("#cost").val();
        var date1 = $("#datetime").val();
        var date2 = $("#endtime").val();
        var notice = $("#notice").val();
        var data = [{
            data: cou
        }, {
            data: place
        }, {
            data: tutor
        }, {
            data: cost
        }, {
            data: date1
        }, {
            data: date2
        }, {
            data: notice
        }];
        if (cou == '.....' || place == '' || tutor == '' || cost == '' || date1 == '' || date2 == '' || notice == '') {
            alert("Please ..Enter the data");

        } else {
            $("#flash").show();
            $("#flash").fadeIn(400).html('<img src="http://tiggin.com/ajax-loader.gif" align="absmiddle">&nbsp;<span class="loading">Loading...</span>');


            $.ajax({
                type: "POST",
                url: "insert.php",
                data: {
                    data: JSON.stringify(data)
                },
                cache: false,
                success: function (response) {
                    if (response.error) return;
                    //fire off other ajax calls
                    $(document).ajaxStop(function () {
                        location.reload(true);
                    });
                }
            });
            alert("done");
        }
        return false;
    });
});

这是insert.php中的接收页面:

include("../config.php");

$query_language = mysql_query("SET NAMES 'utf8'");  
$data           = $_POST['data'];
$data2          = json_decode($data, true);
$course         = $data2[0]['data'];
$place          = $data2[1]['data'];
$tutor          = $data2[2]['data'];
$cost           = $data2[3]['data'];
$date1          = $data2[4]['data'];
$start_dates    = date("Y-m-d", strtotime($data2[4]['data']));
$date2          = $data2[5]['data'];
$end_dates      = date("Y-m-d", strtotime($data2[5]['data']));
$notice         = $data2[6]['data'];
$insert_new_shc = mysql_query(
  "insert into scheduling (place, tutor, cost, start_date, end_date, period, notice, course_id ) ".
  "values ('$place', '$tutor', '$cost', '$start_dates', '$end_dates', '$diff', '$notice', '$course')")
  or die($insert_new_shc."<br/><br/>".mysql_error()
);
firebug控制台中的

向我显示如下数据:

data[
    {
        "data": "127"
    },
    {
        "data": "1"
    },
    {
        "data": "2"
    },
    {
        "data": "3"
    },
    {
        "data": "4"
    },
    {
        "data": "5"
    },
    {
        "data": "6"
    }
]

但在数据库中设置空值...

1 个答案:

答案 0 :(得分:0)

php.ini文件的问题 但我用第二种方式而不是编辑php。 ini档案 所以我必须修改插入页面,添加striplash .... 修改这一行:

  $data2 = json_decode($data, true);

  $data2=json_decode(stripslashes($data), true);