13

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>
Dharman
  • 30,962
  • 25
  • 85
  • 135
LiamHorizon
  • 171
  • 2
  • 3
  • 10
  • 3
    you are not catching the term in php use $term = $_POST["term"] – amitchhajer Oct 05 '12 at 13:51
  • Thanks, but I am still getting table.liam doesn't exist errors :( – LiamHorizon Oct 05 '12 at 13:57
  • 1
    Ouch. I'm sympathetic to your predicament here, but you're going to have a hard time getting good answers on Stack Overflow with this question as it stands. The reason is that it's not really one question; it's a whole bunch of questions. For good, useful help of the kind Stack Overflow is best at providing, you need to break your problem down into discrete pieces (e.g. "Why am I getting table doesn't exist errors?") and ask those questions. – pjmorse Oct 05 '12 at 14:05
  • Thanks very much for your advice. Sorry have only ever read or use to answer on stackflow, never actually asked a question. I will bare it in mind for the future. Thanks. – LiamHorizon Oct 05 '12 at 14:06
  • Typically, `$_POST` should be used when you intend to "write" data (INSERT/UPDATE/DELETE) on the server side. When you merely intend to "read" data (SELECT/search) on the server side, you should use `$_GET`. (of course, I mean that you should adjust the form `method` to suit this advice too) – mickmackusa Aug 17 '21 at 23:02

2 Answers2

15

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>
rackemup420
  • 1,600
  • 2
  • 15
  • 37
  • Thank you for your help! But I still get this when I submit the search, "Table 'db_test.liam' doesn't exist" :( – LiamHorizon Oct 05 '12 at 14:01
  • try my new edit. Remember DB, Tables, And Column names are case sensitive. – rackemup420 Oct 05 '12 at 14:04
  • i am getting this error now "Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /Applications/XAMPP/xamppfiles/htdocs/form.php on line 24" – LiamHorizon Oct 05 '12 at 14:04
  • 1
    This is an example of why this is a bad question: it's so hard to tell what a good answer is. @rackemup420 solved one problem, but that uncovered another one. It's a rat's nest. Break it down. – pjmorse Oct 05 '12 at 14:07
  • var_dump($sql); and see if $term is getting any info set. – rackemup420 Oct 05 '12 at 14:07
  • Okay so currently: We seemed to overcome the unfortunate mess of the table seeming not to exist. We have now moved on to there being a boolean problem with @rackemup420's code. – LiamHorizon Oct 05 '12 at 14:08
  • let me create the table real quick and run it. – rackemup420 Oct 05 '12 at 14:15
  • I can get passed all errors now, and I have checked PHPMyAdmin and the table is there and instead of entering data in a form I created, I have just manually put data in through PHPMyAdmin. But now when I search it just says NULL. – LiamHorizon Oct 05 '12 at 14:18
  • its a problem with your term getting set correctly. i ran it on my db and it printed results like so: `Primary key: 1 Code: 0 Description: asdasdasd bar asdasdasdad Category: 0 Cut Size: 0 ` and i searched for the term bar. – rackemup420 Oct 05 '12 at 14:19
  • How can I fix the term problem? – LiamHorizon Oct 05 '12 at 14:23
  • I will edit my answer to reflect what i currently have in my file. I just kept it all on the same page insted of traversing pages. – rackemup420 Oct 05 '12 at 14:25
  • I have managed to fix the term problem, but for me it displayed all of the results in the database instead of just the result that matches my search word. But thanks so much for your help so far! – LiamHorizon Oct 05 '12 at 14:26
  • dump your table structure and add it to your question might help out to see what your fields are set as. – rackemup420 Oct 05 '12 at 14:30
  • @rackemup420 I managed to get it working, thanks so much for your help throughout all of this dude. Very much appreciated. – LiamHorizon Oct 05 '12 at 14:34
  • yup no problem. that's what SO is for! i am still wondering why i got a downvote lol. my answer is the only complete one that actually stuck around to help out lol oh well. – rackemup420 Oct 05 '12 at 14:38
  • @rackemup420 If you are still an active user, you may like to revisit this answer to improve the coding standard for the benefit of researchers. Much of this answer is outdated. Also, this answer has no educational explanation and these are low-value contributions on Stack Overflow. – mickmackusa Aug 17 '21 at 22:42
3

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.

pjmorse
  • 9,204
  • 9
  • 54
  • 124