Link to home
Create AccountLog in
Avatar of Robert Granlund
Robert GranlundFlag for United States of America

asked on

Query Syntax

I'm getting the following error:  Syntax Error, Unexpected 'else'.  I can not find what I am doing wrong here:

	<?php
	$import="INSERT INTO products 
	(products_id, products_numeric_size, products_alpha_size, products_quantity_warehouse_b,  products_speed, products_warranty, products_model, products_price, manufacturers_id)
	 VALUES 
	 ('', '.mysql_real_escape_string($data2[1]).', '.mysql_real_escape_string($data2[2]).', '.mysql_real_escape_string($data2[17]).', '.mysql_real_escape_string($data2[5]).', '.mysql_real_escape_string($data2[8]).', '.mysql_real_escape_string($data2[0]).', '.mysql_real_escape_string($data2[18]).', '$name_B') 
	 WHERE products_model='$model'";
	?>	

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Robert Schutt
Robert Schutt
Flag of Netherlands image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Try with quotes like these:

<?php
    $import="INSERT INTO products 
    (products_id, products_numeric_size, products_alpha_size, products_quantity_warehouse_b,  products_speed, products_warranty, products_model, products_price, manufacturers_id)
     VALUES ('', '".mysql_real_escape_string($data2[1])."', '".mysql_real_escape_string($data2[2])."', '".mysql_real_escape_string($data2[17])."', '".mysql_real_escape_string($data2[5])."','".mysql_real_escape_string($data2[8])."', '".mysql_real_escape_string($data2[0])."', '".mysql_real_escape_string($data2[18])."', '".$name_B."') WHERE products_model='".$model."'";
    ?>

Open in new window

Ah, good one, but I would still say get rid of the WHERE.
If you're talking about a PHP parse error, then the word "unexpected" means you need to look upstream in the code to find something you can fix.  Often an open parentheses, or mismatched qoutes, things like that.
And I agree with robert_schutt -- a WHERE clause has no purpose in an INSERT statement and will probably make MySQL bark.

And since we're on the subject of MySQL, you may have noticed the large red warning boxes on the PHP man pages.  This article explains why, and what you need to do, right now, to prepare for the changes that are coming.
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_11177-PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html
Oh, ... this is very true - why the where in insert ?
A last note, then I will sign off on this question.  You may find that you can make your code neater (and therefore make your work easier) if you use HEREDOC notation for this sort of thing.  And you probably do not want or need a placeholder for an AUTO_INCREMENT key in the query string.  Just a thought...

<?php
error_reporting(E_ALL);

// QUERY SAFETY
$safe = array_map('mysql_real_escape_string', $data2);

// CONSTRUCT THE INSERT QUERY USING HEREDOC NOTATION
$import = <<<EOD
INSERT INTO products
( products_numeric_size
, products_alpha_size
, products_quantity_warehouse_b
, products_speed
, products_warranty
, products_model
, products_price
, manufacturers_id
)
VALUES
( '$data2[1]'
, '$data2[2]'
, '$data2[17]'
, '$data2[5]'
, '$data2[8]'
, '$data2[0]'
, '$data2[18]'
, '$name_B'
)
EOD;

// SHOW THE WORK PRODUCT
echo $import;

Open in new window

Good luck with your project, ~Ray