PHP/MySQL Tutorial Pt. 3 - Forms and Results Pages
Solution In our previous tutorials, we ran through the steps of creating a MySQL database through your Control Panel and also how to create and edit tables, insert, back up and view data using phpMyAdmin through a simple exercise.

In this tutorial, we'll provide you with the code to create a simple HTML based search form and a MySQL database results page, created using PHP to display information from the database we created.

Results page

Copy the code in the box below into a new page. You can work on the formatting after the exercise is finished and you've tested to see everything is working. Ensure the code goes between your <body> and </body> tags.

You'll also need to provide the username and password you created for the database where indicated in the code. The hostname, database name and table name are already inserted based on what we used in our phpMyAdmin exercise. If you named the database and table something different, then you'll need to adjust these as well. The hostname should not be changed.

Please note: PHP code blocks always begin with <? and end with ?>. PHP code can be inserted into HTML, much like JavaScript can be inserted, but PHP code will only work if the file extension is correct, e.g. .php, and it is published to a server that is PHP enabled as our servers are. FrontPage users should copy the code block into NotePad first and then copy and paste it into the page source code - this prevents erroneous characters being inserted.

The PHP lines in this sample code must not be broken using any kind of line break. You'll also notice that each line of PHP coding ends with a ';' and that comment lines in php are preceded by '//'.

<center>
<table border="1" cellpadding="5" cellspacing="0" bordercolor="#000000">
<tr>
<td width="60"><b>id</b></td>
<td width="100"><b>name</b></td>
<td width="70"><b>telephone</b></td>
<td width="150"><b>birthday</b></td>
</tr>
<tr>
<td>
<? $hostname = "localhost"; // Our DB server.
$username = "username"; // The username you created for this database.
$password = "password"; // The password you created for the username.
$usertable = "details"; // The name of the table you made.
$dbName = "example"; // This is the name of the database you made.

MYSQL_CONNECT($hostname, $username, $password) OR DIE("DB connection unavailable");
@mysql_select_db( "$dbName") or die( "Unable to select database");
?>
<?
//error message (not found message)begins
$XX = "No Record Found, to search again please close this window";
//query details table begins
$query = mysql_query("SELECT * FROM details WHERE $metode LIKE '%$search%' LIMIT 0, 50");
while ($row = @mysql_fetch_array($query))
{
$variable1=$row["id"];
$variable2=$row["name"];
$variable3=$row["telephone"];
$variable4=$row["birthday"];
//table layout for results

print ("<tr>");
print ("<td>$variable1</td>");
print ("<td>$variable2</td>");
print ("<td>$variable3</td>");
print ("<td>$variable4</td>");
print ("</tr>");
}
//below this is the function for no record!!
if (!$variable1)
{
print ("$XX");
}
//end
?>
</table>
</center>


Save this page in your web as results.php. The LIMIT statement limits the number of results shown for any query.

You may be asking yourself, "why would I put all those password and username details on this page, won't that be viewable to people looking at my source code?". Once you've published all this up to your site and the results page is generated, you'll find that when you view the source code that none of these details will be viewable.

To be extra safe, you can also add a blank page called "index.htm" if you don't already have an index or default page in the directory where this will be published. This will keep out prying eyes whom may try to browse the folder.

The search form

Now for a simple search form. Copy the code below into a new page, between the <body> and </body> tags. Be sure to change the form ACTION statement to point to where the results.php page will be on your site. This form is straight HTML, so it doesn't need to be saved as .php.

<form method="post" action="http://yourdomain.com/results.php" target="_blank">
<div align="center">
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td bordercolor="#000000">
<p align="center">
<select name="metode" size="1">
<option value="name">Name</option>
<option value="telephone">Telephone</option>
<option value="birthday">Birthday</option>
</select> <input type="text" name="search" size="25">  <br>
Search database: <input type="submit" value="Go!!" name="Go"></p>
</td>
</tr>
</table>
</div>
</form>


Save this page as search.htm. This code will submit the query to the database, then open results.php in a new window.

The big moment - publish and test your first PHP/MySQL application!

Now you're ready to publish up these pages to your site and to test out this very simple PHP/MySQL application!

With this particular application (assuming you've entered some records as per our phpMyAdmin exercise), you'll be able to type in partial or full names, numbers or dates - just make sure your query is relevant to the selection you make; i.e. Name, Telephone or Birthday.

A simpler data input form

In our phpMyAdmin tutorial, we learned how to input data into our table, but using phpMyAdmin for this purpose can be a bit cumbersome and risky as you are operating in the "heart" of your MySQL database - mistakes can occur. In our next tutorial, we'll create a very simple input form and php script you can use for this purpose, bypassing the need to use phpMyAdmin.


Article Details
Article ID: 69
Created On: 23 Jan 2008 12:06 AM

 Back
 Login [Lost Password] 
Email:
Password:
Remember Me:
 
 Search
 Article Options