|
|
|
Quality business correspondence.
|
|
|
|
Update a record
There are more than one way to do things. For this example, we are going
to list items from the database so that you can select a record to edit.
Code to list records form the table we created in previous lesson named feeds.
Take this code and save it file as php file display.php
display.php
<head>
<title>Display feeds for edit</title>
<link href="sheet.css" rel="stylesheet" type="text/css">
</head>
<body >
<?php
include("connect.inc");
print "<table width='100%' border='1'>";
$query="SELECT * FROM feed";
$result=mysql_query($query);
$num=mysql_numrows($result);
print "<tr><td align='center'>Edit</td><td colspan='3' align='center'>Name</td></tr>";
$i=0;
while ($i < $num) {
$name=mysql_result($result,$i,"name");
$comments=stripslashes(mysql_result($result,$i,"comments"));
$id=mysql_result($result,$i,"user_id");
print "<tr><td><a href='toUpdate.php?id=$id'>Edit</a></td><td>$name</td></tr>";
$i++;
}
mysql_close($dbh);
print "</table>";
?>
</body>
</html>
|
User_ID is a unique field which identifies the selected record.
When the user selects record and clicks on Edit, the information is displayed for processed
in the toUpdat.php form. Most of the Code is SQL, go to the sql section if you need futher help with SQL.
Two php function to mention are addslashes() and stripslashes().
If you write a text with Hyphen, the php will replace it with backword slash \ and you will lose any text after \.
To prevent this, use addslashes() at insert and stripslashes() at display.
There is also PHP addon function; magic_quotes_gpc that you can set by default to handle qoutes and hyphens.
toUpdate.php
<html>
<head>
<title>Edit comment</title>
</head>
<body>
<?php
$msg="";
include("connect.inc");
if(isset($_REQUEST['id']))
{
$id=$_REQUEST['id'];
$query="SELECT * FROM feed WHERE user_id='$id'";
$result=mysql_query($query);
if( $result && $feeds = mysql_fetch_object($result) )
{
$id=$feeds->user_id;
$name=$feeds->name;
$comments=$feeds->comments;
}
}
else
{
$msg="No records retreived. Wrong access";
$id=NULL;
$name="";
$comments="";
}
?>
<form name="updated" action="saveupdate.php" method="post">
<?php print "<font color='red'>$msg</font>";?>
<table border=1>
<tr><td colspan="2" align="center">Update and save</td></tr>
<tr>
<?php
print"<td><input type='hidden' name='id' value='$id'></td>";
?>
</tr><tr>
<td>Name: </td>
<?php
print"<td><input type='text' name='name' value='$name'></td>";
?>
</tr><tr>
<td>Comment: </td>
<?php
print"<td><textarea name='comments' cols='40' rows='8'>$comments</textarea></td>";
?>
</tr><tr>
<td colspan="2" align="center"><input type="submit" name="submit" value="Save"></td>
</tr>
</table>
</form>
</body>
</html>
|
After the new data is entered, the next thing is to save the data.
Following code will handle the data update.
Check the demo result
saveupdate.php
<?php
include("connect.inc");
$msg="";
$save="";
if (isset($_POST['submit']))
{
$id=$_POST['id'];
$name=$_POST['name'];
$comments=$_POST['comments'];
if(trim($name)=="")
{
$msg=$msg."Name is missing.<br>";
}
if(trim($comments)=="")
{
$msg=$msg."No comment provided.<br>";
}
if(!get_magic_quotes_gpc())
{
$comments = addslashes($comments);
}
if(trim($msg) =="")
{
$query="UPDATE feed SET name='$name', comments='$comments' WHERE id='".$id."' ";
$result = mysql_query($query);
$save=$save."<font color=blue size=4><strong>* Record was sucessfully entered* <br><a href='view.php'>View Feeds</a></font></strong><br>";
mysql_close($dbh);
}
}
}
if(trim($msg)!=""){
print "$msg";}
if(trim($save)!=""){
print "$save";}
?>
|
Add Data
Delete Data
|
|
|