0

我使用数组来存储值,然后使用 ajax 调用将数据发送到另一个页面以将它们插入数据库:这是使用 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()
);

在萤火控制台显示我的数据,如:

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

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

4

1 回答 1

0

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

  $data2 = json_decode($data, true);

  $data2=json_decode(stripslashes($data), true);
于 2013-02-07T15:55:06.947 回答