INSERT INTO not inserting...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
me!
Forum Contributor
Posts: 133
Joined: Sat Nov 04, 2006 8:45 pm

INSERT INTO not inserting...

Post by me! »

Ran into a problem, every time I try to insert a new camper into our DB I am getting
mySQL Error: Column count doesn't match value count at row 1

I count 60 columns and values for the insert. Must I insert every column in the DB? I am going nuts with this, it all worked fine last year, all I added was the "times" and "lessons" to the insert to accommodate more options. :banghead:

What am I doing wrong? :dubious:

Code: Select all

mysql_query ("INSERT INTO  campers 
                                                (first_name, 
                                                last_name, 
                                                password, 
                                                email, 
                                                age, 
                                                dob_1, 
                                                dob_2, 
                                                dob_3, 
                                                s_address, 
                                                s_city, 
                                                s_state, 
                                                s_zip, 
                                                m_address, 
                                                m_city, 
                                                m_state, 
                                                m_zip,
                                                mom_name, 
                                                m_home_phone, 
                                                m_summer_phone, 
                                                m_work_phone, 
                                                m_cell_phone, 
                                                dad_name, 
                                                d_home_phone, 
                                                d_summer_phone, 
                                                d_work_phone, 
                                                d_cell_phone,
                                                staying_name, 
                                                staying_relation, 
                                                staying_address, 
                                                staying_city, 
                                                staying_state, 
                                                staying_zip, 
                                                staying_phone, 
                                                pickup_name1, 
                                                pickup_relation1,
                                                pickup_name2, 
                                                pickup_relation2, 
                                                pickup_name3, 
                                                pickup_relation3, 
                                                med_problem, 
                                                med_explanation, 
                                                meds, 
                                                allergies, 
                                                doctor, 
                                                doctor_phone, 
                                                dentist,
                                                dentist_phone, 
                                                insurance, 
                                                ins_id, 
                                                em_1_name, 
                                                em_1_relation, 
                                                em_1_phone, 
                                                em_2_name, 
                                                em_2_relation,
                                                em_2_phone,
                                                reg_date, 
                                                reg_time, 
                                                reg_year, 
                                                times, 
                                                lessons) 
                                        
                                            VALUES (
                                            '".pnVarPrepForStore($first_name)."', 
                                            '".pnVarPrepForStore($last_name)."', 
                                            '".createRandomPassword()."' 
                                            '".pnVarPrepForStore($email)."', 
                                            '".pnVarPrepForStore($age)."', 
                                            '".pnVarPrepForStore($dob_1)."', 
                                            '".pnVarPrepForStore($dob_2)."', 
                                            '".pnVarPrepForStore($dob_3)."', 
                                            '".pnVarPrepForStore($s_address)."', 
                                            '".pnVarPrepForStore($s_city)."', 
                                            '".pnVarPrepForStore($s_state)."', 
                                            '".pnVarPrepForStore($s_zip)."', 
                                            '".pnVarPrepForStore($m_address)."', 
                                            '".pnVarPrepForStore($m_city)."', 
                                            '".pnVarPrepForStore($m_state)."', 
                                            '".pnVarPrepForStore($m_zip)."',
                                            '".pnVarPrepForStore($mom_name)."', 
                                            '".pnVarPrepForStore($m_home_phone)."', 
                                            '".pnVarPrepForStore($m_summer_phone)."', 
                                            '".pnVarPrepForStore($m_work_phone)."', 
                                            '".pnVarPrepForStore($m_cell_phone)."', 
                                            '".pnVarPrepForStore($dad_name)."', 
                                            '".pnVarPrepForStore($d_home_phone)."', 
                                            '".pnVarPrepForStore($d_summer_phone)."', 
                                            '".pnVarPrepForStore($d_work_phone)."', 
                                            '".pnVarPrepForStore($d_cell_phone)."',
                                            '".pnVarPrepForStore($staying_name)."', 
                                            '".pnVarPrepForStore($staying_relation)."', 
                                            '".pnVarPrepForStore($staying_address)."', 
                                            '".pnVarPrepForStore($staying_city)."', 
                                            '".pnVarPrepForStore($staying_state)."', 
                                            '".pnVarPrepForStore($staying_zip)."', 
                                            '".pnVarPrepForStore($staying_phone)."', 
                                            '".pnVarPrepForStore($pickup_name1)."', 
                                            '".pnVarPrepForStore($pickup_relation1)."',
                                            '".pnVarPrepForStore($pickup_name2)."', 
                                            '".pnVarPrepForStore($pickup_relation2)."', 
                                            '".pnVarPrepForStore($pickup_name3)."', 
                                            '".pnVarPrepForStore($pickup_relation3)."', 
                                            '".pnVarPrepForStore($med_problem)."', 
                                            '".pnVarPrepForStore($med_explanation)."', 
                                            '".pnVarPrepForStore($meds)."', 
                                            '".pnVarPrepForStore($allergies)."', 
                                            '".pnVarPrepForStore($doctor)."', 
                                            '".pnVarPrepForStore($doctor_phone)."', 
                                            '".pnVarPrepForStore($dentist)."', 
                                            '".pnVarPrepForStore($dentist_phone)."', 
                                            '".pnVarPrepForStore($insurance)."',
                                            '".pnVarPrepForStore($ins_id)."', 
                                            '".pnVarPrepForStore($em_1_name)."',
                                            '".pnVarPrepForStore($em_1_relation)."', 
                                            '".pnVarPrepForStore($em_1_phone)."', 
                                            '".pnVarPrepForStore($em_2_name)."', 
                                            '".pnVarPrepForStore($em_2_relation)."', 
                                            '".pnVarPrepForStore($em_2_phone)."', 
                                            '$reg_date', 
                                            '$reg_time', 
                                            '$reg_year', 
                                            '".pnVarPrepForStore($time)."', 
                                            '".pnVarPrepForStore($lessons)."'   
                                             )")
                                            or die (mysql_error());
sparrrow
Forum Commoner
Posts: 81
Joined: Mon Oct 20, 2008 12:22 pm

Re: INSERT INTO not inserting...

Post by sparrrow »

What is pnVarPrepForStore(), and what exactly are you passing to it?

First thing though, echo the full query you are attempting to insert and take a look at it for anything out of the ordinary. If nothing stands out, please post that and some more code supporting the pnVarPrepForStore stuff.
me!
Forum Contributor
Posts: 133
Joined: Sat Nov 04, 2006 8:45 pm

Re: INSERT INTO not inserting...

Post by me! »

Code: Select all

pnVarPrepForStore()
It is a PostNuke function that preps for storage into a MySQL DB, why re invent the wheel it should be fine since I have it in use else ware with no problems.

echo the values...

Code: Select all

First Name = John,
Last Name = Public,
Password = mnwrbwah
e-mail =email@domain.com,
Age = 6,
dob1 = 03
dob2 = 03,
dob3 = 2003,
s_address = 123 Main Street,
s_city = Anytown,
s_state = AL,
s_zip = 01234,
m_address = 123 Main Street,
m_cityAnytown,
m_stateAL,
m_zip01234,
Mom = Mom,
Mom H phone = 111-222-1234,
Mom S Phone = ,
Mon W Phone = ,
Mom C Phone = ,
Dos = Dad,
Dad H Phone = 111-222-1234,
Dad S Phone = ,
Dad W Phone = ,
Dad C Phone = ,
Staying = ,
Relation = ,
Staying address = ,
,
AL,
,
,
,
,
,
,
,
,
No,
,
,
,
,
,
,
,
,
,
em1,
any,
111-222-1234,
person 2,
Some,
111-222-1234,
02-11-09,
1234399968,
Feb_2009,
1,
5
 
 Column count doesn't match value count at row 1
They all look as one would expect ( I tarted to put labels on them for the echo, but gave up half way down the list) some are bank since the users does not always fill in all form data and the ones that are present are fine.
sparrrow
Forum Commoner
Posts: 81
Joined: Mon Oct 20, 2008 12:22 pm

Re: INSERT INTO not inserting...

Post by sparrrow »

I'm not familiar with postnuke; I apologize.

After looking at your query again, one question comes to mind. Does your table have an auto-incrementing id field? You should specify it in the column list and insert an empty string or 0 into it.

Instead of echoing the values separately, what I meant was echo the whole insert query you are attempting to execute. So copy all the text you have inside mysql_query() and just add echo to the beginning. Whenever I have problems with a query, it is much easier for me to look at the plain text query with all literal values already printed into it (as opposed to text and variables). At this point, if I still don't see what the issue is, I will open phpMyAdmin and paste the query into the SQL worksheet and start fiddling with it to get it working. If you still need help, please post the query so we can take a look at it.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: INSERT INTO not inserting...

Post by Benjamin »

Here's what I would do, put your fields and values into an array and use the following code to write your database query:

Code: Select all

 
$data = array(
  'first_name' => $first_name,
  'last_name' => $last_name,
  // .....
);
 
mysql_query("INSERT INTO campers (`" . implode("`, `", array_map('mysql_real_escape_string', array_keys($data))) . "`) VALUES ('" . implode("', '", array_map('mysql_real_escape_string', $data)) . "')");
 
Post Reply