Page 1 of 1

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

Posted: Wed May 02, 2007 11:45 am
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

Posted: Wed May 02, 2007 11:49 am
by jayshields
Is the customers_id field a string? If not, remove the single quotes surrounding it the variable in your query.

Posted: Wed May 02, 2007 11:52 am
by oskare100
Hello,
I've tried that now and it didn't work. Still the same error.

Best Regards

Posted: Wed May 02, 2007 11:53 am
by feyd
INSERT queries do not have WHERE clauses.

Posted: Wed May 02, 2007 12:06 pm
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.

Posted: Wed May 02, 2007 12:27 pm
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.

Posted: Wed May 02, 2007 2:37 pm
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.