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
--------------------
Just incase, you are intersested - THE TABLECode: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());
--------------------------------------------
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
Last edited by Jaan; 09-01-2008 at 10:10 PM. Reason: Please use code tags when you're posting your codes!
correct syntax is:
Code:mysql_query("INSERT INTO table_name (column1, column2, column3) VALUES ('value1', 'value2', 'value3')");
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 ?
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:
if you want to enter more than one row at a time, you can writeCode:insert into mytable values (1,2,3);
for inserting three rows at once.Code:insert into mytable values (1,2,3), (4,5,6), (7,8,9);
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.
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!
I'm surprised I didn't mention that. Here is my explanation of SQL injections.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks