How to update data in Database using PHP and MySQLi | Part 4

In this tutorial, I will show you how to update data from database.please view my previous videos of Insert, Show and Delete for better Understanding.Here again with the help of id, we can update particular row.Updating data is little bit lengthy than others.

Steps to Update data :

  • insert.php 

$sel="SELECT * FROM `data`";
$sel1=mysqli_query($con,$sel);
while($result=mysqli_fetch_array($sel1))
{?>
<table>
<tr>
<td><?php echo $result['name'] ?></td>
<td><?php echo $result['class'] ?></</td>
<td><?php echo $result['marks'] ?></</td>
<td><a href="insert.php?var=<?php echo $result['id'] ?>">Delete</a></td>
<td><a href="update.php?up=<?php echo $result['id'] ?>">edit</a></td>
</tr>
</table>
<?php
}
  • update.php
<?php include('config.php'); ?>
<title>
Update data in database
</title>
<body>
<?php
$sel="SELECT * FROM `data` WHERE id='".$_GET['up']."'";
$sel2=mysqli_query($con,$sel); 
$result2=mysqli_fetch_array($sel2);
?>
<form method="post" action="">
Name<input type="text" name="name" value="<?php echo $result2['name']?>">
class<input type="text" name="class" value="<?php echo $result2['class']?>">
marks<input type="text" name="marks" value="<?php echo $result2['marks']?>">
<input type="submit" name="submit">
</form>

</body>
</html>
<?php 
if(isset($_POST['submit']))
{
$name=$_POST['name'];
$class=$_POST['class'];
$marks=$_POST['marks'];
$up1="UPDATE `data` SET name='$name',class='$class',marks='$marks' WHERE id='".$_GET['up']."'";
if(mysqli_query($con,$up1))
{
header('location:insert.php');
}
}
?>


Rate this posting:
{[['']]}

No comments

Powered by Blogger.