|
|
|
CMS takes the work out of building interactive websites. |
PHP MySQL
|
| 1 | Peggy | June4 |
| 2 | Mark | Mar27 |
The form which allows the user to make changes should display existing information in input boxes for editing. The code for accomplishing this is shown below.
Once again we make use of the mysql_query and mysql_num_rows functions. Then we create a while loop, insert a basic html form inside and use PHP echo statements in the value field to display existing data.
We also make use of a new function mysql_result(), which separates each field into resource identifier, index and value.
We'll use a hidden input text box to pass the id value to the processing form as ud_id
<input type="hidden" name="ud_id" value="<? echo "$id" ?>">
<html><head><title></title></head>
<body>
<?
$id=$_POST['id'];
$db="mydatabase";
$link = mysql_connect("localhost");
if (! $link)
die("Couldn't connect to MySQL");
mysql_select_db($db , $link)
or die("Couldn't open $db: ".mysql_error());
$result=mysql_query(" SELECT * FROM birthdays WHERE id='$id'");
$num=mysql_num_rows($result);
$i=0;
while ($i < $num) {
$name=mysql_result($result,$i,"name");
$birthday=mysql_result($result,$i,"birthday");
?>
<table width="300" cellpadding="3" cellspacing="0" border="2">
<tr align="center" valign="top">
<td align="center" colspan="1" rowspan="1" bgcolor="#5FACD6">
<form action="change_birthdays.php" method="post">
<input type="hidden" name="ud_id" value="<? echo "$id" ?>">
Name: <input type="text" name="ud_name" value="<? echo "$name"?>"><br>
Birthday: <input type="text" name="ud_birthday" value="<? echo "$birthday"?>"><br>
<input type="Submit" value="Update">
</form>
</td></tr></table>
<?
++$i;
}
?>
</body>
</html>
The user makes changes to the desired field or fields and clicks the Submit button to pass the information to the processing script. The PHP script change_birthdays.php isn't really much more than the first line of code we presented at the top of the page.
The SET option is used to assign the values received from the previous form to the fields name and birthday WHERE the ud_id string matches the id field.
<html><head><title></title></head>
<body>
<?
$ud_id=$_POST['ud_id'];
$ud_name=$_POST['ud_name'];
$ud_birthday=$_POST['ud_birthday'];
$db="mydatabase";
$link = mysql_connect("localhost");
if (! $link)
die("Couldn't connect to MySQL");
mysql_select_db($db , $link)
or die("Couldn't open $db: ".mysql_error());
mysql_query(" UPDATE birthdays SET name='$ud_name' ,birthday='$ud_birthday' WHERE id='$ud_id'");
echo "Record Updated";
mysql_close($link);
?>
</body>
</html>
The Birthdays Database management files can be downloaded in a zip file. The package contains an integrated db management system, with a simple login file and interface. To run the scripts on your PC you must have a localhost server installed along with PHP and the MySQL server.
Download birthdays_db.zip
To extend your knowledge of MySQL study the Docs and Tutorials at the official MySQL website. MYSQL.com
Contents
Free PHP MySQL ToolsAdvanced
Multiple Selection FormsCopyright © 2008 - Net Success 2000 Plus Inc - Somerset KY 42502 |