PHP MYSQL search engine help

Hi,

When I click the submit button on the form without entering any text all the data from my database appears.

How can I have it so when submit is clicked without any text entered (so an empty text box) NOTHING happens and NO data appears.

<html>
<body>
James' New Site
<form action="seaside.php" method="post">
<input type="text" name="search"><br>
<input type="submit">
</form>
<hr width="100%"></hr>
</body>
</html>
<?php
  $search = "%" . $_POST["search"] . "%";
 
  mysql_connect ("", "", "");
  mysql_select_db ("");
 
  $query = "SELECT name,msg FROM contact WHERE name LIKE '$search'";
  $result = mysql_query ($query);
  if ($result) {
    while ($row = mysql_fetch_array ($result)) {
      echo "<br>$row[0]</br>";
      echo $row[1];
    }
  }
?>

Thanks!

James

make sure search is not empty before performing a search:

<html>
<body>
James' New Site
<form action="seaside.php" method="post">
<input type="text" name="search"><br>
<input type="submit">
</form>
<hr width="100%"></hr>
</body>
</html>
<?php

  mysql_connect ("", "", "");
  mysql_select_db ("");

  $search = $_POST['search'];
  $search = strip_tags($search);
  $search = mysql_real_escape_string($search);
  $search = "%" . $search . "%";
  
  //No keywords entered.
 if ($search == "")
 {
 	echo "<h1>Opps! You forgot to enter a search word</h1>\n";
 } else {
 
	  $query = "SELECT name,msg FROM contact WHERE name LIKE '$search'";
	  $result = mysql_query ($query);
	  if ($result) {
		while ($row = mysql_fetch_array ($result)) {
		  echo "<br>$row[0]</br>";
		  echo $row[1];
		}
	  }
  }
?>
Sponsor our Newsletter | Privacy Policy | Terms of Service