[back]

Populating the Table: Using a PHP script with an HTML form

There is an easier method of getting information into your table. You can create a simple HTML page and pass the information through forms.

First, you need to set up an HTML input page:

<html>
<head><title>info input</title></head>
<body>

<form action="insert2.php" method="post">

First Name: <input type="text" name="first" SIZE="30"><p>
Last Name: <input type="text" name="last"SIZE="30"><p>

Graduation Year:
<SELECT NAME="grad_year" SIZE="1">
<OPTION SELECTED>2004</option>
<OPTION>2005</option>
<OPTION>2006</option>
<OPTION>2007</option>
</SELECT><p>

Student Number: <input type="text" name="student_no" SIZE="6"><p>
<input type="Submit">
</form></body>
</html>

HTML Explained:

This is a pretty straight forward form. Note that the Action is being sent to our input2.php page. (which we will create in a minute) Also note that I set up the Graduation Year as a pull down menu. Remember that when we set up our table we set this field as an "INT". If the user would type in "freshman", the entire entry would be dropped. Another option would be to have a text field and then use a conditional to check that the value was an integer.

Creating the PHP page:

<?php
$user="mills";
$password="123";
$database="class";

$first=$_POST['first'];
$last=$_POST['last'];
$grad_year=$_POST['grad_year'];
$student_no= $_POST['student_no'];

mysql_connect(localhost,$user,$password);
@mysql_select_db($database) or die( "Unable to select database");

$query = "INSERT INTO students VALUES ('','$first','$last','$grad_year','$student_no')";

mysql_query($query);
mysql_close();

?>

PHP Explained:
Note that the only change is that the values from the HTML form are being passed to the PHP page. Instead of the values being entered directly into the PHP form, we are using the HTML variables.

Create a new HTML page called input.htm and a new php page and named "insert2.php". Insert the next couple of students from names and info you make up.