...

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



Jon W
01-07-2008, 12:28 PM
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

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

kbluhm
01-07-2008, 12:38 PM
What are the field names in the `register` table?

Jon W
01-07-2008, 12:41 PM
They are id, ip, username, password, email, firstname, lastname

Jon W
01-07-2008, 12:50 PM
I'm going to sleep. If you have any ideas why this may be, then please let me know.

Thanks
Jon W

daemonkin
01-07-2008, 01:41 PM
Jon,

Is your register table id field auto incrementing?

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



"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.

oesxyl
01-07-2008, 01:54 PM
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:

http://www.codingforums.com/showthread.php?t=130457

best regards

johnnyb
01-07-2008, 03:06 PM
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.

kbluhm
01-07-2008, 07:21 PM
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.

Jon W
01-07-2008, 07:34 PM
Hrmm.. So do you guys mean do this?...



<?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()); ?>

johnnyb
01-08-2008, 01:59 AM
Yes.

Except - you forgot to put a value for the column ip in that query, add that & it should work.

daemonkin
01-08-2008, 10:30 AM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum