Update data in PHP

To update data in database using php code we have to fetch data from database and then display it in the textbox so that we can modify it according to our requirements and then run update query so that the updated data stored in database. To do this see the below code for your help.

Step1: update_form.php code
 <?php

    $link = mysqli_connect("localhost", "root", "root", "employee_db");

     // Check connection

    if($link === false){

        die("ERROR: Could not connect. " . mysqli_connect_error());

    }

    $email_address = mysqli_real_escape_string($link, $_POST['emailaddress']);
    $sql = "SELECT * FROM employee WHERE email_address='$email_address'";

    if($result = mysqli_query($link, $sql)){

        if(mysqli_num_rows($result) > 0){
while($row = mysqli_fetch_array($result)){
  $firstName=$row['first_name'];
$lastName=$row['last_name'];
$emailAddress=$row['email_address'];
}
        }
    
           else{

            echo "No records matching your query were found.";

        }

    } else{

        echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);

    }  

    ?>
  
  
<html lang="en">

    <head>

    <meta charset="UTF-8">

    <title>Update Record Form</title>

    </head>

    <body>
            <form action="update.php" method="post">

        <p>

            <label for="firstName">First Name:</label>

            <input type="text" name="firstname" id="firstName" value="<?php  echo $firstName ?>">

        </p>

        <p>

            <label for="lastName">Last Name:</label>

            <input type="text" name="lastname" id="lastName" value="<?php  echo $lastName ?>">

        </p>

        <p>

<!--            <label for="emailAddress">Email Address:</label>-->

            <input type="hidden" name="email" id="emailAddress" value="<?php  echo $emailAddress ?> ">

        </p>

        <input type="submit" value="Submit">

    </form>
       
          </body>

    </html>  
 <?php
 mysqli_close($link);
 ?>

Step2: update.php code
    <?php

     $link = mysqli_connect("localhost", "root", "root", "employee_db");

    // Check connection

    if($link === false){

        die("ERROR: Could not connect. " . mysqli_connect_error());
    }

    $first_name = mysqli_real_escape_string($link, $_POST['firstname']);

    $last_name = mysqli_real_escape_string($link, $_POST['lastname']);

    $email_address = mysqli_real_escape_string($link, $_POST['email']);
    // Attempt update query execution

    $sql = "UPDATE employee SET first_name='$first_name', last_name='$last_name' WHERE email_address='$email_address'";

    if(mysqli_query($link, $sql)){

        echo "Records were updated successfully.";

    } else {

        echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);

    }

    // Close connection

    mysqli_close($link);

    ?>


Note: For any other problem you have to write in comments i will solve your problem.

Post a Comment

5 Comments

  1. Can you help us coding of send form in php,

    ReplyDelete
    Replies
    1. Tell me which type of send form...explain

      Delete
  2. Can you help us coding of send form in php,

    ReplyDelete
    Replies
    1. Ok wait i am sending you

      Delete
    2. see the below link for more details, if you still have some problems then tell me.

      http://p4programmingland.blogspot.in/2016/01/insert-data-into-database-using-php.html

      Delete