Amazon.com: PC Books || Educational Software || Magazines
Amazon: Books-CA || Software-CA || Books-UK || Software-UK

HTML | Javascript | ASP | PHP | VBScript | SQL | Hardware | PC FAQ| WinXP|

Learn to Build, Upgrade, or Repair your Computer Ebook + PC Safety 101 kwwebservice.com Reasonably priced web development & hosting
PHP Basics
Introduction
Variables & Arrays
Sub & Functions
If Statements
Case Statements
Loop Statements
Date Objects
Form Processing
Text File Objects
Database Access
Adding Record
Updating Record
Deleting Record
Virtual Includes
Cookies
Session
Server Variables
Creating Login Page
E-mail
PHP Books @
CA Amazon
UK Amazon
US Amazon

  :: Home
PC Topics
  :: Build A PC
  :: Windows XP
  :: PC Help
Tutorials
  :: HTML
  :: JavaScript
  :: ASP
  :: PHP
  :: VBScript
  :: SQL
Miscellaneous
  :: Code/Scripts
  :: Forum
  :: Links
  :: Contact us
  :: Tell A Friend
  :: In Somali
  ::
Web Articles
  Top Web Hosting
Reviews
  Processing
Online Card Payment Guide
  Domain name
registration & buying guide
  Getting
a website online guide
  Search Engine
submission & optimization tips
Netfirms Web Hosting
Free trial
Quality business correspondence.
Yahoo! Search Marketing

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
Bluehost.com Web Hosting $6.95
Listed @ ConsumerVote.com - The Consumer Rated Web Directory
PC System Tools
SpyCleaner
Registry Mechanic
4Diskclean Pro
Health PC Club
TweakNow
Registry Washer
SpyAnyWhere
System Lifeguard
...More/Details

PC Articles
  Computer Safety
prevent viruses & Spyware