Hello and thank you for any help.
When I submit the form without keyword, it displays all the result from the table.
How could I fix this?
I need it to display the form again and an error message to enter a keyword or enter something.
My my code is messy, but if change anything, it displays nothing or just the first result from the table or just the search again form only.
[code=php]
<?php
/* if the "submit" variable does not exist, the form has not been submitted - display initial page */
if (!isset($_POST['submit']))
{
?>
<form name="submitForm" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" />
Search : <input type=text value="" name=searchstring size=25 maxlength=25>
<input type="submit" name="submit" value="Submit">
</form>
<?php
}
else
{
$searchstring="searchstring";
if(!empty($searchstring))
{
$hostname='localhost';
$username='root';
$database='cartonlots';
//establish database connection
$connect = mysql_connect($hostname,$username);
if ($connect==FALSE) {
print 'Unable to connect to database: '.mysql_error();
exit;
}
//select database
mysql_select_db($database);
$search = $_POST ['searchstring'];
$result = mysql_query ("SELECT * FROM descriptions
WHERE smdescription LIKE '%$search%'");
while ($row = mysql_fetch_array($result)) {
print '<tr><td>'.$row['image_id'].'</td><td>'.$row['smdescription'].'</td></tr>';
}
}
else{
echo 'You search for: '.$searchstring;
echo'<form name="submitForm" action="'.$_SERVER['PHP_SELF'].'" method="post" />
Search AGAIN: <input type="text" value="" name="searchstring" size="25" maxlength="25">
<input type="submit" name="submit" value="Submit">
</form>';
}
}
?>