Page 1 of 1

Help! Error inserting record

Posted: Wed Nov 12, 2008 11:06 am
by funkd
This is my first post to any forum, and I am relatively new to PHP & MYSQL. That said, here's the problem; I am trying to make an online photogallery and on the album create page I keep recieving the error Error inserting record: Column count doesn't match value count at row 1

I have searched online for an answer, and a missing comma seems to be the most likely conclusion

Here is the section of code where I believe the problem is:

Code: Select all

db_connect();    
      $sql = "INSERT INTO albums VALUES( '" . addslashes($_POST['album_name']) . "', '" . addslashes($_POST['album_desc']) . "', 0, '', '')";
      $result = @mysql_query($sql) or die("Error inserting record: " . mysql_error());
      if ($result){


Any help/ideas would be greatly appreciated

Thanks!
FD

Re: Help! Error inserting record

Posted: Wed Nov 12, 2008 11:10 am
by infolock
Hello, welcome to the forums.

Let's see if I can answer this good for you.

Whenever you try to execute an sql INSERT statement without defining the fields and are ONLY defining the values, that means that you are supplying to MySQL ALL VALUES for ALL fields. I don't think that is the case in this sql statement:

Code: Select all

 
$sql = "INSERT INTO albums VALUES( '" . addslashes($_POST['album_name']) . "', '" . addslashes($_POST['album_desc']) . "', 0, '', '')";
 
This sql statement suggests that there are only 5 fields. If you go into your table, check to make sure this matches.

I'm willing to bet that you are leaving out the auto increment field. If this is the case, you will need to change your sql statement to specify which fields are to be used with each values. Note that your fields MUST match the SAME order as your values...

example:

Code: Select all

 
$sql = "INSERT INTO albums (`album_name`, `album_desc`, `show_album`, `edited_by`, `date_ended`) VALUES( '" . addslashes($_POST['album_name']) . "', '" . addslashes($_POST['album_desc']) . "', 0, '', '')";
 
Of course, your fieldnames will be different, so substitute where needed. hope this helps.

Re: Help! Error inserting record

Posted: Wed Nov 12, 2008 11:36 am
by funkd
infolock

I did have the auto increment field set. However your advice worked like a charm. I had one to many value, like you said 5. and only 4 tables. I deleted one, and BAM new album created problem solved.

THANKS ALOT!

funkd

Re: Help! Error inserting record

Posted: Wed Nov 12, 2008 11:38 am
by infolock
No problemo at all (=