Creating a search form in PHP to search the database?

I am currently trying to complete a project in which specifications should use the search form to search through the packaging database. The database has many variables: from sizes, names, types and meat. I need to create a search form in which users can search for several different queries (for example, search for a cover tray of 50 cm length).

I've been trying all day to create some PHP code that could look up information in a test database that I created. I had a lot of errors: from mysql_fetch_array errors, logical errors, and now my last error is that my table does not seem to exist. Although I can enter data into it (html and php pages where I can enter data), I do not know what causes this, and I started again several times.

Can someone give me some idea or advice on what I need to do now? Here are just my little tests at the moment, before I go to the SQL database of real sites.

Database creation:

<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 that I use to try to collect information from the database (I have rewritten this several times, this code also displays "table.liam does not 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> 

If anyone knows or can help me, I would be very grateful! Thanks in advance.

+8
html sql database php mysql
source share
3 answers

try this let me know what will happen.

the 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: Removed it a bit 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> 
+8
source share

You get the error table "liam" doesn’t exist "because the name of the Liam table is not the same as Liam . MySQL table names are case sensitive.

+3
source share

Are you sure the specified database and table exist? Have you tried to look at your database using any database client? For example, the MySQL client command line is bundled with the MySQL server. Or, if you are a newbie developer, there are dozens of GUI and web interface clients (HeidiSQL, MySQL Workbench, phpMyAdmin and many others). So, first check if the script table was successfully created and what it needs was created.

By the way, do you have a script to create a database structure? This is usually a non-stop operation, so writing a script to do this is not required. This is only useful if you need to repeatedly create and manage the database structure on the fly.

0
source share

All Articles