Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a HTML Form to query and produce results from Mysql datebase

I have created a form that has one text field and one select box, I also have created a button. Now what I want to happen is when the button is pressed I want the string in the forms to be matched up to my database and then I want the results of those matches to be returned and shown on the screen.

For example if I was to type 'History' into my text field box and I also selected 'level 2' from my select box when the button (Submit Button) was pressed I would want returned on the page everything that matches up with the word history and the selection of level 2 in my database.

I know that I have to connect my page with my Database through PHP and I have successfully done that, but what I don't understand is how to then get my HTML Form to Query the database and provide results back onto the screen

In terms of a example website that is very similar to the concept I would like to create take a look at this webpage. http://search.ucas.com/cgi-bin/hsrun/search/search/search.hjx;start=search.HsSearch.run?y=2013&w=H (UCAS Course Search) this website has multiple text fields and select boxes and a submit button exactly as I am trying to create and the results provided are only the results that match with what has been searched.

The code below works in terms of it links my text-field to my database but I can't get my text-field and my select box to link and query the database, only one or the other. I want them both to work from the same button (search button)

<form  method="post" action="Webpage here"  id="searchform">
  <input  type="text" name="name">
  <input  type="submit" name="submit" value="Search">
</form>
<form id="form1" name="ExamBoard" method="post" action="Webpage here">
  <label for="select"></label>
  <select name="ExamBoard" id="select">
    <option value="EB1" selected="selected">EB1</option>
    <option value="EB2">EB2</option>
    <option value="EB3">EB3</option>
  </select>
  <input  type="submit" name="submit" value="Search">
</form>
<p>&nbsp;</p>
<p>
<?php
 if(isset($_POST['submit'])){
 if(isset($_GET['go'])){
 if(preg_match("/^[  a-zA-Z]+/", $_POST['name'])){
 $name=$_POST['name'];
  //connect  to the database
 $db=mysql_connect  ("Name", "User",  "Password*") or die ('I cannot connect to the      database  because: ' . mysql_error());
 //-select  the database to use
 $mydb=mysql_select_db("Table Name");
  //-query  the database table
  $sql="SELECT  ID, CourseName, ExamBoard FROM subjects WHERE CourseName LIKE '%" . $name .  "%' ";
   //-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)){
      $CourseName  =$row['CourseName'];
      $ID=$row['ID'];
      $ExamBoard=$row['ExamBoard'];
  //-display the result of the array
  echo "<ul>\n";
  echo "<li>" . "<a  href=\"search.php?id=$ID\">"   .$CourseName . " " .  "</a></li>\n" ;
  echo  $ExamBoard . " " .  "</a>\n";
  echo "</ul>";
  }
  }
  }
  }
  ?>
like image 828
user2013520 Avatar asked Nov 12 '22 12:11

user2013520


1 Answers

New HTML File

newHTML.htm

<form  method="post" action="Webpage here"  id="searchform">
  <input  type="text" name="name">
  <input  type="submit" name="submit" value="Search">
</form

New PHP file

newPHP.pfp

<?php
 if(preg_match("/^[  a-zA-Z]+/", $_REQUEST['name'])){
 $name=$_REQUEST['name'];
  //connect  to the database
 $db=mysql_connect  ("Name", "User",  "Password*") or die ('I cannot connect to the      database  because: ' . mysql_error());
 //-select  the database to use
 $mydb=mysql_select_db("Table Name");
  //-query  the database table
  $sql="SELECT  ID, CourseName, ExamBoard FROM subjects WHERE CourseName LIKE '%" . $name .  "%' ";
   //-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)){
      $CourseName  =$row['CourseName'];
      $ID=$row['ID'];
      $ExamBoard=$row['ExamBoard'];
  //-display the result of the array
  echo "<ul>\n";
  echo "<li>" . "<a  href=\"search.php?id=$ID\">"   .$CourseName . " " .  "</a></li>\n" ;
  echo  $ExamBoard . " " .  "</a>\n";
  echo "</ul>";
}
?>
like image 142
user1551869 Avatar answered Nov 15 '22 06:11

user1551869