How to Update Data in mySql Database using PHP

How to Update Data in mySql Database using PHP

Today I will explain how to edit/update data or records in database using PHP  and mySql

This topic is very important part of any web developer at beginner to advance level. I know most of people recommended mysql database because it is free and easy to understand. This post is simple edit/update data or records in database later I will explain how to delete data or records from database using php and mysql.

In this post, following file are required.


Follow These Steps.

Step 3.   Open view_stu.php File
      Go to where xampp is installed and open htdoc folder then go to tutorials  folder and open view_stu.php and some changes Code shown below

       <th>Action</th>

       placed at <thead> section and


       <td><a href='edit_stu.php?id=".$stu_id."'>Update</a></td>

       placed at echo section after that your view_stu.php look like shown below

      <table align="center" class="table " border="1">
       <thead>
        <tr style="background-color: gray; color: white;">
          <th>S.No</th>
          <th>Name</th>
          <th>Gender</th>
          <th>Email</th>
          <th>Phone No</th>
          <th>Address</th>
          <th>Action</th>
        </tr>
      </thead>
      <tbody>
       
       <?php

           include_once('config.php');

           $data="select * from students";
           $data = $conn->query($data);
     
           while ($row = $data->fetch_array())
          {

            $stu_id            = $row['stu_id'];
            $stu_name       = $row['stu_name'];
            $stu_email       = $row['stu_email'];
            $stu_gender     = $row['stu_gender'];
            $stu_phone      = $row['stu_phone'];
            $stu_address    = $row['stu_address'];

            echo("
                     <tr>
                      <td>$stu_id</td>
                      <td>$stu_name</td>
                      <td>$stu_gender</td>
                      <td>$stu_email</td>
                      <td>$stu_phone</td>
                      <td>$stu_address</td>
                      <td><a href='edit_stu.php?id=".$stu_id."'>Update</a></td>
                    </tr>
                 ");
           }

         ?>

      </tbody>
      </table>

Step 4.   Now, go to browser and type localhost/tutorials/view_stu.php, screen shown below


Update Data mySql using PHP

Step 5.   Create Edit File
      Go to where xampp is installed and open htdoc folder then go to tutorials  folder and create a new file for example, edit_stu.php , Code shown below


           <?php

              include_once('config.php');

             $id       = $_GET['id'];
             $sql      = "select * from students where stu_id = '$id'";
             $result  = mysqli_query($conn, $sql);
             $row     = mysqli_fetch_array($result);

             if($row['stu_gender']=='male')
               {
                 $male='checked';
                 $female='';
               }
             elseif ($row['stu_gender']=='female') 
               {
                 $male='';
                 $female='checked';
               }

           ?>

           <!DOCTYPE html>
           <html lang="en">
             <head>
               <title>Edit Students Records</title> 
             </head>
             <body>
               <form action="update_stu.php" method="post">
               <input type="hidden" name="id" id="id" value="<?php echo $row['stu_id']; ?>" />
                 <table align="center" border="1">
                   <tbody>
                     <tr>
                       <td>Name</td>
                       <td><input name="name" placeholder="Name" type="text" required value="<?php echo $row['stu_name']; ?>"></td>
                     </tr>
                     <tr>
                       <td>Gender</td>
                       <td><input name="gender" type="radio" value="male" <?php echo $male; ?> >Male
                            <input name="gender" type="radio" value="female" <?php echo $female; ?> >Female</td>
                     </tr>
                     <tr>
                       <td>Email Id.</td>
                       <td><input name="email" id="email" placeholder="Email Id."  type="text" required value="<?php echo $row['stu_email']; ?>"></td>
                     </tr>
                     <tr>
                       <td>Phone No.</td>
                       <td><input name="phone" id="phone" placeholder="Phone No." type="text" maxlength="10" required value="<?php echo $row['stu_phone']; ?>"></td>
                     </tr>
                     <tr>
                       <td>Address:</td>
                       <td><textarea name="address" placeholder="Address" rows="5"><?php echo $row['stu_address']; ?></textarea></td>
                     </tr>
                     <tr>
                       <td></td>
                       <td><input type="submit" name="submit" value="Submit"></td>
                      </tr>
                  </tbody>
               </table>
            </form>
         </body>
      </html>

Now, go to browser and type localhost/tutorials/view_stu.php, then click update link, screen shown below


Update Data mySql using PHP

Step 6.   Create Update File
      Go to where xampp is installed and open htdoc folder then go to tutorials  folder and create a new file for example, update_stu.php , Code shown below


           <?php
               
                include_once('config.php');
      
$stu_id          = $_POST['id'];
$stu_name     = $_POST['name'];
$stu_gender   = $_POST['gender'];
$stu_email     = $_POST['email'];
$stu_phone    = $_POST['phone' ];
$stu_address  = $_POST['address'];

$sql = "UPDATE students SET stu_name='$stu_name', stu_gender='$stu_gender', stu_email='$stu_email', stu_phone='$stu_phone', stu_address= '$stu_address' where stu_id='$stu_id'";
$result = mysqli_query($conn, $sql);

if(isset($result))
{
                    echo "<script>alert('Do you want to update records?');window.location='view_stu.php?p=1';</script>";
}
else
{
                    echo "Updated not completed.".mysqli_error($conn);die;
}

          ?>

Step 7.   Now, go to browser and type localhost/tutorials/view_stu.php, screen shown below


Update Data mySql using PHP

Step 8.  Now click update link, for example, I will update s.no 8 whose name is sneha, screen shown below


Update Data mySql using PHP

Step 9.   Now changes some values, for example i will changes name like sneha to suman also changes email, phone no and address, screen shown below


Update Data mySql using PHP


then click submit button, screen shown below


Update Data mySql using PHP

You can easily seen that data are changed in table also you can check in database table, go to phpmyadmin(see More How to Insert data in Database using php and mysql), screen shown below


Update Data mySql using PHP


              

                   I hope, it helped you to understand, how to edit/update data in database using PHP and mySql......Thanks

About MSK TUTORIAL

Hi...I am Md Sahjad Karim and I am Engineering Graduate (B.E in Computer Science and Engineering) working as a Web Developer and write Blog Articles. http://www.msktutorial.com/ was launched on july 2016 to provide free online hints, tips, guides and tutorials related to technology(PHP, CodeIgniter, WordPress, Blogger etc). My primary goal is to provide the good, quality and easily to understand the articles to readers around the world.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment

Thanks For Visiting Blog...