简体   繁体   English

jQuery .ajax查询无法在数据库中插入数据

[英]jQuery .ajax query to insert data inside a database is not working

I am building a website that uses jQuery/AJAX to send data to a php page, and from there insert it into a database. 我正在建立一个使用jQuery / AJAX将数据发送到php页面的网站,然后从那里将其插入数据库。 For some reason, the code isn't inserted and I get no response at all. 由于某种原因,没有插入代码,我也没有任何回应。

my javascript: 我的JavaScript:

    function insert_data(){
    var title = debate_title.value;
    var subtitle = debate_sub.value;
    var sides = debate_sides.value;

    $(function() {
        $.ajaxSetup({
            error: function(jqXHR, exception) {
                if (jqXHR.status === 0) {
                    window.location.replace('errors/noConnection.html');
                } else if (jqXHR.status == 404) {
                    window.location.replace('errors/noConnection.html');
                } else if (jqXHR.status == 500) {
                    window.location.replace('errors/noConnection.html');
                } else if (exception === 'parsererror') {
                    window.location.replace('errors/noConnection.html');
                } else if (exception === 'timeout') {
                    window.location.replace('errors/noConnection.html');
                } else if (exception === 'abort') {
                    window.location.replace('errors/noConnection.html');
                } else {
                    window.location.replace('errors/noConnection.html');
                }
            }
        });
    });

    $.ajax({
        type: "POST",
        url: "post_debate.php",
        data: { post_title: title, post_sub: subtitle, post_sides: sidesm, ajax: 1 },
        dataType: "json",
        timeout: 5000, // in milliseconds
        success: function(data) {
            if(data!==null){
                window.location.replace('show_debate.php?id=' + data);
            }else{
                window.location.replace('errors/noConnection.html');
            }
        }
    });
}

My PHP code (post_debate.php): 我的PHP代码(post_debate.php):

    <?php

    require('connect.php');

    $title = $_POST['post_title'];

    $subtitle = $_POST['post_sub'];

    $sides = $_POST['post_sides'];

    $ajax = $_POST['ajax'];

    $date = new DateTime();
    $timeStamp = $date->getTimeStamp();

    if($ajax==1){
        $query = mysql_query("INSERT INTO debates VALUES('','$title','$subtitle','$sides','0','0','$timeStamp')");
        $get_data = mysql_query("SELECT id FROM debates WHERE title='$title', subtitle='$subtitle', sides='$sides', timestamp='$timeStamp'");
        while($id=mysql_fetch_array($get_data)){
            $final_id = $id['id'];
        }
        exit($final_id);
    }else{
        die("404 SERVER ERROR");
    }

?>

Thanks! 谢谢!


EDIT - NOT SOLVED YET 编辑-尚未解决

My new PHP code: 我的新PHP代码:

<?php
header("content-type: application/json");

require('connect.php');

$title = $_POST['post_title'];

$subtitle = $_POST['post_sub'];

$sides = $_POST['post_sides'];

$ajax = $_POST['ajax'];

$date = new DateTime();
$timeStamp = $date->getTimeStamp();

if($ajax==1){
    $query = mysql_query("INSERT INTO debates VALUES('','$title','$subtitle','$sides','0','0','$timeStamp')");
    $get_data = mysql_query("SELECT id FROM debates WHERE title='$title', subtitle='$subtitle', sides='$sides', timestamp='$timeStamp'");
    while($id=mysql_fetch_array($get_data)){
        $final_id = $id['id'];
    }
    print (json_encode(array("Id"=>$final_id)));
}else{
    die("404 SERVER ERROR");
}

?> ?>

my new Javascript .ajax: 我的新Javascript .ajax:

    $.ajax({
    type: "POST",
    url: "post_debate.php",
    data: { post_title: title, post_sub: subtitle, post_sides: sides, ajax: 1 },
    dataType: "json",
    timeout: 5000, // in milliseconds
    success: function(data) {
        if(data!==null){
            window.location.replace('show_debate.php?id=' + data['Id']);
        }else{
            window.location.replace('errors/noConnection.html');
        }
    }
});

Your code is expecting JSON as a response... 您的代码期望JSON作为响应...

dataType: "json",

( Documentation Here ) 此处的文档

But you're returning a non-json value without an appropriate content-type header. 但是,您将返回一个没有适当内容类型标头的非json值。

Try changing your PHP script from 尝试从更改PHP脚本

   exit($final_id);

to (untested) 到(未经测试)

header("content-type: application/json");
print (json_encode(array(
        "Id"=>$final_id
    )));

Also, put a breakpoint on your success callback in your Javascript code (using Firebug or a similar tool) and examine what data contains. 另外,在您的Javascript代码(使用Firebug或类似工具)的success回调中设置一个断点,并检查包含的data It should now be an associative array so you can do 现在它应该是一个关联数组,因此您可以执行

window.location.replace('show_debate.php?id=' + data['Id']);

Improvement: 改善:

Instead of doing a SELECT to get the recently inserted Id, use mysql_insert_id() . 不用执行SELECT来获取最近插入的ID,而是使用mysql_insert_id() Something like this... 像这样

$query = mysql_query("INSERT INTO debates VALUES('','$title','$subtitle','$sides','0','0','$timeStamp')");
$final_id = mysql_insert_id();
print (json_encode(array("Id"=>$final_id)));

Also, an alternate way to test what your PHP is returning if you can't see the response in your development tool is to browse to the page directly (You'd have to change all your $_POST to $_REQUEST ) 另外,如果您在开发工具中看不到响应,则测试PHP返回的另一种方法是直接浏览至页面(您必须将所有$_POST更改为$_REQUEST

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM