Thursday, May 24, 2012

PHP MySQL Update

One of the previous post  we created a table named "biodata" it looks as follows


First Name Last Name Age
Scarlett Johansson 25
Clooney Anderson 34
Kate Hudson Hudson 25

The following php codes update some data in the "biodata" table in my_database

 

Update data in Mysql Table

The following example update data in table named "Biodata", with three columns. The column names will be "FirstName", "LastName" and "Age":

<?php
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("my_database", $con);

mysql_query("UPDATE biodata SET Age=25
WHERE FirstName='Clooney' AND LastName='Anderson'");

mysql_close($con);
echo "sucessfully updated";
?>

After the update is completed the "biodata" table will look like this


First Name Last Name Age
Scarlett Johansson 25
Clooney Anderson 25
Kate Hudson Hudson 25

Update table "biodata using Web Interface  File(php&Html)

The Update Table I designed will look like this

First Name Last Name Age Update
Scarlett Johansson25 Update
Clooney Anderson25 Update
Kate Hudson Hudson25 Update

Codes For Above Update table as follows

<?php
$host="localhost";
$username="root";
$password="";
$db_name="my_database";
$tbl_name="biodata";
mysql_connect("$host","$username","$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); $sql="SELECT * FROM $tbl_name"; $result=mysql_query($sql); ?>
<table align="center" bgcolor="#b3dfef">
<tr> <td align="center" bgcolor="#FFFFFF"><strong>First Name</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Last Name</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Age</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Update</strong></td> </tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
 <td bgcolor="#FFFFFF"><?php echo $rows['FirstName']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['LastName']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['Age']; ?></td>
<td bgcolor="#FFFFFF"><a href="edit.php?id=<?php echo $rows['id']; ?>">Update</a></td>
</tr>
<?php
}
mysql_close(); ?>
</table>

After Clicking Update the fallowing Table will be displayed

Person ID : 3
Person ID. can't changed :
FirstName : Kate Hudson
Edit First Name :
LastName : Hudson
Edit Last Name :
Age : 25
Edit Age : :
Codes for the above edit.php as follows

<table align="center" border=1 >
<?php $id=$_GET['id'];
$host="localhost";
$user="root";
$pass="";
$db="my_database";
mysql_connect($host,$user,$pass); @mysql_select_db($db) or die( "Unable to select database"); $Query="SELECT * from biodata where id = '$id'";
$dbresult=mysql_query($Query);
if(mysql_num_rows($dbresult) >0) { while($row=mysql_fetch_row($dbresult)) {
 echo "<form method=post name=f1 action='editsave.php'>";
echo "<tr><td>Person ID : " . "$row[0] </td></tr>"; echo "<tr><td><font color = red> ID. can't changed </font> : " . "<input name = editid id = editid readonly type='hidden' value = '$row[0]'></td></tr>";
echo "<tr><td>FirstName : " . "$row[1] </td></tr>"; echo "<tr><td><font color = red> Edit First Name </font> : " . "<input name = editFirstName id =editFirstName type = text value = '$row[1]'></td></tr>"; echo "<tr><td>LastName : " . "$row[2] </td></tr>"; echo "<tr><td><font color = red> Edit Last Name </font> :"; echo "<input name='editLastName' id = 'editLastName' type=test value='$row[2]'></td></tr>"; echo "<tr><td>Age : " . "$row[3] </td></tr>"; echo "<tr><td><font color = red> Edit Age : </font> :"; echo "<input name='editAge' id = 'editAge' type=test value='$row[3]'></td></tr>"; echo "<tr><td><input type=submit value=Submit></td></tr>"; echo "</form>";
 }
} else
{
 } ?>
</td></tr></table>

File Used to update mysql is editsave.php Codes of "editsave.php" as follows

<?php
$id=$_POST['editid'];
$FirstName=$_POST['editFirstName'];
$LastName=$_POST['editLastName'];
 $Age=$_POST['editAge'];
echo "<p class=\"text_black\"><b>The Details of Persion ID.    ".$person_id."are Updated as following<br><br></b>";
echo "First Name : "; echo $FirstName."<br>";
echo "Last Name : "; echo $LastName."<br>";
echo "Age:"; echo $Age."<br>";
$host="localhost";
$user="root";
$pass="";
$db="my_database";
mysql_connect($host,$user,$pass); @mysql_select_db($db) or die( "Unable to select database");
$Query = "UPDATE biodata Set FirstName = '$FirstName',LastName = '$LastName',Age= '$Age' WHERE id='$id'";
$dbresult=mysql_query($Query); if($dbresult) {
echo "<p class=\"text_black\">Thankyou for Updating the Detail.<br><br>";
}
else
{ echo "<p class=\"text_black\">Some problem occured. Please try again <br><br>"; }
 ?>

15 comments: