Update Data From MySQL Using PHP – Tutorial makcode.in

For updating a knowledge that exist already within the database, UPDATE statement is employed

In the this example we update the details data from MySQL database

We include three PHP files for updating records

database.php – For connecting database.

update.php – To retrieve data from database with a update option.

update-process.php – To update data from database

Database.php

 <?php
$url='localhost';
$username='root';
$password='';
$conn=mysqli_connect($url,$username,$password,"user_details");f(!$conn){
 die('Could not Connect My Sql:' .mysql_error());
}
?>

Update.php

<?php
include_once 'database.php';
$result = mysqli_query($conn,"SELECT * FROM details");
?>
<!DOCTYPE html>
<html>
<head>
<title>Delete User Details </title>
</head>
<body>
<table>
<tr>
<td>First Name</td>
<td>Last Name</td>
<td>City</td>
<td>Email id</td>
<td>Action</td>
</tr>
<?php
$i=0;
while($row = mysqli_fetch_array($result)) {
if($i%2==0)
$classname="even";
else
$classname="odd";
?>
<tr class="<?php if(isset($classname)) echo $classname;?>">
<td><?php echo $row["id"]; ?></td>
<td><?php echo $row["firstname"]; ?></td>
<td><?php echo $row["lastname"]; ?></td>
<td><?php echo $row["city"]; ?></td>
<td><?php echo $row["email"]; ?></td>
<td><a href="update-process.php?id=<?php echo $row["id"]; ?>">Update</a></td>
</tr>
<?php
$i++;
}
?>
</table>
</body>
</html>

update-process.php

<?php
include_once 'database.php';
if(count($_POST)>0) {
mysqli_query($conn,"UPDATE details set id='" . $_POST['id'] . "', firstname='" . $_POST['firstname'] . "', lastname='" . $_POST['lastname'] . "', city='" . $_POST['city'] . "' ,email='" . $_POST['email'] . "' WHERE id='" . $_POST['id'] . "'");
$message = "Record Modified Successfully";
}
$result = mysqli_query($conn,"SELECT * FROM details WHERE id='" . $_GET['id'] . "'");
$row= mysqli_fetch_array($result);
?>
<html>
<head>
<title>Update User Details</title>
</head>
<body>
<form name="frmUser" method="post" action="">
<div><?php if(isset($message)) { echo $message; } ?>
</div>
<div style="padding-bottom:5px;">
<a href="retrieve.php">Ueser Details List</a>
</div>
Username: <br>
<input type="hidden" name="id" class="txtField" value="<?php echo $row['id']; ?>">
<input type="text" name="id"  value="<?php echo $row['id']; ?>">
<br>
First Name: <br>
<input type="text" name="firstname" class="txtField" value="<?php echo $row['firstname']; ?>">
<br>
Last Name :<br>
<input type="text" name="lastname" class="txtField" value="<?php echo $row['lastname']; ?>">
<br>
City:<br>
<input type="text" name="city" class="txtField" value="<?php echo $row['city']; ?>">
<br>
Email:<br>
<input type="text" name="email" class="txtField" value="<?php echo $row['email']; ?>">
<br>
<input type="submit" name="submit" value="Submit" class="buttom">

</form>
</body>
</html>

Leave a Reply

Your email address will not be published. Required fields are marked *