MYSQL Insert Into - Error

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
Waterskiier23
Forum Newbie
Posts: 3
Joined: Mon Jul 27, 2009 10:34 pm

MYSQL Insert Into - Error

Post by Waterskiier23 »

I'm trying to use this insert code, but get an error saying "unknown column" error for anytime there is no value. For example, $service2 will sometimes be empty. Is there a way around this?

Thx in advance!

Code: Select all

$sql="INSERT INTO bookings SET
contact = '$contact',
address = '$address', 
phone = '$phone', 
city = '$city', 
date = '$date', 
start_time = '$start_time',
end_time = '$end_time',
info = '$info',
main_service = '$main_service',
service1 = '$service1',
service2 = '$service2',
service3 = '$service3',
service4 = '$service4',
email = '$email',
region = '$region',
refer = '$refer',
total = '$grand_total'
";
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: MYSQL Insert Into - Error

Post by aceconcepts »

An insert works like this:

Code: Select all

INSERT INTO table_name(fieldname1, fieldname2) VALUES('$var1', '$var2')
Before you set an values for your variables try initialising them e.g.

Code: Select all

 
//this should be done before any coding
$var1="";
$var2="";
$var3=array();
//etc...
 
webmonkey88
Forum Newbie
Posts: 20
Joined: Fri Aug 14, 2009 4:30 am

Re: MYSQL Insert Into - Error

Post by webmonkey88 »

you can do the insert the way you have done or the way suggested above i prefer doing it the way you have.

It will not matter if $service2 has any value or not, the unknown column error is mysql saying that there is a column in the insert that is not in the table

try echoing out the sql and running it in phpmyadmin it may give you more info.
Post Reply