|
Registration Form Tutorial
Part 2: Add member Script
|
Copy
the following script into another blank document in a text editor,
Netscape Composer, Dreamweaver, etc. and save the script as
add_entry.php.
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
<!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type"
content="text/html; charset=iso-8859-1" />
<title>Add a Blog Entry</title>
</head>
<body>
<?php // Script
12.5 - add_entry.php
// This script adds a
blog entry to the database.
// Address error handing.
ini_set
('display_errors', 1);
error_reporting (E_ALL
& ~E_NOTICE);
if (isset
($_POST['submit'])) { // Handle the form.
// Connect and select.
if
($dbc = @mysql_connect ('localhost', 'username',
'password')) {
if (!@mysql_select_db ('myblog'))
{
die
('<p>Could select the database because: <b>' .
mysql_error() . '</b></p>');
}
}
else {
die ('<p>Could not connect
to MySQL because: <b>' . mysql_error() . '</b></p>');
}
//
Define the query.
$query = "INSERT INTO blog_entries (blog_id, title,
entry, date_entered) VALUES
('{$_POST['title']}',
'{$_POST['entry']}',
NOW())";
//
Execute the query.
if
(@mysql_query ($query)) {
print '<p>The blog entry
has been added.</p>';
}
else {
print "<p>Could add the
entry because: <b>" . mysql_error() . "</b>. The query was
$query.</p>";
}
mysql_close();
}
// Display the form.
?>
<form
action="add_entry.php" method="post">
<p>Entry Title:
<input type="text" name="title" size="40"
maxsize="100" /></p>
<p>Entry Text:
<textarea name="entry" columns="40"
rows="5"></textarea></p>
<input type="submit"
name="submit" value="Add to the Blog!" />
</form>
</body>
</html>
|
Script Explanation:
The primary working portion of this script is lines
18-42.
Lines 18-27 connect to MySQL and select the database or give an error
message if a problem occurs. Lines 30-32 define the query and
into which columns the information is to be placed. Lines 33-38
execute the query or return an error message. Line 40 closes the
connection to the database. More information about SQL can be
obtained at W3
Schools or SQL
Course.com.
Modifications:
Delete all of the lines except those which are in
blue.
Add :
// End
modified Script 12.5 - add_member.php to the bottom of the script to delineate
the end of the script
Line 31: 0, after
VALUE ( ;
this will allow the database to
automatically create and member id and increment it with each new member
Next, change the information in the following lines:
Line 9: change add_entry to add_member
Line 10: change blog entry to member's data
Line 21:
change myblog to mymembers
Line 31: change blog_entries (blog_id, title,
entry, date_entered) to member_list
(member_id, username, first_name,
last_name, email_addy, password, date_reg)
copy '{$_POST['title']}', three(3) additional times before NOW()
change the words in the
brackets to username, first_name, last_name, email_addy and password, respectively
Line 35: change blog entry to member's data
Line 37: change entry to member's data
Save the script.
|
|