Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    Regular Coder
    Join Date
    Jan 2008
    Posts
    334
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Database error: Column count doesn't match value count at row 1

    I can't seem to figure out why I'm getting this error. I don't really know what the error really means.. So if I could get some help on figuring this problem out, it would be great.

    PHP Script:

    PHP Code:
    <?php

    include("db.php");
    $ip $_POST['REMOTE_ADDR'];

      if(isset(
    $_POST['submit']))
      
      
    {
       
         
    $CheckUser mysql_query("SELECT username FROM register WHERE username='{$_POST['username']}'") or die('Database error: ' .mysql_error());
       
           
    $num_rows mysql_num_rows($CheckUser);
      
         if(
    $num_rows 0)
         {
         
    $userTaken 'This User name is being used.';
         include(
    "register.php");
         }
         elseif(empty(
    $_POST['username']))
         {
         
    $msg1 'User Name';
         
    $error 'Please fill in everything that has a red start at the end of it';
         include(
    "register.php");
         }
         elseif(empty(
    $_POST['password']))
         {
         
    $msg2 'Password';
         
    $error 'Please fill in everything that has a red start at the end of it';
         include(
    "register.php");
         }
         elseif(empty(
    $_POST['comfirmed_password']))
         {
         
    $msg3 'Retype Password';
         
    $error 'Please fill in everything that has a red start at the end of it';
         include(
    "register.php");
         }
         elseif(
    $_POST['password'] != $_POST['comfirmed_password'])
         {
         
    $pass_match 'Your passwords do not match';
         include(
    "register.php");
         }
         elseif(empty(
    $_POST['email']))
         {
         
    $msg4 'Email';
         
    $error 'Please fill in everything that has a red start at the end of it';
         include(
    "register.php");
         }
         else
         
         {
         
         
    $query mysql_query("INSERT INTO register VALUES('$ip', '".mysql_real_escape_string($_POST['username'])."', '".mysql_real_escape_string(md5($_POST['password']))."', '".mysql_real_escape_string($_POST['email'])."', '".mysql_real_escape_string($_POST['firstname'])."', '".mysql_real_escape_string($_POST['lastname'])."')") or die('Database error: ' .mysql_error());
         }

    }      
      
      
    ?>
    Thanks
    Jon W

  • #2
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    What are the field names in the `register` table?

  • #3
    Regular Coder
    Join Date
    Jan 2008
    Posts
    334
    Thanks
    9
    Thanked 0 Times in 0 Posts
    They are id, ip, username, password, email, firstname, lastname

  • #4
    Regular Coder
    Join Date
    Jan 2008
    Posts
    334
    Thanks
    9
    Thanked 0 Times in 0 Posts
    I'm going to sleep. If you have any ideas why this may be, then please let me know.

    Thanks
    Jon W

  • #5
    Regular Coder
    Join Date
    Jun 2007
    Location
    N. Ireland
    Posts
    351
    Thanks
    16
    Thanked 4 Times in 4 Posts
    Jon,

    Is your register table id field auto incrementing?

    I would specify exactly what fields I am going to enter when I insert:

    PHP Code:
    "INSERT INTO register (field1,field2,field3...fieldX) VALUES (value1,value2,value3...valueX)" 
    That way you will not be inserting incorrectly or missing fields or values out when inserting.

    Hope this helps.
    D.
    Daemonkin.
    If this was helpful, please add to my reputation
    Thousand Sons - Freelance Web Developer - ninetyonedegrees.com

  • #6
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by Jon W View Post
    I can't seem to figure out why I'm getting this error. I don't really know what the error really means.. So if I could get some help on figuring this problem out, it would be great.
    maybe I'm wrong but read this thread:

    Error in column count?

    best regards

  • #7
    Regular Coder
    Join Date
    Apr 2003
    Location
    Montreal, QC
    Posts
    340
    Thanks
    3
    Thanked 2 Times in 2 Posts
    When you don't list the columns like in daemonkin's example MySQL expects a value for every column. So, in your case it expects 7 values, however, you're only providing 6, (I assume that id is an auto-increment that you want to have automatically filled). So, the Column Count, (assumed 7 since you don't give columns), doesn't match the value count, (6).

    By listing the columns, like daemonkin suggests, you'll avoid this problem and get rid of the error.
    Search for Laughter or Just Search?
    GiggleSearch.org
    Blog: www.johnbeales.com
    All About Ballet: www.the-ballet.com

  • #8
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    You can also specify NULL or '' (empty quotes) for the auto-incrementing column to have it automatically assigned, but it is good practice to explicitly specify the column names, at least for readability sake.

  • #9
    Regular Coder
    Join Date
    Jan 2008
    Posts
    334
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Hrmm.. So do you guys mean do this?...

    PHP Code:
    <?php $query mysql_query("INSERT INTO register (`ip`, `username`, `password`, `email`, `firstname`, `lastname`) VALUES('".mysql_real_escape_string($_POST['username'])."', '".mysql_real_escape_string(md5($_POST['password']))."', '".mysql_real_escape_string($_POST['email'])."', '".mysql_real_escape_string($_POST['firstname'])."', '".mysql_real_escape_string($_POST['lastname'])."')") or die('Database error: ' .mysql_error()); ?>

  • #10
    Regular Coder
    Join Date
    Apr 2003
    Location
    Montreal, QC
    Posts
    340
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Yes.

    Except - you forgot to put a value for the column ip in that query, add that & it should work.
    Search for Laughter or Just Search?
    GiggleSearch.org
    Blog: www.johnbeales.com
    All About Ballet: www.the-ballet.com

  • #11
    Regular Coder
    Join Date
    Jun 2007
    Location
    N. Ireland
    Posts
    351
    Thanks
    16
    Thanked 4 Times in 4 Posts
    For insertions into table with a large number of fields I sometimes take a new line for each field and value and then make sure the fields and values match. This helps when you are just starting out to get you into the habit of explicitly naming field and value pairs.

    Doing this also will not break your code if you add in extra fields at a later date but forget to change the insertion code. By stating what values you are inserting means that the sql will not break.

    D.
    Daemonkin.
    If this was helpful, please add to my reputation
    Thousand Sons - Freelance Web Developer - ninetyonedegrees.com


  •  

    Posting Permissions

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