buggy searching code
Posted: Sat Aug 14, 2010 1:15 am
This block of code that i am using to search a database is very buggy. Sometimes it returns results and other times it doesn't. How can I make it better?
Code: Select all
<?php
if(isset($_POST['submit'])){
if(isset($_GET['go'])){
if(preg_match("/[A-Z | a-z]+/", $_POST['RName'])){
$RName=$_POST['RName'];
//connect to the database
$db=mysql_connect ("something", "something", "something") or die ('I cannot connect to the database because: ' . mysql_error());
//-select the database to use
$mydb=mysql_select_db("something");
//-query the database table
$sql="SELECT ID, name FROM Foodlist WHERE name LIKE '%" . $RName . "%'";
//-run the query against the mysql query function
$result=mysql_query($sql);
//-create while loop and loop through result set
while($row=mysql_fetch_array($result)){
$name =$row['name'];
$ID=$row['ID'];
//-display the result of the array
echo "<ul>\n";
echo "<li>" . "<a href=\"result.php?id=$ID\">" .$name . "</a></li>\n";
echo "</ul>";
}
}
else{
echo "<p>Please enter a search query</p>";
}
}
}
?>