PHP update database table data using form input fields

How to update the data using forms input fields ?

To update the data using forms , first we need to display the respective data into the form input field. • First we will pass the unique identifier 'ID' as the GET request parameter through the URL to the update page. E.g<a href='update.php?id=4'>Update</a> • Then use this GET request parameter to fetch and display the relative record into the input fields. Now we are going to display the following table record .

How to display the record into text input fields?

First we will fetch the record using incoming ID . <?php $conn = mysqli_connect('localhost', 'root', '', 'myfirstdatabase'); $id = $_GET['id']; //Geting the id value from URL parameter id $sql_query = mysqli_query($conn,"SELECT ! FROM users WHERE id='".$id."' "); $fetch_result = mysqli_fetch_assoc($sql_query); ?> <form action="submitupdate.php" method="post"> ID:<input type="text" value="<?php echo $fetch_result['id']; ?>" name="id"><br> First Name:<input type="text" value="<?php echo $fetch_result['first_name']; ?>" name="first_name"><br> Last name:<input type="text" value="<?php echo $fetch_result['last_name']; ?>" name="last_name"><br> Password:<input type="password" value="<?php echo $fetch_result['password']; ?>" name="password"><br> <input type="submit" name="submit" value="Update"> </form>

Sample form with content Display

Unique ID :
First Name:
Last Name:

Submitting the updated form data using PHP

<?php $conn = mysqli_connect('localhost', 'root', '', 'myfirstdatabase'); $id = $_POST['id']; //Getting the id value from POST parameter id $first_name = $_POST['first_name']; $last_name = $_POST['last_name']; $password = $_POST['password']; $sql_query = mysqli_query($conn,"UPDATE users SET first_name='".$first_name."',last_name='".$last_name."', password='".$password."' WHERE id='".$id."' "); header("location: show.php"); // redirect to the main page after submitting update ?>
Chapter Next »