Column count doesn't match value count at row 1

I am getting the error “Column count doesn’t match value count at row 1” and don’t know why. I know it sounds like my php script has too few or too many results for my database but I’m not sure. I have tripple checked everything so don’t know how to fix. Please help. in my database the first entries will go in which are f_name and l_name with $add_master but then the address section will not enter in the database. Below is my script and is lengthy so I will color the part that I believe to be the issue but unsure how to fix:

<?php if ($_POST[op] != "add") { $display_block = "

Add an Entry

First/Last Names:

Address

City/State/Zip:

Address Type
home work other

Telephone Number
home work other

Fax Number
home work other

Email Address
home work other

Personal Note:

"; } else if ($_POST[op] == "add") { if (($_POST[f_name] == "") || ($_POST[l_name] == "")) { header("Location: addentry.php"); exit; } $conn = mysql_connect("localhost", "xxxx", "xxxx") or die(mysql_error()); mysql_select_db("xxxxxxx",$conn) or die(mysql_error()); $add_master = "insert into master_name values ('', now(), now(), '$_POST[f_name]', '$_POST[l_name]')"; mysql_query($add_master) or die(mysql_error()); $master_id = mysql_insert_id(); if (($_POST[address]) || ($_POST[city]) || ($_POST[state]) || ($_POST[zipcode])) { $add_address = "insert into address values ('', $master_id, now(), now(), '$_POST[address]', '$_POST[city]', '$_POST[state]', '$_POST[zipcode]' '$_POST[add_type]')"; mysql_query($add_address) or die(mysql_error()); } if ($_POST[tel_number]) { $add_tel = "insert into master_telephone values ('', $master_id, now(), now(), '$_POST[tel_number]', '$_POST[tel_type]')"; mysql_query($add_tel) or die(mysql_error()); } if ($_POST[fax_number]) { $add_fax = "insert into fax values ('', $master_id, now(), now(), '$_POST[fax_number]', '$_POST[fax_type]')"; mysql_query($add_fax) or die(mysql_error()); } if ($_POST[email]) { $add_email = "insert into master_email values ('', $master_id, now(), now(), '$_POST[email]', '$_POST[email_type]')"; mysql_query($add_email) or die(mysql_error()); } if ($_POST[note]) { $add_note = "insert into personal_note values ('', $master_id, now(), now(), '$_POST[note]')"; mysql_query($add_note) or die(mysql_error()); } $display_block = "

Entry Added

Your entry has been added. Would you like to add another?

"; } ?> Add an Entry <? print $display_block; ?>

is there a column before $master_id ? because there is a comma before it in the INSERT statement. This could be making it think there is a column before master_id which you want to add no data to…

[php]
$master_id = mysql_insert_id();
if (($_POST[address]) || ($_POST[city]) || ($_POST[state]) ||
($_POST[zipcode])) {
$add_address = “insert into address values (’’, $master_id,
now(), now(), ‘$_POST[address]’, ‘$_POST[city]’,
‘$_POST[state]’, ‘$_POST[zipcode]’ ‘$_POST[add_type]’)”;
mysql_query($add_address) or die(mysql_error());
}
[/php]

also no comma between ‘$_POST[zipcode]’ and ‘$_POST[add_type]’
[php]
$add_address = “insert into address values (’’$master_id,
now(), now(), ‘$_POST[address]’, ‘$_POST[city]’,
‘$_POST[state]’, ‘$_POST[zipcode]’, ‘$_POST[add_type]’)”;
[/php]

are there 8 columns in the address table?

I found the missing comma and still having the same issue. I apologize I did not send my database setup. perhaps that will help. I appreciate both suggestions but I am still having same issue. Database:

This is the address table which has 9 fields:
id
master_id
date_added
date_modified
address
city
state
zipcode
type

I hope this helps. Please any additional help with this would be awesome and I would really appreciate it! Thanks!

I would suggest to use field names in the sql insert, because if you do not explicitly state field names, the order of fields in the table may also be a source of error.
[php]mysql_query(“insert into address (master_id,date_added) values ($master_id,now())”)[/php]

I tried as you said to insert the field names into the insert section. Still has same error. ugh. Any other ideas? I don’t think I’m trying to do anything very hard but it sure is becoming a pain… Thanks for helping!

Is field id - integer, auto increment? You are assigning it empty string in your insert query, which is not correct. Must be either numeric value, or skip this field at all (if it is auto increment).

yes, id field in integer, auto increment. So I need to skip it completely? It worked fine in the $master_name section above the address one I’m having the issue with. I will try it. Thank you!

So I made some changes to comply with the help that has been offered and I am still getting the same error. Here’s what I changed to (this is just the section we are having the issue with:

$add_master = “insert into master_name (id,date_added,date_modified,f_name,l_name) values (’’, now(), now(), ‘$_POST[f_name]’, ‘$_POST[l_name]’)”;
mysql_query($add_master) or die(mysql_error());

$master_id = mysql_insert_id();

if (($_POST[address]) || ($_POST[city]) || ($_POST[state]) ||
($_POST[zipcode])) {
$add_address = “insert into address (master_id,date_added,date_modified,address,city,state,zipcode,type) values ($master_id, now(), now(), ‘$_POST[address]’, ‘$_POST[city]’, ‘$_POST[state]’, ‘$_POST[zipcode]’, ‘$_POST[add_type]’)”;
mysql_query($add_address) or die(mysql_error());
}

Now, information will insert into the address table but it is not the correct info. the master_id is not pulling from the id of the master_name. It enters 0 and then all of my other columns are one off. So the address shows up in city and city goes into state colum. I don’t know how to get more specific with this. why is this so hard? Thanks again for helping. Am I doing something wrong getting the master_name’s id pulled and inserted into the master_id column in my address table?

WHOO HOO! I got it finally. Turns out the last set of changes did resolve my issue. thanks everyone for your help.

Sponsor our Newsletter | Privacy Policy | Terms of Service