Page 1 of 1

buggy searching code

Posted: Sat Aug 14, 2010 1:15 am
by scifirocket
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>";
  }
  }
  }
?>

Re: buggy searching code

Posted: Sat Aug 14, 2010 2:25 am
by arrielmabale
This code does not make errors by itself, it beacuse or you input,
validate $RName=$_POST['RName']; properly.
leaving it null changes the sql and gives off the bug.

Re: buggy searching code

Posted: Sun Aug 15, 2010 7:30 pm
by scifirocket
i dont think it is null. what exactly do you mean?