SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Evangelist
    Join Date
    Mar 2011
    Posts
    423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Says in browser: Column count doesn't match value count at row 1

    I am using this code to enter in to a MySQL database:

    PHP Code:
        function add_content($p) {
            
    $name mysql_real_escape_string($p['name']);
            
    $price mysql_real_escape_string($p['price']);
        
        
        
        
    $sql "INSERT INTO products VALUES (null, '$name', '$price')";
        
    $res mysql_query($sql) or die(mysql_error()); 
    But I get this error when the page processes the data. And nothing appears in the MySQL database.

    It says in the browser:

    Column count doesn't match value count at row 1

    What does this mean?

  2. #2
    SitePoint Evangelist
    Join Date
    Mar 2011
    Posts
    423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Following from my first post I have just added "(id, name, price)" so the code now reads:

    $sql = "INSERT INTO products (id, name, price) VALUES (null, '$name', '$price')";
    $res = mysql_query($sql) or die(mysql_error());


    Why did the author who provided the code not include this reference to the corresponding fields in the database. Are these essential?

    It is now working but I would like to get the code done professionally.

    Matt.

  3. #3
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,806
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    If the id column is auto-increment then you dont need to include it in your field list.
    The way you have it now is pretty much how I would do it:
    PHP Code:
    "INSERT INTO products ( name, price) VALUES ( '$name', '$price')"
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....


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
  •