I'm trying to search a mysql table column for a number. However when I enter a number into the search form I get told that no query has been entered.

Here is my code. Database connection information left out on purpose.

<?php
  if(isset($_POST['submit'])){
  if(isset($_GET['go'])){
  if(preg_match("/^[  a-zA-Z]+/", $_POST['search'])){
  $search=$_POST['search'];
  
  $db=mysql_connect  ("", "",  "") or die ('I cannot connect to the database  because: ' . mysql_error());
 
  $mydb=mysql_select_db("");
  
  $sql="SELECT CompanyID, ZipCat FROM A WHERE ZipCat LIKE '%" . $search .  "%'";
  
  $result=mysql_query($sql);

echo "<table border='1'>  
<tr>
<th>Company ID</th>
<th>ZipCat</th>
</tr>";  

  while($row=mysql_fetch_array($result)){
       echo "<tr>";
  echo "<td>" . $row['CompanyID'] . "</td>";
  echo "<td>" . $row['ZipCat'] . "</td>";
  echo "</tr>";    
  }
  }
  else{
  echo  "<p>Please enter a search query</p>";
  }
  }
  }
?>

I can search for a name or a word. Probably an easy fix but I cannot figure it out for the life of me. Any help would be apreciated.

Recommended Answers

Change if(preg_match("/^[ a-zA-Z]+/", $_POST['search']) to if(preg_match("/^[ a-zA-Z0-9]+/", $_POST['search'])

Jump to Post

All 3 Replies

Change if(preg_match("/^[ a-zA-Z]+/", $_POST['search']) to if(preg_match("/^[ a-zA-Z0-9]+/", $_POST['search'])

commented: solved the problem +1

Solved. Thanks

Change if(preg_match("/^[ a-zA-Z]+/", $_POST['search']) to if(preg_match("/^[ a-zA-Z0-9]+/", $_POST['search'])

Please mark the thread as solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.