Professional Documents
Culture Documents
Basics of PHP - 3
Basics of PHP - 3
1. Adding new record to MySQL database
Basically, you will need to do the following things.
(1) Get new values from the user using HTML form.
(2) Validate new data (This is the most important part)
(3) Add new record to database using SQL INSERT command
(Example)
Suppose we have the following table of participants.
No.
1
2
3
4
5
Name
Mickey Mouse
Minnie Mouse
Donald Duck
Buffy Doggy
Dumbo Elephant
Sex
M
F
M
M
M
Birthday
1961-07-01
1987-09-23
1989-11-25
2002-07-03
2002-07-04
Address
3535, Wall street, CA
NAFED, Jakarta
RETPC, Surabaya
JICA, Tokyo
MOE, Somewhere
Telephone
02-1234-0987
02-2341-9573
02-0492-9713
03-6731-1231
04-4827-3719
ADMTC-UCSC-University of Colombo
Basics of PHP - 3
<HTML><BODY>
<H1>Add new record to participants database</H1>
<FORM method="POST">
<p>Name:<INPUT type="text" name="name" size="50" maxlength="50"></p>
<p>Sex:
<INPUT type="radio" name="sex" value="M">Male
<INPUT type="radio" name="sex" value="F">Female
</p>
<p>Birthday:
size and maxlength should be used to limit
Year
the maximum size of the data. These values must
match with the definition of each field in database
<select name="year">
"<option></option>"
<?php
for ($y = 1900; $y <= date("Y"); $y++) {
echo "<option>$y</option>n";
}
?>
</select>
Month
<select name="month">
"<option></option>"
<?php
for ($m = 1; $m <= 12; $m++) {
echo "<option>$m</option>n";
}
?>
</select>
2
ADMTC-UCSC-University of Colombo
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
Basics of PHP - 3
Day
<select name="day">
"<option></option>"
<?php
for ($d = 1; $d <= 31; $d++) {
echo "<option>$d</option>n";
}
?>
</select>
</p>
<p>Address:<INPUT type="text" name="address" size="100" maxlength="100"></p>
<p>Telephone:
<INPUT type="text" name="tel1" size="4" maxlength="4"><INPUT type="text" name="tel2" size="4" maxlength="4"><INPUT type="text" name="tel3" size="4" maxlength="4">
</p>
<p><INPUT type="submit" name="submit" value="Add Record"></p>
<p><INPUT type="reset" name="reset" value="Clear"></p>
</FORM>
<HR>
<?php
function invalid($error_message) {
echo "<p><font color='red'>$error_message</font></p>";
echo "</BODY></HTML>";
die;
Same as previous practice
}
require_once("show_MySQL_table.php");
ADMTC-UCSC-University of Colombo
Basics of PHP - 3
ADMTC-UCSC-University of Colombo
Basics of PHP - 3
sprintf is a very
useful function that
returns a string that
combines the values of
one or more variables
using specified format
Prevent duplication of data If the database should not have duplicated data, you
should first search for the same data in database so that there will be no same data in
the database.
In order to implement this function, some additional modification is needed for our
general-purpose function show_MySQL_table ( see 2.2)
Better retry interface for error When user made even tiny mistake, all input data will
be disappeared and must be entered from the beginning. The form should be able to
display the user-entered data as the initial value in each form element.
Below is the example of inserting initial value for input-text box.
ADMTC-UCSC-University of Colombo
Basics of PHP - 3
Advantage: Very easy to develop. You can use almost the same code as record
addition in practice 1, only changing the SQL command from INSERT ~ to
DELETE ~.
Disadvantage: There is no display of existing record to be deleted. It is very
difficult to use. User must enter exactly the same data to delete.
ADMTC-UCSC-University of Colombo
Basics of PHP - 3
Advantage: Easy to use because user can see the list of existing records. It is also
convenient if the user needs to delete multiple records at once.
Disadvantage: Not so easy to write PHP code to perform this function. It is also
necessary to implement multiple page display if number of records are very large.
Interface design for the deletion 3
Advantage: Rather easy to develop. The same interface can be used for Search
page. It is also useful for the database with large number of records. Multiple
records deletion is also possible depending on the search result.
ADMTC-UCSC-University of Colombo
Basics of PHP - 3
if ($num_found != 0) {
Output number of
records on screen
...
Add this line
(Closing brace for
if statement)
echo "</table>n";
}
/* Closing procedures */
mysql_free_result($result);
// Free result
mysql_close($link); // Closing connection
return $num_found;
}
?>
ADMTC-UCSC-University of Colombo
Basics of PHP - 3
<HTML><BODY>
<H1>Search & Delete record from participants database</H1>
<H3>Enter one or more fields to search</H3>
<FORM method="POST">
...
45 <p><INPUT type="submit" name="submit" value="Search Record"></p>
46 <p><INPUT type="reset" name="reset" value="Clear"></p>
47 </FORM>
2.4 PHP initialization
It is almost the same as practice 1 except that we should change the name of function
invalid.
delete_record.php (continued)
48 <?php
49 function stop($error_message) {
...
56 require_once("show_MySQL_table.php");
2.5 Overall submit validation
First of all, we check if the page has been shown for the first time (without posting of any
data), or user chose NO for the confirmation of deletion. In both case, we stop the PHP code
here.
58 /* If it is the initial screen or user has clicked "NO" button */
59 if (!isset($_POST["submit"]) $_POST["submit"]=="NO") stop("");
60
IMPORTANT: In this example, all 3 submit buttons on the page (Search Record, YES, and
NO) have the same name attribute as submit (i.e. name=submit).
Therefore, the posted value from the form $_POST["submit"] can also take 3 different
values (Search Record, YES, or NO) depending on which button the user
clicked.
ADMTC-UCSC-University of Colombo
Basics of PHP - 3
10
ADMTC-UCSC-University of Colombo
Basics of PHP - 3
11
ADMTC-UCSC-University of Colombo
Basics of PHP - 3
IMPORTANT: Confirmation buttons (YES and NO) are located in another HTML form. You
can include multiple forms within one page, so that you can post different
information by different forms.
In line 94~95, we use single quotation (') instead of double quotation (") to enclose echo
statement. This is because the variable $where already contains single quotation,
and that we must make all the strings valid in terms of quotation usage. In other
words, any enclosing symbols in PHP must be used as pair with no conflict with
others.
(Example)
$where
1st-level (')
3rd-level (")
3rd-level (")
2nd-level (<~>)
4th-level (')
"
value =
2nd-level (<~>)
"
name LIKE
'
3rd-level (")
$where
%kogure%
4th-level (')
'
"
>
'
<INPUT type="hidden"> is a text box that will not appear on the screen. You can use this
hidden text box to store information that should be posted again to Web server. In
this case, we store the WHERE phrase that we will use for record deletion.
12