Dymond
Dymond

Reputation: 2277

Auto increment wont update

just created a form that saves the filename into an SQL database.

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.

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 =/

Any ideas ?

Upvotes: 2

Views: 174

Answers (2)

OptimusCrime
OptimusCrime

Reputation: 14863

Since ID is auto_inc, you don't need to include it in your script anywhere.

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')") ; 

Upvotes: 2

nickb
nickb

Reputation: 59699

I think you need to change your SQL query to this:

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. Then your auto-increment should work fine.

In fact, the problem isn't with auto-increment, it's with your SQL syntax. You should check your queries for errors by saving the return value from mysql_query and checking if it returned false. If it did, call mysql_error to determine the error message.

Upvotes: 4

Related Questions