无法使用php& amp;将数据添加到mysql数据库添加auto_increment id列后的javascript

时间:2016-02-15 22:53:49

标签: javascript php jquery mysql ajax

我正在使用php,mysql和javascript创建一个简单的表单。想法是让用户输入文本,然后将文本保存到数据库,同时用户输入的最新文本从数据库返回并显示在同一页面上而不刷新页面。在我手动将id列添加到表之前,提交功能正常工作。请看一下我的代码并帮我解决,谢谢。

submit.php↓

<?php

$conn = mysql_connect('localhost','root','') or die (mysql_error);
$db = mysql_select_db('qinglish_nihaoemilie') or die (mysql_error);

$lastname = $_POST['lastname'];
$table = 'emilieinfo_lastname';

mysql_query("INSERT INTO $table VALUES('$lastname',NULL)");

?>

return.php↓

<?php

$conn = mysql_connect('localhost','root','');
$db = mysql_select_db('qinglish_nihaoemilie');

$res = mysql_query ("SELECT * FROM emilieinfo_lastname ORDER BY id DESC LIMIT 1");
$result = array();

while ($row = mysql_fetch_array($res)) {
    array_push($result, array('lastname' => $row[0]));
}

echo json_encode(array('result' => $result));
?>

Javascript↓

$('.lastname-container i').click(function(){
    var lastnameInput = $('.lastname').val();
    var lastnameInputLength = lastnameInput.length;
    if (lastnameInputLength > 0) {
        $('.lastname').hide();
        $(this).hide();
        var data = {
        lastname: $('.lastname').val(),
        };
        $.ajax({
            type: "POST",
            url: "../php/submitinfo-lastname.php",
            data: data,
        });

        $(this).parent().parent().find('p .fa-pencil-square-o').addClass('inline-table');
        $(this).parent().parent().find('p .fa-pencil-square-o').show();

        // $.getJSON("../php/returninfo-lastname.php", function(result){
        //  $.each(result, function(i, lastname){
        //      $(this).parent().parent().find('span').append(lastname + '');
        //  });
        // });

        // $.getJSON("../php/returninfo-lastname.php", function(returndata){
        //  $.each(returndata.result, function(){
        //      $(this).parent().parent().find('span').append("a"+this['lastname']);
        //  });
        // });
    };

    if (lastnameInputLength == 0) {
        $('.lastname').addClass('red-underline');
        $(this).addClass('red-color');
    };  
});

数据库截图↓

database screenshot

HTML↓

<h2>
    <p>last name: 
        <span></span> 
        <i style="position:static; display: none;padding-left:5px;" class="fa fa-pencil-square-o"></i>
    </p>
    <div class="lastname-container">
        <input class="lastname" name="lastname" type="text">
        <i class="fa fa-check-square-o"></i>
    </div>
</h2>

3 个答案:

答案 0 :(得分:0)

你的查询应该看起来不像这样吗

mysql_query('INSERT INTO '.$table.' VALUES("'.$lastname.'")');

也可以尝试在mySQL SQL窗口中运行您的查询,以确保您的查询实际工作

答案 1 :(得分:0)

检查此查询

$sxml1 = simplexml_load_string($xml1);
$sxml2 = simplexml_load_string($xml2);

$facilities = $sxml1->NewDataSet->HotelFacility;
$presentations = $sxml2->NewDataSet->HotelPresentation;

foreach ($facilities as $i => $facility) {
    echo '<div class="article">';
    echo '<div class="name">'. $facility->FacName .'</div>';
    echo '<div class="id">'. $presentations[$i]->HotelCode .'</div>';
    echo '</div>';
}

答案 2 :(得分:0)

mysql_query('INSERT INTO `emilieinfo_lastname`(`lastname`) VALUES('{$lastname}'));

只需删除idNULL值,因为它是AUTO_INCREMENT字段,您可以在INSERT数据时将其删除。