16

I'm creating a registration and I'm using just straight PHP not JavaScript to send my form to the MySQL database, so everything is working fine, no syntax error or anything but I fill out all my information and click 'Register' and it returns a message saying 'Column count doesn't match value count at row 1'.

I'm only 14 so this is pretty confusing for me does anyone have a solution?

This is my INSERT INTO code:

 $sql = mysql_query("INSERT INTO users(firstname, lastname, email, password, day, month, year, gender) 
 VALUES('$firstname','$lastname','$email','$db_password','$day','$month','$year')")  
 or die (mysql_error());
cypherabe
  • 2,562
  • 1
  • 20
  • 35
Will
  • 175
  • 1
  • 1
  • 7
  • Can you please post your PHP code? – ChrisM Aug 16 '12 at 14:16
  • 2
    Probably a result of an `INSERT` statement having more or fewer values inside the `VALUES()` than listed in the columns `()`. Post your code and we'll have a look. – Michael Berkowski Aug 16 '12 at 14:17
  • 1
    This is related to your sql more than your php – Sablefoste Aug 16 '12 at 14:17
  • 1
    It most likely means that the amount of values do not match up to the amount of columns (ie. `INSERT INTO tbl (val1) VALUES (1, 2)`). If you are not specifying which columns to insert into, you should do so. – Kermit Aug 16 '12 at 14:19
  • There are many duplicates for this question. Did you search on Stackoverflow before asking? – Jocelyn Aug 16 '12 at 14:29
  • possible duplicate of [Column count doesn't match value count at row 1](http://stackoverflow.com/questions/5931900/column-count-doesnt-match-value-count-at-row-1) – KingCrunch Aug 17 '12 at 14:17

4 Answers4

24

You are trying to insert 7 values into 8 columns - you are missing the insertion of gender.

The correct code would be:

$sql = mysql_query("INSERT INTO users(firstname, lastname, email, password, day, month, year, gender) 
VALUES('$firstname','$lastname','$email','$db_password','$day','$month','$year', '$gender')")  
or die (mysql_error());

By the way, if you are not already doing it, I highly recommend escaping the strings first, before passing them to the query like so:

$firstname=mysql_real_escape_string($firstname)

You should do this with all variables above. Here you can find more about the escape function.

Dennis Hackethal
  • 13,662
  • 12
  • 66
  • 115
1

With your code there, I see you forget to insert $gender.

When inserting data into a MySQL table, you will have to specify which data goes into which column. You do this by specifying the column names before the VALUES part:

INSERT INTO tblA (col1, col2) VALUES ('value1','value2');

If you omit that information, MySQL will expect all columns:

If your table is like this:

 CREATE TABLE tblA (
     col1 INT,
     col2 INT 
 );

You can insert information like this:

INSERT INTO tblA VALUES ('value1', 'value2');

If you omit column names and do no specify values for all columns, MySQL will give the "Column count doesn't match value count at row" error will occur, as MySQL doesn't know what to put in the missing columns. With the table structure as above,

INSERT INTO tblA VALUES ('value1');

will result in that error.

In non-strict mode, MySQL will insert default values for omitted column names.

Bart Friederichs
  • 33,050
  • 15
  • 95
  • 195
0

You have missed a value for gender column (the last one)

Mariusz Sakowski
  • 3,232
  • 14
  • 21
-1

You are completely missing the gender value:

$sql = mysql_query("INSERT INTO users(firstname, lastname, email, password, day, month, year, gender) 
VALUES('$firstname','$lastname','$email','$db_password','$day','$month','$year', 'gender goes here')") 
or die (mysql_error());
Jocelyn
  • 11,209
  • 10
  • 43
  • 60