How to Edit/Update Records From MYSQLi Via Textbox in PHP

Hi, friends in this tutorial I show you how to edit/update records from a MySQL database via textbox in PHP. Using fetch_assoc function we are going to display records in the textbox from MySQL database. To update a record can be updated into MySQL tables by executing an SQL UPDATE statement through PHP function mysql_query.

disp.php

<?php
//Connection for database
include 'conn.php';
//Select Database
$sql = "SELECT * FROM data";
$result = $conn->query($sql);
?>
<!doctype html>
<html>
<body>
<h1 align="center">Employee Details</h1>
<table border="1" align="center" style="line-height:25px;">
<tr>
<th>Employee ID</th>
<th>Name</th>
<th>Gender</th>
<th>Department</th>
<th>Address</th>
<th>Mobile Number</th>
<th>Email</th>
</tr>
<?php
//Fetch Data form database
if($result->num_rows > 0){
 while($row = $result->fetch_assoc()){
 ?>
 <tr>
 <td><?php echo $row['empid']; ?></td>
 <td><?php echo $row['name']; ?></td>
 <td><?php echo $row['gender']; ?></td>
 <td><?php echo $row['department']; ?></td>
 <td><?php echo $row['address']; ?></td>
 <td><?php echo $row['mobile']; ?></td>
 <td><?php echo $row['email']; ?></td>
 <!--Edit option -->
 <td><a href="edit.php?edit_id=<?php echo $row['empid']; ?>" alt="edit" >Edit</a></td>
 </tr>
 <?php
 }
}
else
{
 ?>
 <tr>
 <th colspan="2">There's No data found!!!</th>
 </tr>
 <?php
}
?>
</table>
</body>
</html>
edit.php

<?php
//Database Connection
include 'conn.php';
//Get ID from Database
if(isset($_GET['edit_id'])){
 $sql = "SELECT * FROM data WHERE empid =" .$_GET['edit_id'];
 $result = mysqli_query($conn, $sql);
 $row = mysqli_fetch_array($result);
}
//Update Information
if(isset($_POST['btn-update'])){
 $name = $_POST['name'];
 $gender = $_POST['gender'];
 $department = $_POST['department'];
 $address = $_POST['address'];
 $mobile = $_POST['mobile'];
 $email = $_POST['email'];
 $update = "UPDATE data SET name='$name', gender='$gender',department='$department',address='$address',mobile='$mobile',email='$email' WHERE empid=". $_GET['edit_id'];
 $up = mysqli_query($conn, $update);
 if(!isset($sql)){
 die ("Error $sql" .mysqli_connect_error());
 }
 else
 {
 header("location: disp.php");
 }
}
?>
<!--Create Edit form -->
<!doctype html>
<html>
<body>
<form method="post">
<h1>Edit Employee Information</h1>
<label>Name:</label><input type="text" name="name" placeholder="Name" value="<?php echo $row['name']; ?>"><br/><br/>
<label>Gender:</label><input type="text" name="gender" placeholder="Gender" value="<?php echo $row['gender']; ?>"><br/><br/>
<label>Department:</label><input type="text" name="department" placeholder="Department" value="<?php echo $row['department']; ?>"><br/><br/>
<label>Address:</label><input type="text" name="address" placeholder="Address" value="<?php echo $row['address']; ?>"><br/><br/>
<label>Mobile:</label><input type="text" name="mobile" placeholder="Mobile" value="<?php echo $row['mobile']; ?>"><br/><br/>
<label>Email:</label><input type="text" name="email" placeholder="email" value="<?php echo $row['email']; ?>"><br/><br/>
<button type="submit" name="btn-update" id="btn-update" onClick="update()"><strong>Update</strong></button>
<a href="disp.php"><button type="button" value="button">Cancel</button></a>
</form>
<!-- Alert for Updating -->
<script>
function update(){
 var x;
 if(confirm("Updated data Sucessfully") == true){
 x= "update";
 }
}
</script>
</body>
</html>

Was this article helpful?

Yes No
×

How can we improve it?

×

We appreciate your helpul feedback!

Your answer will be used to improve our content. The more feedback you give us, the better our pages can be.

Follow us on social media:

Facebook Pinterest
Share via:

1 Comment

  • Hello can u help me i follow all your step for update however i did not get any error and it does not update either..

Leave a Reply

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