buggy searching code

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
scifirocket
Forum Commoner
Posts: 31
Joined: Fri Aug 13, 2010 1:24 am

buggy searching code

Post 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>";
  }
  }
  }
?>
User avatar
arrielmabale
Forum Newbie
Posts: 15
Joined: Fri Aug 13, 2010 3:57 pm
Location: Dubai

Re: buggy searching code

Post 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.
scifirocket
Forum Commoner
Posts: 31
Joined: Fri Aug 13, 2010 1:24 am

Re: buggy searching code

Post by scifirocket »

i dont think it is null. what exactly do you mean?
Post Reply