PHP Condition to validate if found or not Search Script

Wednesday, November 4, 2015

I'm new to PHP and still learning it's syntax really how it works. I just completed a working script but I would like to add a condition to check if keyword entered is found vs not found. I know I'll need to use an IF ELSE but I don't know what to put inside (). I googled and saw examples where if(!result) solution to receive true or false return value was used but it doesn't seem to be applicable on what I'm trying to do.



I hope you can help. So I can improve my search script. Thanks in advance.
I'd appreciate the simplest condition/script possible.



<?php
include('databaseConnection.php');
?>

<?php

if(isset($_POST['userSearch']))
{
$keyword = $_POST['userSearch'];

$sql = " SELECT * FROM booklist WHERE (Title LIKE '%$keyword%' OR Author LIKE '%$keyword%' OR ISBN LIKE '%$keyword%' ) ";

$result = mysql_query($sql);

while($row=mysql_fetch_array($result))
{
$title = $row['Title'];
$author = $row['Author'];
$yearLevel = $row['YearLevel'];
$isbn = $row['ISBN'];

echo "<table border = 1>" ;

echo "<th>Title </th>" ;
echo "<th> Author </th>" ;
echo "<th> Year Level </th>" ;
echo "<th> ISBN </th>";

echo "<tr>" ;
echo "<td>" .$title."</td> " ;
echo "<td>" .$author."</td> " ;
echo "<td>".$yearLevel."</td> " ;
echo "<td>" .$isbn. "</td> " ;
echo "</tr>" ;

echo "</table>" ;
echo "<br />" ;
echo "<a href='./menu.php'> Back to Home </a>" ;
}

} // end of Main IF

?>

0 comments:

Post a Comment