0

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.

2
Contributors
3
Replies
4
Views
6 Years
Discussion Span
Last Post by smantscheff
1

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

Edited by smantscheff: n/a

Votes + Comments
solved the problem
0

Solved. Thanks

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.