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

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>

If anyone has any insight or can help me with this I would be very grateful! Thanks in advance.

This question is related to php html mysql sql database

The answer is


Are you sure, that specified database and table exists? Did you try to look at your database using any database client? For example command-line MySQL client bundled with MySQL server. Or if you a developer newbie, there are dozens of a GUI and web interface clients (HeidiSQL, MySQL Workbench, phpMyAdmin and many more). So first check, if your table creation script was successful and had created what it have to.

BTW why do you have a script for creating the database structure? It's usualy a nonrecurring operation, so write the script to do this is unneeded. It's useful only in case of need of repeatedly creating and manipulating the database structure on the fly.


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.


Examples related to php

I am receiving warning in Facebook Application using PHP SDK Pass PDO prepared statement to variables Parse error: syntax error, unexpected [ Preg_match backtrack error Removing "http://" from a string How do I hide the PHP explode delimiter from submitted form results? Problems with installation of Google App Engine SDK for php in OS X Laravel 4 with Sentry 2 add user to a group on Registration php & mysql query not echoing in html with tags? How do I show a message in the foreach loop?

Examples related to html

Embed ruby within URL : Middleman Blog Please help me convert this script to a simple image slider Generating a list of pages (not posts) without the index file Why there is this "clear" class before footer? Is it possible to change the content HTML5 alert messages? Getting all files in directory with ajax DevTools failed to load SourceMap: Could not load content for chrome-extension How to set width of mat-table column in angular? How to open a link in new tab using angular? ERROR Error: Uncaught (in promise), Cannot match any routes. URL Segment

Examples related to mysql

Implement specialization in ER diagram How to post query parameters with Axios? PHP with MySQL 8.0+ error: The server requested authentication method unknown to the client Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver' phpMyAdmin - Error > Incorrect format parameter? Authentication plugin 'caching_sha2_password' is not supported How to resolve Unable to load authentication plugin 'caching_sha2_password' issue Connection Java-MySql : Public Key Retrieval is not allowed How to grant all privileges to root user in MySQL 8.0 MySQL 8.0 - Client does not support authentication protocol requested by server; consider upgrading MySQL client

Examples related to sql

Passing multiple values for same variable in stored procedure SQL permissions for roles Generic XSLT Search and Replace template Access And/Or exclusions Pyspark: Filter dataframe based on multiple conditions Subtracting 1 day from a timestamp date PYODBC--Data source name not found and no default driver specified select rows in sql with latest date for each ID repeated multiple times ALTER TABLE DROP COLUMN failed because one or more objects access this column Create Local SQL Server database

Examples related to database

Implement specialization in ER diagram phpMyAdmin - Error > Incorrect format parameter? Authentication plugin 'caching_sha2_password' cannot be loaded Room - Schema export directory is not provided to the annotation processor so we cannot export the schema SQL Query Where Date = Today Minus 7 Days MySQL Error: : 'Access denied for user 'root'@'localhost' SQL Server date format yyyymmdd How to create a foreign key in phpmyadmin WooCommerce: Finding the products in database TypeError: tuple indices must be integers, not str