Documente Academic
Documente Profesional
Documente Cultură
Basics of PHP - 4
Basics of PHP - 4
1. Updating existing record in MySQL database
Basically, you will need to do the following things.
(1) Show the data from database and let user choose a record to update
(2) Let user modify existing data
This requires setting default values to HTML form
(3) Update the data in database using SQL UPDATE command
1.1 Database structure and update operation
If the database consists of non-relational (independent) flat tables only, then updating can be
done by simply deleting old data and inserting new data into the table.
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
Minnie Mouse 2
1987-09-23
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
NAFED, Jakarta, 2
1. Delete record
02-2341-9573
However, if the database has relational tables, updating means Modifying data while
keeping the internal position of record in the table. So it cannot be done by simply deleting &
adding, especially when the table has primary key.
Suppose we do update of the following simple (but related) tables
participants2 table
pt_id
1
2
3
4
5
province table
participant_name
Mickey Mouse
Minnie Mouse
Donald Duck
Buffy Doggy
Dumbo Elephant
pv_id
1
2
3
4
5
province_name
Jakarta
Surabaya
Medan
Makassar
Bali
province_id
2
1
3
1
5
You can delete the record, and add new record with updated information
But updating Medan record in province table cannot be done by delete & add,
because doing so will change the id of Medan to other value. You should rather use
SQL UPDATE command to update the record.
ADMTC-UCSC-University of Colombo
Basics of PHP - 4
1.2 Interface for adding or updating table that has the link to other table
If the table has a field that is actually a key number to another table (like participants2
table), then that field should be a drop-down list of selectable values like shown below.
In order to display this kind of drop-down list, it is a good idea to write general-purpose
function to produce the list.
show_table_droplist
General-purpose function
<select name="province_id">
<option value=1>Jakarta</option>
<option value=2>Surabaya</option>
<option
value=3
selected>Medan</option>
<option value=4>Makassar</option>
... </select>
ADMTC-UCSC-University of Colombo
Basics of PHP - 4
show_table_droplist.php
1 <?php
2 function show_table_droplist (
3 $host_name, $user_name, $password, $database_name,
4 $table_name, $id_name, $field_name, $selected_id)
5{
6 /* Connect to MySQL database */
7 $link = mysql_connect($host_name, $user_name, $password) or die;
8 mysql_select_db($database_name) or die;
9
10 /* Execute SQL command */
11 $sql = "SELECT `$id_name`, `$field_name` FROM `$table_name`";
12 $result = mysql_query($sql);
13
14 /* Display drop-down list items */
15 while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
16 echo "<option value='$line[$id_name]'";
17
if ($line[$id_name] == $selected_id) echo " selected";
18
echo ">";
19 echo htmlspecialchars($line[$field_name]);
20 echo "</option>\n";
21 }
22
23 /* Closing procedures */
24 mysql_free_result($result);
25 mysql_close($link);
26 }
27 ?>
test_droplist.php Test page for the function
1 <html><body>
2 <p>Province:
You can add blank item if needed
3 <select name="province_id">
4 <option></option>
Name of host, user, password,
5 <?php
and database of MySQL
6 require_once("show_table_droplist.php");
7 show_table_droplist("localhost","yoichi","","test",
8 "province", "pv_id", "province_name", 3);
9 ?>
Name of table to show as drop-down list,
10 </select>
Field name of the table to be used as value,
11 </body></html>
Field name of the table to be used as each list item
Selected item number in drop-down list
ADMTC-UCSC-University of Colombo
Basics of PHP - 4
rdb_browse.php
Start page
Browse record
Browse record
Browse all
Search
Delete
rdb_delete.php
Delete
Confirmation
Yes
No
Update
Add new
Update
Add
/ Update
Name:
Province:
add/update
rdb_add_update.php
rdb_main.php
1
2
3
4
5
<HTML><BODY>
<H1>Relational Database Main Page</H1>
<p><a href="rdb_browse.php">Browse Record</a></p>
<p><a href="rdb_add_update.php">Add New Record</a></p>
</BODY></HTML>
rdb_browse.php
1 <?php
2 /* Initialize variables */
3 $submit = ""; // Which submit button was clicked by user
4 $search = ""; // Search string
5 $offset = 0;
// Starting position of record to show
6 $num_rows = 10;
// Number of records to show at a time
7 $max_row = 0; // Maximum row that has been displayed before
Show_MySQL_table2.php
show_table_droplist.php
ADMTC-UCSC-University of Colombo
Basics of PHP - 4
ADMTC-UCSC-University of Colombo
Basics of PHP - 4
ADMTC-UCSC-University of Colombo
Basics of PHP - 4
90
91 <!-- Hidden field to hold current variables -->
92 <INPUT type="hidden" name="offset" value="<?php echo $offset; ?>">
93 <INPUT type="hidden" name="max_row" value="<?php echo $max_row; ?>">
94
95 <HR>
96
97 <!-- Display button for update or delete -->
98 <p><b>Operation</b>: Mark a record, then</p>
99 <INPUT type="submit" name="submit" value="Update Marked Record">
100 <INPUT type="submit" name="submit" value="Delete Marked Record">
101 </FORM>
102 <a href="rdb_main.php">Return to Main Menu</a>
103 </body></html>
rdb_add_update.php
1 <?php
2 require_once("show_MySQL_table2.php");
3
4 /* Initialize all variables */
5 $is_update = isset($_GET["id"]); // TRUE if it is update screen
6 $pt_nm = "";
// Participant name
7 $pv_id = 0;
// Province id
8 $error = "";
// Error message
9 $success = ""; // Success message
10 if ($is_update) {
11
$title = "Update Record";
12
$button_caption = "Update";
13 } else {
14
$title = "Add New Record";
15
$button_caption = "Add";
16 }
17
18 /* Get current record datas */
19 if (isset($_POST["submit"])) {
20
$pt_nm = $_POST["name"];
21
$pv_id = $_POST["province_id"];
22 } elseif ($is_update) {
23
$link = mysql_connect("localhost", "yoichi", "") or die;
24
mysql_select_db("test") or die;
25
$result = mysql_query("SELECT * FROM participants2 WHERE pt_id ="
. $_GET["id"]);
26
if (mysql_num_rows($result) == 0) die;
ADMTC-UCSC-University of Colombo
Basics of PHP - 4
27
$line = mysql_fetch_array($result, MYSQL_ASSOC);
28
$pt_nm = $line["participant_name"];
29
$pv_id = $line["province_id"];
30
mysql_free_result($result);
mysql_close($link);
31 }
32
33 /* Perform SQL command */
34 if (isset($_POST["submit"])) {
35
/* Check values */
36
if ($pt_nm == "") $error .= "Please enter the name of participant";
37
if ($pv_id == "") $error .= "Please choose province";
38
/* If there's no error, then perform SQL command */
39
if ($error == "") {
40
if ($is_update) {
41
$sql = "UPDATE participants2 SET
participant_name='$pt_nm',
province_id=$pv_id WHERE pt_id=" . $_GET["id"];
42
$success = "The record has been updated.";
43
} else {
44
$sql = "INSERT INTO participants2 VALUES ('', '$pt_nm',
$pv_id)";
45
$success = "A new record has been added.";
46
$pt_nm = ""; $pv_id = 0;
// Clear all field!
47
}
48
show_MySQL_table2("localhost", "yoichi", "", "test", $sql, "",
"", "");
49
}
50 }
51 ?>
52 <html><body>
53 <H1><?php echo $title; ?></H1>
54 <FORM method='POST'>
55 <p>Participant Name:
56 <INPUT type='text' name='name' size=30 maxlength=50
value="<?php echo $pt_nm; ?>"></p>
57 <p>Province:
58 <select name="province_id">
59 <?php
60 if (!$is_update) echo "<option></option>";
61 require_once("show_table_droplist.php");
62 show_table_droplist("localhost","yoichi","","test","province",
"pv_id", "province_name", $pv_id);
63 ?>
64 </select></p>
65 <input type="submit" name="submit" value="<?php echo $button_caption
?>">
8
ADMTC-UCSC-University of Colombo
Basics of PHP - 4
ADMTC-UCSC-University of Colombo
Basics of PHP - 4
show_MySQL_table2.php
1 <?php
2 function show_MySQL_table2 (
3 $host_name, $user_name, $password, $database_name,
4 $sql, $offset, $num_rows, $id_field)
5{
6 /* Connect to MySQL database */
7 $link = mysql_connect($host_name, $user_name, $password) or die;
8 mysql_select_db($database_name) or die;
9
10 /* Execute SQL command */
11 $result = mysql_query($sql);
12 if (!$result) {
13 echo "<B>(Error in SQL)</B> " . mysql_error();
14 die;
15 }
16
17 /* If the SQL command is not SELECT, then stop function here */
18 if (strtoupper(substr($sql,0,6)) != "SELECT") {
19
mysql_close($link);
// Closing connection
20
return;
21 }
22
23 /* Get number of records found */
24 $num_found = mysql_num_rows($result);
25
26 if ($num_found == 0 ) {
27
mysql_close($link);
// Closing connection
28
return 0;
29 }
30
10
ADMTC-UCSC-University of Colombo
Basics of PHP - 4
11