I'm hoping for some help: I am trying to output a mysql data table that users can view and arrange by various select fields. I want the data to automatically arrange in date order (newest first) and limited to 10 results per page.
I have the following script excerpt (I have truncated it for brevity happy to post more if necessary)...
<!-- **** THIS CODE CREATES THE FORM ****-->
<form id="form1" name="form1" method="post" action="deals.php">
<!-- **** THIS CODE IS FOR THE "FROM" DATE ****-->
<label for="from">From</label>
<input name="from" type="text" id="from" size="10" value="<?php echo $_REQUEST["from"]; ?>" />
Results will only show offers that have been listed since this date. Leave blank for all offers.
<!-- **** THIS CODE IS FOR THE "TO" DATE ****-->
</p>
<label for="to">To</label>
<input name="to" type="text" id="to" size="10" value="<?php echo $_REQUEST["to"]; ?>"/>
Results will only show offers that run until this date. Leave blank for all offers.
<label>Country</label>
<select name="country">
<option value="">--Any--</option>
<?php
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." GROUP BY country ORDER BY country";
$sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql);
while ($row = mysql_fetch_assoc($sql_result)) {
echo "<option value='".$row["country"]."'".($row["country"]==$_REQUEST["country"] ? " selected" : "").">".$row["country"]."</option>";
}
?>
</select>
Only shows offers from the selected country.
// ***************************************** // ******* REPEATED FOR EACH VARIABLE (I SHOULD HAVE SET IT UP AS AN ARRAY REALLY ******************************* // *****************************************
if ($_REQUEST["from"]<>'' and $_REQUEST["to"]<>'')
{
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE from_date >= ' ORDER BY from_date DESC LIMIT 0, 10 ".mysql_real_escape_string($_REQUEST["from"])."' AND to_date <= '".mysql_real_escape_string($_REQUEST["to"])."'".$search_Seller.$search_country.$search_id.$search_Offer.$search_Item.$search_Description.$search_Brand.$search_Was.$search_Now;
}
else if ($_REQUEST["from"]<>'')
{$sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE from_date >= ' ORDER BY from_date DESC LIMIT 0, 10".mysql_real_escape_string($_REQUEST["from"])."'".$search_Seller.$search_country.$search_id.$search_Offer.$search_Item.$search_Description.$search_Brand;
}
else if ($_REQUEST["to"]<>'')
{$sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE to_date <= ' ORDER BY date_time DESC LIMIT 0, 10".mysql_real_escape_string($_REQUEST["to"])."'".$search_Seller.$search_country.$search_id.$search_Offer.$search_Item.$search_Description.$search_Brand.$search_Was.$search_Now;}
else {$sql = "SELECT * FROM ".$SETTINGS["data_table"]." ORDER BY from_date DESC LIMIT 0, 10 ".$search_Seller.$search_country.$search_id.$search_Offer.$search_Item.$search_Description.$search_Brand.$search_Was.$search_Now;
}
This works perfectly when outputting the initial table, however, once users input any data I get the error
"request "Could not execute SQL query" SELECT * FROM newoffers ORDER BY from_date DESC LIMIT 0, 10 AND country='UK'"
So I'm guessing there is a problem with the syntax? I have tried re-arranging the final part so it reads:
else {$sql = "SELECT * FROM ".$SETTINGS["data_table"]. .$search_Seller.$search_country.$search_id.$search_Offer.$search_Item.$search_Description.$search_Brand.$search_Was.$search_Now; "ORDER BY from_date DESC LIMIT 0, 10";
}
RESULT: Parse error: syntax error, unexpected '"ORDER BY from_date DESC LIMIT' (T_CONSTANT_ENCAPSED_STRING) in /home/iratebjj/public_html/dealstest.php on line 267
Please check out http://www.iratebjj.com/dealstest.php to see the script in action. If anyone has any suggestions I had really appreciate it!
Thanks!
Hope this post is ok. I think I've followed all the guidelines!
Here's your mistake
$search_Now; "ORDER BY from_date DESC LIMIT 0, 10";
You should concatenate "ORDER BY from_date DESC LIMIT 0, 10"
after $search_Now
and add a space
$search_Now." ORDER BY from_date DESC LIMIT 0, 10"
You also need to add WHERE 1 = 1
condition after $SETTINGS["data_table"]
since the following variables seem to begin with AND
: $search_Seller
, $search_country
, $search_id
, $search_Offer
, $search_Item
, $search_Description
, $search_Brand
, $search_Was
, $search_Now
.
The last else
block should be as follows
else {
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE 1 = 1 ";
$sql .= $search_Seller.$search_country.$search_id.$search_Offer.$search_Item.$search_Description.$search_Brand.$search_Was.$search_Now." ORDER BY from_date DESC LIMIT 0, 10";
}