Please send questions to
st10@humboldt.edu .
<html>
<!-- try_mysql2.php - see: CAN we create a form to populate the pets table
on sorrel?
by: Sharon Tuttle
last modified: 11-30-06
-->
<head>
<title> try_mysql2.php </title>
</head>
<body>
<h2> try_mysql2.php - insert a new pet into the pets table </h2>
<?php
// if you have username and password already...
if ($_POST["_submit_check"])
{
$username = $_POST["username"];
$password = $_POST["password"];
$pet_id = $_POST["pet_id"];
$pet_name = $_POST["pet_name"];
$pet_birth = $_POST["pet_birth"];
$num_siblings = $_POST["num_siblings"];
$con = mysql_connect("localhost", $username, $password);
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
// NOTE: on sorrel's mysql, database name is same as username
@mysql_select_db($username) or die("Unable to select database");
// try to insert a new row into pets
$command = "insert into pets ".
"values ".
"($pet_id, '$pet_name', '$pet_birth', $num_siblings)";
if ( mysql_query( $command, $con ) )
{
print "<br> Inserted row - thank you! </br>\n";
}
else
{
print "Could not insert row: " . mysql_error();
}
mysql_close($con);
}
else
{
print '<form method="post" action="' . $_SERVER[PHP_SELF] . '">';
?>
<table>
<tr> <td> Username: </td>
<td> <input type="text" name="username">
</tr>
<tr> <td> Password: </td>
<td> <input type="password" name="password">
</tr>
<tr> <td> Pet id </td>
<td> <input type="text" name="pet_id">
</tr>
<tr> <td> Pet name </td>
<td> <input type="text" name="pet_name">
</tr>
<tr> <td> Pet date of birth (YYYY-MM-DD) </td>
<td> <input type="text" name="pet_birth">
</tr>
<tr> <td> Number of siblings of this pet </td>
<td> <input type="text" name="num_siblings">
</tr>
</table>
<br><br>
<input type="submit" value="Try to insert">
<input type="hidden" name="_submit_check" value="1">
</form>
<?php
}
?>
</body>
</head>