Page 1 of 1

MYSQL Insert Into - Error

Posted: Sat Aug 15, 2009 10:48 pm
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'
";

Re: MYSQL Insert Into - Error

Posted: Sat Aug 15, 2009 10:55 pm
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...
 

Re: MYSQL Insert Into - Error

Posted: Sun Aug 16, 2009 5:56 am
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.