[英]Auto increment wont update
just created a form that saves the filename into an SQL database. 刚刚创建了一个将文件名保存到SQL数据库的表单。
The thing is that when I ad the ID column(Auto increment) into the SQLDatabase the form stops to save my filename, but as soon I delete the ID row, it begins to save. 事实是,当我将ID列(自动递增)添加到SQLDatabase中时,表单停止保存我的文件名,但是一旦我删除ID行,它就会开始保存。
Dont know whats the problem. 不知道是什么问题。
this is some of my code 这是我的一些代码
<?php
if(isset($_FILES['file_name'])) {
$file_name = mysql_real_escape_string($_FILES['file_name']['name']);
//Writes the information to the database
mysql_query("INSERT INTO `files` VALUES ('$file_name')") ;
}
//Retrieves data from MySQL
$data = mysql_query("SELECT * FROM files") or die(mysql_error());
//Puts it into an array
?>
And my database has two columns file_id and file_name but as I just wrote, the file_id wont work =/ 而且我的数据库有两列file_id和file_name,但是正如我刚才所写的,file_id不能工作= /
Any ideas ? 有任何想法吗 ?
I think you need to change your SQL query to this: 我认为您需要将SQL查询更改为:
mysql_query("INSERT INTO `files` (`file_name`) VALUES ('$file_name')") ;
With more than one column, you need to name which column you want the $file_name
string to be saved in if you're not going to enumerate every column in the table. 如果有不止一列,则如果不打算枚举表中的每一列,则需要命名要保存
$file_name
字符串的列。 Then your auto-increment should work fine. 然后,您的自动增量应该可以正常工作。
In fact, the problem isn't with auto-increment, it's with your SQL syntax. 实际上,问题不在于自动增量,而在于您的SQL语法。 You should check your queries for errors by saving the return value from
mysql_query
and checking if it returned false
. 您应该通过保存
mysql_query
的返回值并检查其是否返回false
来检查查询中的错误。 If it did, call mysql_error
to determine the error message. 如果是这样,请调用
mysql_error
以确定错误消息。
Since ID
is auto_inc, you don't need to include it in your script anywhere. 由于
ID
为auto_inc,因此您无需将其包含在脚本中的任何位置。
I think the problem is because you don't specify in your query what fields to put the values in. 我认为问题是因为您没有在查询中指定将值放入哪些字段。
Change this: 更改此:
mysql_query("INSERT INTO `files` VALUES ('$file_name')") ;
To something like this 对于这样的事情
mysql_query("INSERT INTO `files` (`YOURFIELD`) VALUES ('$file_name')") ;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.