edit final :
$sql = "SELECT * FROM volunteers_database
WHERE ". $_GET['option'] ." like '". $_GET['search'] ."'
AND shelter_name IS NOT NULL AND shelter_address IS NOT NULL ";
this worked, SQL commands such as AND , IS NOT NULL appear to be string and not blue as SQL keywords SELECT , FORM WHERE but they are still working i was trying to over concatenate them so they appear blue as other SQL keywords .
So I am making a form where users can search for animal shelters using their city or pincode but i only want to display records where those details are NOT NULL ( by defualt they will be null ) I tried the query shown below but I know the syntax isn't right I also want to display user contact number where the shelter details are present currently, this code is redirecting me to the last else statement 'no shelter available'
edit : var dump :
string(126) "SELECT * FROM volunteers_database WHERE city ' like 'dehradun' 'AND shelter_name IS NOT NULL AND shelter_address IS NOT NULL "
error :
Fatal error: Uncaught mysqli_sql_exception: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' like 'dehradun' 'AND shelter_name IS NOT NULL AND shelter_address IS NOT NULL' at line 1 in C:\xampp\htdocs\College Project\shelter-search.php:21 Stack trace: #0 C:\xampp\htdocs\College Project\shelter-search.php(21): mysqli->query('SELECT * FROM v...') #1 {main} thrown in C:\xampp\htdocs\College Project\shelter-search.php on line 21
<form class="search--form" action="shelter-search.php" method="GET">
<div >
<h1 class="heading--lable">Find Shelters in Your CITY or Around Your Pincode </h1>
<lable class="lable">Search</lable>
<input class="input" type="text" name="search" placeholder="CITY/PINCODE" required="">
<label class="lable" for="">Search Using</label>
<select type="" name="option">
<option value="city">Search CITY</option>
<option value="pincode">Search PINCODE</option>
</select>
<input class="search-submit" type="submit" value="Find Volunteers" >
</div>
</form>
$sql = "SELECT * FROM volunteers_database WHERE ". $_GET['option'] ." ' like '". $_GET['search'] ."' '". "AND shelter_name IS NOT NULL AND shelter_address IS NOT NULL ";
<?php include_once( 'search-register-header.php' ); ?>
<div class="search_reasult">
<?php
if ( empty($_GET['search']) && isset($_GET['option']) ){
echo '<table id="" class="display">';
echo "<tbody>";
echo '<tr>';
echo '<div class="enter--city-pincode">Please Enter City/Pincode</div>';
echo '</tr>';
echo "</tbody>";
echo '</table>';
} else {
if( $conn->connect_error ){
die("connection failed: ". $conn-> connect_error);
}
$sql = "SELECT * FROM volunteers_database WHERE ". $_GET['option'] ." ' like '". $_GET['search'] ."' '". "AND shelter_name IS NOT NULL AND shelter_address IS NOT NULL ";
$result = $conn->query($sql);
echo '<h1 class="heading--lable"> Please be Respectful towards Shelter Management! </h1>';
echo '<table id="" class="display">';
echo "<thead>";
echo '<tr>';
echo '<td>Shelter Name</td>';
echo '<td>Contact Number</td>';
echo '<td>Shelter Address</td>';
echo '</tr>';
echo "</thead>";
echo "<tbody>";
echo '<tr>';
if(!empty($result) && $result->num_rows > 0){
while($row = $result->fetch_assoc() ) {
echo '<tr>';
echo '<td>'. $row["shelter_name"] .'</td>';
echo '<td>'. $row["contact_number"] .'</td>';
echo '<td>'. $row["shelter_address"] .'</td>';
echo '</tr>';
}
} else {
echo '<div class="enter--city-pincode">No Shelters Available</div>';
}
echo '</tr>';
echo "</tbody>";
echo '</table>';
}
?>
</div>
<?php include_once( 'footer.php' ); ?>