All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

How To Update Data In PHP Using Form

Last Updated : Mar 11, 2024

How To Update Data In PHP Using Form

In this article we will show you the solution of how to update data in PHP using form, with online applications, updating database data is frequently necessary. This may be done in PHP by creating a form that allows users to enter new values for particular database columns.

The procedure entails accessing to the database, obtaining information from the form, and using SQL to update the pertinent rows in the database.

These procedures make utilising a PHP form to update data in such a database simple. Now let us talk about the idea of updating data in PHP via a form.

Step By Step Guide On How To Update Data In PHP Using Form :-

HTML Code:

<!DOCTYPE html>
 <title>Update Record</title>
 <h2>Update Record</h2>
 <form action="update.php" method="post">
  <label for="id">ID:</label>
  <input type="text" name="id"><br><br>
  <label for="name">Name:</label>
  <input type="text" name="name"><br><br>
  <label for="email">Email:</label>
  <input type="email" name="email"><br><br>
  <input type="submit" value="Update">

PHP Code:

$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
 $id = $_POST['id'];
 $name = $_POST['name'];
 $email = $_POST['email'];
 $sql = "UPDATE users SET name='$name', email='$email' WHERE id='$id'";
 if (mysqli_query($conn, $sql)) {
     echo "Record updated successfully";
 } else {
     echo "Error updating record: " . mysqli_error($conn);
  1. As you can see, we use the form to update data in PHP by writing HTML and PHP code.
  2. ID, Name, and Email are the three input boxes on the HTML form (update.html).
  3. Because update.php is set as the form's action, the data from forms will be retrieved.
  4. The POST method is used to send the form's data to a update.php script after the user presses the "Update" button.
  5. The mysqli connect() function is used to establish a database connection at the beginning of the PHP code (update.php).
  6. The four parameters for this function are database name, server name, username, and password.
  7. The mysqli connect error() method is then used to determine whether the connection was successful.
  8. The script ends & shows the error message if the connection fails.
  9. The code uses the $_SERVER["REQUEST METHOD"] superglobal to determine whether the HTTP request method as POST if the connection gets successful.
  10. The code retrieves the ID, Name, & Email field data using their respective names from the $_POST superglobal if the request type is POST.
  11. In order to update the "users" database table called "users" with the new information, the code next creates a SQL UPDATE query.
  12. According to the ID value, the WHERE clause tells which row needs to be updated.
  13. The SQL statement is subsequently carried out by the code using the mysqli query() function.
  14. The script repeats the phrase "Record updated successfully" if the query is successful.
  15. The script uses the mysqli error() function to echo the error message if the query fails.
  16. The mysqli close() function is used by the code to close the database connection.

Conclusion :-

Thus, we have successfully acquired the concept of updating data in PHP via a form.

Also, we discovered that using a PHP form to update data in a database necessitates connecting to a database, obtaining the information from the form, and using SQL to update the pertinent database rows.

The code above shows how to use a PHP form to update data in a MySQL database.

It is simple to update data in a database using a PHP form if you adhere to the instructions provided in this code.

I hope this article on how to update data in PHP using form helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Ashish

Ashish is a dynamic and motivated individual with a passion of programming and an experienced programmer having 3+ years of experience in various languages like Java, Python, HTML, CSS, JavaScript, jQuery and various frameworks like Bootstrap

Follow Ashish On Linkedin 🡪