Closed Thread
Results 1 to 8 of 8

Thread: Column count doesn't match value count at row 1

  1. #1
    shiyam198 is offline Newbie
    Join Date
    Aug 2008
    Posts
    15
    Rep Power
    0

    Column count doesn't match value count at row 1

    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
    --------------------
    Code:
    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
    Last edited by Jaan; 09-01-2008 at 10:10 PM. Reason: Please use code tags when you're posting your codes!

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Jaan Guest

    Re: Column count doesn't match value count at row 1

    correct syntax is:

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

  4. #3
    shiyam198 is offline Newbie
    Join Date
    Aug 2008
    Posts
    15
    Rep Power
    0

    Re: Column count doesn't match value count at row 1

    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 ?

  5. #4
    Join Date
    Jul 2006
    Location
    Amherst, New York, United States
    Posts
    6,277
    Blog Entries
    26
    Rep Power
    20

    Re: Column count doesn't match value count at row 1

    Quote Originally Posted by shiyam198 View Post
    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 ?
    Code:
    ('$_POST[fname]'),('$_POST[lname]'
    is a little different than
    Code:
    ('$_POST[fname]''$_POST[lname]', ...) 

  6. #5
    Join Date
    Sep 2007
    Location
    Karlstad, Sweden
    Posts
    3,082
    Blog Entries
    7
    Rep Power
    42

    Re: Column count doesn't match value count at row 1

    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:

    Code:
    insert into mytable values (1,2,3);
    if you want to enter more than one row at a time, you can write

    Code:
    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.

  7. #6
    shiyam198 is offline Newbie
    Join Date
    Aug 2008
    Posts
    15
    Rep Power
    0

    Re: Column count doesn't match value count at row 1

    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!

  8. #7
    Join Date
    Jul 2006
    Location
    Amherst, New York, United States
    Posts
    6,277
    Blog Entries
    26
    Rep Power
    20

    Re: Column count doesn't match value count at row 1

    Quote Originally Posted by orjan View Post
    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.

  9. #8
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,174
    Blog Entries
    13
    Rep Power
    114

    Re: Column count doesn't match value count at row 1

    Hey, nice tutorial!

    Quote Originally Posted by Jordan View Post
    Good members, like yourself, stick around and post for ages to come!
    Mr. Xav | Blog | Forums

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 4
    Last Post: 09-29-2010, 09:26 AM
  2. Post Count
    By kegtappa in forum Technology Ramble
    Replies: 4
    Last Post: 01-25-2010, 03:45 PM
  3. Column count doesn't match value count at row 1
    By msebar in forum PHP Development
    Replies: 2
    Last Post: 12-29-2009, 07:42 AM
  4. When you count...
    By Jordan in forum The Lounge
    Replies: 39
    Last Post: 08-19-2008, 10:56 AM
  5. Getting Row Count
    By Lop in forum Managed C++
    Replies: 2
    Last Post: 01-10-2007, 06:17 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts