New member to sign up
Sep 25, 2009
Author: Developer
The following example program allows a new member to sign up; upon submitting the form, a new record is created for him in the database.
<?php
/* ch13ex05.php – member signup form demonstration
*//* CONSTANT DECLARATIONS */
define(‘MYSQL_HOST’, ‘mysql.example.com’);
define(‘MYSQL_USER’, ‘admin’);
define(‘MYSQL_PASS’, ‘abc123’);
define(‘MYSQL_DB’, ‘PHPByExample’);
/* MAIN PROGRAM HERE */
if (! isset($action) )
{
$action = NULL;
}
switch($action)
{
default:displayForm();
break;
case ‘
signup’:signUp($HTTP_POST_VARS);
displaySuccess();
break;
}
/* DEFINITIONS ARE BELOW THIS POINT */
function displayForm()
{
head();
?><form action="”<?php">” method=”POST”>
<input name="”action”" type="”hidden”" value="”signup”" />
Name: <input type=”text” name=”name”><br>
E-mail: <input type=”text” name=”email”><br>
Age: <input type=”text” name=”age”><br>
<input type=”submit”>
</form>
<?php
foot();
}
function signUp($input)
{
// If we fail to connect, we can’t keep going, so we exit
if (! mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASS) )
{
echo ‘Failed to connect to host “‘ . MYSQL_HOST . ‘“.’;
exit;
}
mysql_select_db(MYSQL_DB);
mysql_query(“INSERT INTO members SET name=’{$input[‘name’]}’,
email=’{$input[‘email’]}’, “ .
“age={$input[‘age’]}”);
}
function displaySuccess()
{
head();
?>Your submission has been completed!
<?php
foot();
}
function head()
{
echo “<html><body>”;
}
function foot()
{
echo “</body></html>”;
}
?>
Upon entering the correct information, this program completes the insertionand displays a success message.
views 2876



