Help! Error inserting record

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
funkd
Forum Newbie
Posts: 2
Joined: Wed Nov 12, 2008 10:59 am

Help! Error inserting record

Post 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
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Re: Help! Error inserting record

Post 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.
funkd
Forum Newbie
Posts: 2
Joined: Wed Nov 12, 2008 10:59 am

Re: Help! Error inserting record

Post 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
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Re: Help! Error inserting record

Post by infolock »

No problemo at all (=
Post Reply