Jump to content

Column count doesn't match value count at row 1

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
7 replies to this topic

#1
shiyam198

shiyam198

    Newbie

  • Members
  • PipPip
  • 16 posts
Dear Guys,

When I am doing an "INSERT" in a PHP file, i am getting an error

"Column count doesn't match value count at row 1"

Normally it comes when the number of column names in a SQL query does not match the number of values we pass in that query.

I made sure it is the case, and i am still getting this error.

My Insert statement
--------------------
mysql_query("INSERT INTO lk_contact(fname,lname,title,company,worktel1,worktel2,worktel3,workfax,workadd,mobtel1,mobtel2,mobtel3,hometel1,hometel2,hometel3,homeadd,email,webpage,comments,bday,afname,alname,aemail,awrkno,lkgroup) VALUES('$_POST[fname]'),('$_POST[lname]'),('$_POST[title]'),('$_POST[company]'),('$_POST[worktel1]'),('$_POST[worktel2]'),('$_POST[worktel3]'),('$_POST[workfax]'),('$_POST[workadd]'),('$_POST[mobtel1]'),('$_POST[mobtel2]'),('$_POST[mobtel3]'),('$_POST[hometel1]'),('$_POST[hometel2]'),('$_POST[hometel3]'),('$_POST[homeadd]'),('$_POST[email]'),('$_POST[webpage]'),('$_POST[comments]'),('$_POST[bday]'),('$_POST[afname]'),('$_POST[alname]'),('$_POST[aemail]'),('$_POST[awrkno]'),('$_POST[lkgroup]')")
or die(mysql_error());
Just incase, you are intersested - THE TABLE
--------------------------------------------

CREATE TABLE lk_contact(
id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY(id),
fname VARCHAR(30),
lname VARCHAR(30),
title VARCHAR(50),
company VARCHAR(20),
worktel1 VARCHAR(20),
worktel2 VARCHAR(20),
worktel3 VARCHAR(20),
workfax VARCHAR(20),
workadd VARCHAR(20),
mobtel1 VARCHAR(20),
mobtel2 VARCHAR(20),
mobtel3 VARCHAR(20),
hometel1 VARCHAR(20),
hometel2 VARCHAR(20),
hometel3 VARCHAR(20),
homeadd VARCHAR(100),
email VARCHAR(40),
webpage VARCHAR(150),
comments VARCHAR(400),
bday VARCHAR(20),
afname VARCHAR(20),
alname VARCHAR(20),
aemail VARCHAR(20),
awrkno VARCHAR(20),
lkgroup VARCHAR(20));

Please, if you find any thing wrong here, please let me know.. 3 hours of trying different things. No luck.

Thanks for your Time.

Regards.
Shiyam

Edited by Jaan, 01 September 2008 - 09:10 PM.
Please use code tags when you're posting your codes!


#2
Guest_Jaan_*

Guest_Jaan_*
  • Guests
correct syntax is:

mysql_query("INSERT INTO table_name (column1, column2, column3) VALUES ('value1', 'value2', 'value3')");


#3
shiyam198

shiyam198

    Newbie

  • Members
  • PipPip
  • 16 posts
That is exactly I have in my code. I have attached it in my first original post. Do you find any thing wrong in that ?

#4
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts

shiyam198 said:

That is exactly I have in my code. I have attached it in my first original post. Do you find any thing wrong in that ?

('$_POST[fname]'),('$_POST[lname]')
is a little different than
('$_POST[fname]', '$_POST[lname]', ...)


#5
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
I counted your named columns and got those to 23, but then your values, I were counting to 24, so there are one value too much there...
and, you shall not have parenthesis around each value, as John mentions above.

and as you're entering all columns if i'm right, you don't have to specify them at all

table with three columns can be inserted like this:

insert into mytable values (1,2,3);

if you want to enter more than one row at a time, you can write

insert into mytable values (1,2,3), (4,5,6), (7,8,9);
for inserting three rows at once.

btw, your code seems a bit unsecure as you drop your $_POST values directly into the database, as there is easily sql injections possible there.
I know that someone here (John?) made an excellent tutorial on sql injections.

#6
shiyam198

shiyam198

    Newbie

  • Members
  • PipPip
  • 16 posts
Thanks a lot for your help guys. I am blind for parenthesis I guess.
I will read upon SQL injection as well.

Thanks again every one!

#7
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts

orjan said:

btw, your code seems a bit unsecure as you drop your $_POST values directly into the database, as there is easily sql injections possible there.
I know that someone here (John?) made an excellent tutorial on sql injections.

I'm surprised I didn't mention that. Here is my explanation of SQL injections.

#8
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
Hey, nice tutorial!
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums