AdRock Posted September 30, 2007 Share Posted September 30, 2007 I am having serious trouble with my script I can connect to database using some search criteria and it displays the results. I have 1 result per page and use pagination which normally works fine but because I am getting a SQL error it won't work....i just get this error message 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 'AND AND bedrooms >= 2 AND location = '' AND type = '' AND `key I only get this error now becuase I set the variable of $min_bedrooms to 2. Before the message got up to near'' and stoped. I think it's becuase the $_POST variables aren't getting passed onto the next page. i have tried using global but it still doesn't work. <?php include "connection.php"; $min_price = $_POST['min_price']; $max_price = $_POST['max_price']; $property_type = $_POST['type']; $location = $_POST['location']; //$min_bedrooms = $_POST['minBeds']; $keywords = $_POST['keywords']; $min_bedrooms = 2; $webpage = basename($_SERVER['PHP_SELF']); function pagination_five($total_pages,$page){ global $webpage, $min_price, $max_price, $property_type; global $location, $min_bedrooms, $keywords; $max_links = 10; $h=1; if($page>$max_links){ $h=(($h+$page)-$max_links); } if($page>=1){ $max_links = $max_links+($page-1); } if($max_links>$total_pages){ $max_links=$total_pages+1; } if($total_pages>1){ echo '<div class="page_numbers"><ul>'; if($page>"1"){ echo '<li class="current"><a href="'.$webpage.'?page=1">First</a></li> <li class="current"><a href="'.$webpage.'?page='.($page-1).'">Prev</a></li> '; } if($total_pages!=1){ for ($i=$h;$i<$max_links;$i++){ if($i==$page){ echo '<li><a class="current">'.$i.'</a></li>'; } else{ echo '<li><a href="'.$webpage.'?page='.$i.'">'.$i.'</a> </li>'; } } } if(($page >="1")&&($page!=$total_pages)){ echo '<li class="current"><a href="'.$webpage.'?page='.($page+1).'">Next</a></li> <li class="current"><a href="'.$webpage.'?page='.$total_pages.'">Last</a></li> '; } echo '</ul></div>'; } } $result = mysql_query("Select count(*) from search WHERE bedrooms = $min_bedrooms") or die (mysql_error()); $numrows = mysql_fetch_row($result); if(isset($_GET['pagenum'])?$page = $_GET['pagenum']:$page = 1); $entries_per_page = 1; $total_pages = ceil($numrows[0]/$entries_per_page); $offset = (($page * $entries_per_page) - $entries_per_page); //after we have $total_pages and $page, we can include the //pagination style wherever we want on the page. //so far there is no output from the script, so we could have //pagination before or after the pages results //before the results $result = mysql_query("SELECT * FROM search WHERE (price BETWEEN $min_price AND $max_price AND bedrooms >= $min_bedrooms AND location = '$location' AND type = '$property_type' AND `keywords` LIKE '%$keywords%') OR (price BETWEEN $min_price AND $max_price AND bedrooms >= $min_bedrooms AND location = '$location' AND type = '$property_type') ORDER BY id ASC LIMIT $offset,$entries_per_page"); if(!$result) die(mysql_error()); $err = mysql_num_rows($result); if($err == 0) die("No matches met your criteria."); while($row=mysql_fetch_array($result)){ $formatted = number_format($row['price'],2); echo "<div id=\"right\"><img src=\"".$row['image']."\"><a href=\"detail.php\">".$row['title']."</a><h4>Offers over £".$formatted."</h4><p>".$row['type'].", ".$row['bedrooms']." bedrooms</p><br /></div>\n"; } //or after the results pagination_five($total_pages,$page); ?> Quote Link to comment https://forums.phpfreaks.com/topic/71294-mysql-errors-caused-by-variables-not-being-passed-global/ Share on other sites More sharing options...
shocker-z Posted September 30, 2007 Share Posted September 30, 2007 try this <?php include "connection.php"; $min_price = $_POST['min_price']; $max_price = $_POST['max_price']; if ($max_price == '') { echo 'Max price is empty!'; } $property_type = $_POST['type']; $location = $_POST['location']; //$min_bedrooms = $_POST['minBeds']; $keywords = $_POST['keywords']; $min_bedrooms = 2; $webpage = basename($_SERVER['PHP_SELF']); function pagination_five($total_pages,$page){ global $webpage, $min_price, $max_price, $property_type; global $location, $min_bedrooms, $keywords; $max_links = 10; $h=1; if($page>$max_links){ $h=(($h+$page)-$max_links); } if($page>=1){ $max_links = $max_links+($page-1); } if($max_links>$total_pages){ $max_links=$total_pages+1; } if($total_pages>1){ echo '<div class="page_numbers"><ul>'; if($page>"1"){ echo '<li class="current"><a href="'.$webpage.'?page=1">First</a></li> <li class="current"><a href="'.$webpage.'?page='.($page-1).'">Prev</a></li> '; } if($total_pages!=1){ for ($i=$h;$i<$max_links;$i++){ if($i==$page){ echo '<li><a class="current">'.$i.'</a></li>'; } else{ echo '<li><a href="'.$webpage.'?page='.$i.'">'.$i.'</a> </li>'; } } } if(($page >="1")&&($page!=$total_pages)){ echo '<li class="current"><a href="'.$webpage.'?page='.($page+1).'">Next</a></li> <li class="current"><a href="'.$webpage.'?page='.$total_pages.'">Last</a></li> '; } echo '</ul></div>'; } } $result = mysql_query("Select count(*) from search WHERE bedrooms = $min_bedrooms") or die (mysql_error()); $numrows = mysql_fetch_row($result); if(isset($_GET['pagenum'])?$page = $_GET['pagenum']:$page = 1); $entries_per_page = 1; $total_pages = ceil($numrows[0]/$entries_per_page); $offset = (($page * $entries_per_page) - $entries_per_page); //after we have $total_pages and $page, we can include the //pagination style wherever we want on the page. //so far there is no output from the script, so we could have //pagination before or after the pages results //before the results $result = mysql_query("SELECT * FROM search WHERE (price BETWEEN $min_price AND $max_price AND bedrooms >= $min_bedrooms AND location = '$location' AND type = '$property_type' AND `keywords` LIKE '%$keywords%') OR (price BETWEEN $min_price AND $max_price AND bedrooms >= $min_bedrooms AND location = '$location' AND type = '$property_type') ORDER BY id ASC LIMIT $offset,$entries_per_page"); if(!$result) die(mysql_error()); $err = mysql_num_rows($result); if($err == 0) die("No matches met your criteria."); while($row=mysql_fetch_array($result)){ $formatted = number_format($row['price'],2); echo "<div id=\"right\"><img src=\"".$row['image']."\"><a href=\"detail.php\">".$row['title']."</a><h4>Offers over £".$formatted."</h4><p>".$row['type'].", ".$row['bedrooms']." bedrooms</p><br /></div>\n"; } //or after the results pagination_five($total_pages,$page); ?> I've added if ($max_price == '') { echo 'Max price is empty!'; } as i have a feeling that max_price is being passed blank if so i would think you have a typo on the page submitting the info. Liam Quote Link to comment https://forums.phpfreaks.com/topic/71294-mysql-errors-caused-by-variables-not-being-passed-global/#findComment-358680 Share on other sites More sharing options...
AdRock Posted September 30, 2007 Author Share Posted September 30, 2007 I still ge the SQL error messgae and it also echoes that message you created about a blank field Quote Link to comment https://forums.phpfreaks.com/topic/71294-mysql-errors-caused-by-variables-not-being-passed-global/#findComment-358693 Share on other sites More sharing options...
shocker-z Posted September 30, 2007 Share Posted September 30, 2007 This means you have an error passing the variable could you post code for the page with the submit button and inpouts.. also.. change $result = mysql_query("SELECT * FROM search WHERE (price BETWEEN $min_price AND $max_price AND bedrooms >= $min_bedrooms AND location = '$location' AND type = '$property_type' AND `keywords` LIKE '%$keywords%') OR (price BETWEEN $min_price AND $max_price AND bedrooms >= $min_bedrooms AND location = '$location' AND type = '$property_type') ORDER BY id ASC LIMIT $offset,$entries_per_page"); to $result = mysql_query("SELECT * FROM search WHERE ((price BETWEEN $min_price AND $max_price) AND (bedrooms >= $min_bedrooms) AND (location = '$location') AND (type = '$property_type') AND (`keywords` LIKE '%$keywords%')) OR ((price BETWEEN $min_price AND $max_price) AND (bedrooms >= $min_bedrooms) AND (location = '$location') AND (type = '$property_type')) ORDER BY id ASC LIMIT $offset,$entries_per_page"); I beleive that should be correct.. for testing purposes try changing $max_price = $_POST['max_price']; to $max_price = 100000; just to make sure we are passing as value to test the query. Liam Quote Link to comment https://forums.phpfreaks.com/topic/71294-mysql-errors-caused-by-variables-not-being-passed-global/#findComment-358696 Share on other sites More sharing options...
AdRock Posted October 1, 2007 Author Share Posted October 1, 2007 It returns 2 links like it is supposed to but now when I try to get the next page, it says No matches met your criteria. Quote Link to comment https://forums.phpfreaks.com/topic/71294-mysql-errors-caused-by-variables-not-being-passed-global/#findComment-359439 Share on other sites More sharing options...
AdRock Posted October 1, 2007 Author Share Posted October 1, 2007 This is the form <html> <head> </head> <body> <form method="post" action="search.php"> <fieldset> <legend>Search</legend> <label for="min_price">price range</label><br /><input type="text" size="6" name="min_price" id="min_price" value="0"/> <label for="max_price">to</label><input type="text" size="6" name="max_price" id="max_price" value="1500000"/><br /> <label for="type">property type</label><br /> <select name="type" id="type" size="1"> <option value="Any">Any</option> <optgroup label="----"></optgroup> <option value="Flat">Flat</option> <option value="Bungalow">Bungalow</option> <option value="Cottage">Cottage</option> <option value="Semi-detached">Semi-det.</option> <option value="Detached">Detached</option> <option value="Terraced">Terraced</option> <option value="Conversion">Conversion</option> <option value="Mews">Mews</option> <option value="Townhouse">Townhouse</option> <option value="Plot">Plot</option> </select><br /> <label for="location">location</label><br /> <select name="location" id="location" style="width:152px"> <option value="Any">All Ayrshire</option> <option value="93">Ayrshire North</option> <option value="52">Ayrshire South</option> <option value="119">Ayrshire East</option> <option value="168">Inverclyde</option> </select><br /> <label for="minBeds">min bedrooms</label><br /> <select name="minBeds" id="minBeds" style="width:152px"> <option value="Any">Any</option> <optgroup label="----"></optgroup> <option value="0">Studio</option> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5 or more</option> </select><br /> <label for="keywords">keywords</label><br /><input type="text" size="20" name="keywords" id="keywords" /><br /> <input type="submit" name="Submit" value="search" style="width:152px"> </fieldset> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/71294-mysql-errors-caused-by-variables-not-being-passed-global/#findComment-359596 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.