Monday, 18 February 2013

How to update/edit a data in database using Mysql

Step 1:  Make a file named edit.php

<?php
include("include/connection.php");
$id=$_GET['id'];
?>

Step 2:  Make a form according to insertion.

Step 3:  Above the form add a selection query using where condition. for e.g.:

$query="select * from register where id=". $id;

Step 4:  $result=mysql_query($query);

while($row=mysql_fetch_array($result))

{

$name=$row['name'];

$email=$row['email'];

}

if(isset($_POST['submit'])=='update')

{

$name=$_POST['name'];

$email=$_POST['email'];

$query_update="update register set name='$name',email='$email' where id=". $id;

Step 5:  Run the query 

$result_update=mysql_query($query_update);
if($result_update=='1')
{
header("location:selection.php");
}
else
{
echo"data not updated. Please try again";
}
}
?>
<html>
<head>
</head>
<body>
<form name="edit" action="" method="post" onsubmit="return validate()">
<table border="0" width="50%" align="center">
<tr>
<td>Name:</td><td><input type="text" name="name" id="name"value="<?php echo $name; ?>" size="20"></td>
</tr>
<tr>
<td>Email:</td><td><input type="text" name="email" id="email" value="<?php echo $email; ?>" size="20"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="submit" value="update"></td>
</tr>
            </table>
</form>
</body>
</html>
Like this we can update the name and email of the person.

No comments:

Post a Comment