"You have an error in your SQL syntax" - But can't

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
oskare100
Forum Commoner
Posts: 80
Joined: Sun Oct 29, 2006 5:47 am

"You have an error in your SQL syntax" - But can't

Post by oskare100 »

Hello,
MySQL gives me this error: "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'where customers_id='14')' at line 1"

For this part of the script:

Code: Select all

getaddressid="SELECT * FROM address_book where customers_id='".$newcustomer['customers_id']."'";
$rowaddressid=mysql_query($getaddressid) or die( mysql_error() );
$addressid = mysql_fetch_assoc($rowaddressid);
Here is the surrounding code:

Code: Select all

$insertaddress="INSERT INTO address_book (customers_id, entry_company, entry_firstname, entry_lastname, entry_street_address, entry_postcode, entry_city, entry_state, entry_country_id, entry_zone_id) values ('".$newcustomer['customers_id']."', '".$row->business_name."', '".$row->first_name."', '".$row->last_name."', '".$row->address_street."', '".$row->address_zip."', '".$row->address_city."', '".$row->address_state."', '".$countryid['countries_id']."', '".$stateid['zone_id']."')";
$resultaddress=mysql_query($insertaddress) or die( mysql_error() );

$getaddressid="SELECT * FROM address_book where customers_id='".$newcustomer['customers_id']."'";
$rowaddressid=mysql_query($getaddressid) or die( mysql_error() );
$addressid = mysql_fetch_assoc($rowaddressid);

$insertaddressid="INSERT INTO customers (customers_default_address_id) values ('".$addressid['address_book_id']."') where customers_id='".$newcustomer['customers_id']."')";
$resultinsertaddressid=mysql_query($insertaddressid) or die( mysql_error() );
I can't find any error so please help me locate it - or tell me if there isn't one so I can start to look for other errors that might cause the MySQL error.

Best Regards
Oskar R
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post by jayshields »

Is the customers_id field a string? If not, remove the single quotes surrounding it the variable in your query.
oskare100
Forum Commoner
Posts: 80
Joined: Sun Oct 29, 2006 5:47 am

Post by oskare100 »

Hello,
I've tried that now and it didn't work. Still the same error.

Best Regards
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

INSERT queries do not have WHERE clauses.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

$insertaddressid="INSERT INTO customers (customers_default_address_id) values ('".$addressid['address_book_id']."') where customers_id='".$newcustomer['customers_id']."')";

Like feyd said, inserts do not have WHERE clauses. Remove everything I have bolded.
User avatar
neel_basu
Forum Contributor
Posts: 454
Joined: Wed Dec 06, 2006 9:33 am
Location: Picnic Garden, Kolkata, India

Post by neel_basu »

I think you need UPDATE
As feyd and Jcart told Insert Doesn't need any Where query
Insert adds a new Row. So no "Where" Query is required
To change an Existing Row you need UPDATE query.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Post by califdon »

Perhaps the lesson that should be learned from this is to make sure that the error is coming from the statement that you think it is. The original post was asking why there would be an error for the SELECT statement syntax, when the real problem was a different statement.
Post Reply