Jump to content

[SOLVED] Added a new field to a table now my SQL statement gives error


dmccabe

Recommended Posts

I had an SQL statement that was working fine, I added a new field to the table and made it the primary key, now my SQL statement shows 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 'SORT BY `REG` ASC' at line 1

 

Here is the code:

 

$make_id = $_POST['MAKE_ID'];
	$model_id = $_POST['MODEL_ID'];
	$colour_id = $_POST['COLOUR_ID'];
	$status_id = $_POST['STATUS_ID'];
	$location_id = $_POST['LOCATION_ID'];
	$fueltype_id = $_POST['FUELTYPE_ID'];
	$transmissiontype_id = $_POST['TRANSMISSIONTYPE_ID'];


	$where = "WHERE ";
	$where .= (isset($make_id) && !empty($make_id))?"`make_id` = '" . $make_id. "' AND ":'';
	$where .= (isset($colour_id) && !empty($colour_id))?"`colour_id` = '" . $colour_id. "' AND ":'';
	$where .= (isset($model_id) && !empty($model_id))?"`model_id` = '" . $model_id. "' AND ":'';
	$where .= (isset($status_id)  && !empty($status_id))?"`status_id` = '" . $status_id. "' AND ":'';
	$where .= (isset($location_id) && !empty($location_id))?"`location_id` = '" . $location_id. "' AND ":'';
	$where .= (isset($fueltype_id) && !empty($fueltype_id))?"`fueltype_id` = '" . $fueltype_id. "' AND ":'';
	$where .= (isset($transmissiontype_id) && !empty($transmissiontype_id))?"`transmissiontype_id` = '" . $transmissiontype_id. "' AND ":'';
	$where = substr($where, 0, -4);

	$query = "SELECT * FROM `tbl_vehicles` " . $where . " SORT BY `REG` ASC";
	echo "$query <br />";

 

When I echo out the value of $query I get:

 

SELECT * FROM `tbl_vehicles` WHERE `make_id` = '1' AND `model_id` = '3' SORT BY `REG` ASC

 

Everything looks right, but I dont know why I am getting the error?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.