简体   繁体   中英

PHP/MYSQLI: INNER JOIN search bar query issues

This is my first time creating a search bar. The search bar is located on the index/home page, it processes UK postal codes for a online food delivery service.The idea of the search bar is to see what restaurants deliver to the users address. The search bar successfully redirects the users to the menu selection page. However, this is where my issue begins.

Silly me, had already created a dynamic menu selection page, so the moment a new restaurant is created in the admin panel it automatically shows on this page. It works successfully apart from at the moment, it shows all the restaurants in the DB, no matter the area or postal code.

As this is the page the user is directed to on searching their postcode,I am now trying to combine my dynamic menu selection page with the search bar query, So for example if the user types in E14 ABU, all the restaurants in E14 will appear on this page. Sounds straight forwards, however because the restaurant details which is called in the restaurant menu page is in one table and the postcodes in another i have used a INNER JOIN to join the both of them. But the query doesn't work.

I know the query works as it was used to print out the dynamic menu selection page. My query is printing back my or die of "could not search!". I have used

 var_dump($sql) 

and

  if (!$sql) {
  die('Invalid query: ' . mysql_error());
  }

But nothing prints, which is confusing. Just a blank white page.

Code

   output='';



   //return to home page if not entered via search
   if(isset($_POST['search'])){
   $searchq= $_POST['search'];

             $sql=mysqli_query($dbc,"SELECT Rest_Details.Resturant_ID, Rest_Details.Resturant_name, Rest_Details.Res_Address_Line_1, Rest_Details.City_name, 
                 Rest_Details.Avg_Del,Delivery_Pcode.Pcode 
    FROM Rest_Details
    INNER JOIN Delivery_Pcode
    ON Delivery_Pcode.Restaurant_ID=Rest_Details.Restaurant_ID
    WHERE Delivery_Pcode.Pcode LIKE '%$searchq'") or die ("could not search!");

             }

Further down the page.

      print("$output"); 

I am only trying to print the restaurant name at the moment, until i can get the query working. My webpage is successfully connected to the DB, i have error handlers and the search bar name is search. Any suggestions? would be appreciated

If your field names is cerrect then try this :

$sql=mysqli_query($dbc,"SELECT Rest_Details.Resturant_ID, Rest_Details.Resturant_name, Rest_Details.Res_Address_Line_1, Rest_Details.City_name, Rest_Details.Avg_Del,Delivery_Pcode.Pcode 
     FROM Rest_Details INNER JOIN Delivery_Pcode
     ON Delivery_Pcode.Restaurant_ID=Rest_Details.Restaurant_ID
     WHERE Delivery_Pcode.Pcode LIKE '%".$searchq."'") or die ("could not search!");

I hope you will get your result.

Escape string then make query.

$searchq = mysqli_real_escape_string($dbc, $_POST['search']);     
$result = mysqli($dbc, "SELECT dpc.Pcode, rd.Resturant_ID, rd.Resturant_name, rd.Res_Address_Line_1, rd.City_name, rd.Avg_Del FROM Delivery_Pcode dpc INNER JOIN Rest_Details rd ON dpc.Restaurant_ID = rd.Restaurant_ID WHERE dpc.Pcode LIKE '%$searchq'");

$results = [];
while($row = mysqli_fetch_assoc($result)) {
   $results[] = $row;
}

// print results array
print_r($results);

You have to make sure the Delivery_Pcode.Pcode is a VARCHAR type

Read about Sanitizing User Input

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM