Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a search form in PHP to search a database? [duplicate]

I am currently trying to complete a project where the specifications are to use a search form to search through a packaging database. The database has lots of variables ranging from Sizes, names, types and meats. I need to create a search form where users can search using a number of different searches (such as searching for a lid tray that is 50 cm long).

I have spent all day trying to create some PHP code that can search for info within a test database I created. I have had numerous amounts of errors ranging from mysql_fetch_array errors, boolean errors and now currently my latest error is that my table doesn't seem to exist. Although i can enter data into it (html and php pages where I can enter data), I don't know what is causing this and I have started again a few times now.

Can anyone give me some idea or tips of what I am going to have to do currently? Here is just my small tests at the moment before I move onto the actual sites SQL database.

Creation of database:

 <body>
  <?php
     $con = mysql_connect("localhost", "root", "");
      if (!$con)
     {
      die('Could not connect: ' . mysql_error());
     }
      if (mysql_query("CREATE DATABASE db_test", $con))
     {
  echo "Database created";
     }
      else
    {
  echo "Error creating database: " . mysql_error();
    }


  mysql_select_db("db_test", $con);
  $sql = "CREATE TABLE Liam
   ( 
  Code varchar (30),
  Description varchar (30),
  Category varchar (30),
  CutSize varchar (30),
   )";

 mysql_query($sql, $con);
     mysql_close($con);
 ?> 
   </body>

HTML search form page:

<body>

      <form action="form.php" method="post">
        Search: <input type="text" name="term" /><br />
      <input type="submit" name="submit" value="Submit" />
      </form>

</body>

The PHP code I am using to attempt to gather info from the database (I have rewritten this a few times, this code also displays the "table.liam doesn't exist")

  <body>
   <?php
 $con = mysql_connect ("localhost", "root", "");
 mysql_select_db ("db_test", $con);

  if (!$con)
    { 
    die ("Could not connect: " . mysql_error());
    } 
    $sql = mysql_query("SELECT * FROM Liam WHERE Description LIKE '%term%'") or die
        (mysql_error());

       while ($row = mysql_fetch_array($sql)){
    echo 'Primary key: ' .$row['PRIMARYKEY'];
    echo '<br /> Code: ' .$row['Code'];
    echo '<br /> Description: '.$row['Description'];
    echo '<br /> Category: '.$row['Category'];
    echo '<br /> Cut Size: '.$row['CutSize']; 
  }
 
  mysql_close($con)
   ?>
     </body>
like image 463
LiamHorizon Avatar asked Oct 05 '12 13:10

LiamHorizon


Video Answer


2 Answers

try this out let me know what happens.

Form:

<form action="form.php" method="post"> 
Search: <input type="text" name="term" /><br /> 
<input type="submit" value="Submit" /> 
</form> 

Form.php:

$term = mysql_real_escape_string($_REQUEST['term']);    

$sql = "SELECT * FROM liam WHERE Description LIKE '%".$term."%'";
$r_query = mysql_query($sql);

while ($row = mysql_fetch_array($r_query)){ 
echo 'Primary key: ' .$row['PRIMARYKEY']; 
echo '<br /> Code: ' .$row['Code']; 
echo '<br /> Description: '.$row['Description']; 
echo '<br /> Category: '.$row['Category']; 
echo '<br /> Cut Size: '.$row['CutSize'];  
} 

Edit: Cleaned it up a little more.

Final Cut (my test file):

<?php
$db_hostname = 'localhost';
$db_username = 'demo';
$db_password = 'demo';
$db_database = 'demo';

// Database Connection String
$con = mysql_connect($db_hostname,$db_username,$db_password);
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db($db_database, $con);
?>

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title></title>
    </head>
    <body>
<form action="" method="post">  
Search: <input type="text" name="term" /><br />  
<input type="submit" value="Submit" />  
</form>  
<?php
if (!empty($_REQUEST['term'])) {

$term = mysql_real_escape_string($_REQUEST['term']);     

$sql = "SELECT * FROM liam WHERE Description LIKE '%".$term."%'"; 
$r_query = mysql_query($sql); 

while ($row = mysql_fetch_array($r_query)){  
echo 'Primary key: ' .$row['PRIMARYKEY'];  
echo '<br /> Code: ' .$row['Code'];  
echo '<br /> Description: '.$row['Description'];  
echo '<br /> Category: '.$row['Category'];  
echo '<br /> Cut Size: '.$row['CutSize'];   
}  

}
?>
    </body>
</html>
like image 56
rackemup420 Avatar answered Oct 13 '22 08:10

rackemup420


You're getting errors 'table liam does not exist' because the table's name is Liam which is not the same as liam. MySQL table names are case sensitive.

like image 40
pjmorse Avatar answered Oct 13 '22 07:10

pjmorse