简体   繁体   English

添加auto_increment id列后,无法使用php和javascript将数据添加到mysql数据库

[英]Unable to add data to mysql database using php & javascript after adding an auto_increment id column

I am creating a simple form using php, mysql and javascript. 我正在使用php,mysql和javascript创建一个简单的表单。 The idea is to have the user input the text, then the text is saved to the database, at the same time the latest text that the user have input returns from the database and shows on the same page without refreshing the page. 想法是让用户输入文本,然后将文本保存到数据库中,与此同时,用户输入的最新文本将从数据库返回并显示在同一页面上,而无需刷新页面。 The submitting function worked before I manually added the id column to the table. 在我将id列手动添加到表之前,提交功能起作用。 Please take a look of my codes and help me fix it, thank you. 请看一下我的代码并帮助我修复它,谢谢。

submit.php ↓ 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 ↓ 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 ↓ 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 ↓ 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>

Should your query not look something like this 您的查询应该不是这样吗

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

Also maybe try running your query in the mySQL SQL window to make sure your query actually works 也可以尝试在mySQL SQL窗口中运行查询,以确保查询确实有效

检查此查询

mysql_query("INSERT INTO $table (lastname,id) VALUES('$lastname',NULL)");
mysql_query('INSERT INTO `emilieinfo_lastname`(`lastname`) VALUES('{$lastname}'));

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

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

相关问题 如何将PHP代码添加到我的倒数javascript代码中,以便在倒数完成后自动将数据添加到MySQL列? - How add PHP code to my countdown javascript code for add data to MySQL column automatically after countdown finished? JavaScript中的自动递增名称和ID - Auto increment name and ID in javascript 如果MySQL中存在ID,则自动在模态上增加ID - Auto increment id on modal if the id exist in MySQL 在 javascript 中重新创建自动增量 php - recreate auto increment php in javascript 通过PHP进行JavaScript验证后,将表单数据发送到mySQL数据库 - Send Form Data to mySQL Database After JavaScript Validation via PHP 无法使用JavaScript将数据添加到Firebase实时数据库 - Unable to add data into firebase realtime database using javascript Json - 创建一个自动递增的 ID 列 - Json - create an ID column that auto increment 我正在尝试使用JavaScript在按钮的有序列表onclick中添加文本框,并在添加文本框时增加ID名称 - I'm trying to add textboxes within ordered list onclick of a button using JavaScript and also increment ID name while adding the textbox 如何在javascript中使用自动增量ID? - how to use auto increment id in javascript? 如何使用PHP在mysql数据库中动态添加html表数据? - How to add dynamically html table data in mysql database using PHP?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM