[英]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}'));
只需删除id
和NULL
值,因为它是一个AUTO_INCREMENT
字段,您可以在INSERT
数据时将其删除。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.