How To Fetch And Update Query In MySQL - PHP


In this tutorial, we learn to update query in MySQL. many time need to  Fetch and update the data from the MySQL database in PHP. This example demonstrates to you how you can easily fetch data and update query in SQL server, the data from the MySQL database in PHP.

Here, we will show you a simple and easy way on how to fetch and update query in MySQL database in PHP.

How To Fetch And Update Query In MySQL - PHP


How to Fetch and update query in MySQL in PHP

How to Update and Delete Record Fro...
How to Update and Delete Record From MySQL Database Table using Command Prompt | 2020

Just follow the below many steps and fetch and update query in SQL server using MySQL database PHP code:

  • Connect to MySQL database
  • Fetch data from the database
  • Update data from database
1. Connect To MySQL Database

In this step, you will create a file name db.php and update the below code in your file.

<?php
    $host='localhost';
    $username='root';
    $password='';
    $dbname = "my_db";
    $conn=mysqli_connect($host,$username,$password,"$dbname");
    if(!$conn)
        {
          die('Could not Connect MySql Server:' .mysql_error());
        }
?>
2. Fetch Data From The Database

In this step, we will fetch data from the MySQL database in PHP and display data in an HTML table. So you can create a new file and update the below code into your file.

The below code is used to retrieve data from database in PHP using MySQL database in PHP. Additionally, we will display the fetch data in an HTML table.

<!DOCTYPE html>
<html lang="en">
   <head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
      <title>PHP code to retrieve and update data from MySQL database - phpcodingstuff.com</title>
      <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto|Varela+Round|Open+Sans">
      <link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
      <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
      <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
      <style type="text/css">
         .bs-example{
         margin: 20px;
         }
      </style>
      <script type="text/javascript">
         $(document).ready(function(){
         $('[data-toggle="tooltip"]').tooltip();   
         });
      </script>
   </head>
   <body>
      <div class="bs-example">
         <div class="container">
          <div class="row">PHP code to retrieve and update data from MySQL database - phpcodingstuff.com</div>
            <div class="row">
               <div class="col-md-12">
                  <div class="page-header clearfix">
                     <h2 class="pull-left">Users List</h2>
                  </div>
                  <?php
                     include_once 'db.php';
                     $result = mysqli_query($conn,"SELECT * FROM users");
                     ?>
                  <?php
                     if (mysqli_num_rows($result) > 0) {
                     ?>
                  <table class='table table-bordered table-striped'>
                     <tr>
                        <td>Name</td>
                        <td>Email id</td>
                        <td>Mobile</td>
                        <td>Action</td>
                     </tr>
                     <?php
                        $i=0;
                        while($row = mysqli_fetch_array($result)) {
                        ?>
                     <tr>
                        <td><?php echo $row["name"]; ?></td>
                        <td><?php echo $row["email"]; ?></td>
                        <td><?php echo $row["mobile"]; ?></td>
                        <td><a href="update-action.php?userid=<?php echo $row["id"]; ?>">Update</a></td>
                     </tr>
                     <?php
                        $i++;
                        }
                        ?>
                  </table>
                  <?php
                     }
                     else{
                     echo "No result found";
                     }
                     ?>
               </div>
            </div>
         </div>
      </div>
   </body>
</html>
3. Update Data From Database

In this step, we will update query in SQL server database using PHP. So you can create a new file and update the below code into your file.

The below code is used to update data from the MySQL database in PHP.

<?php
// Include database connection file
require_once "connection.php";
if(count($_POST)>0) {
mysqli_query($conn,"UPDATE users set  name='" . $_POST['name'] . "', mobile='" . $_POST['mobile'] . "' ,email='" . $_POST['email'] . "' WHERE id='" . $_POST['id'] . "'");
$message = "Record Modified Successfully";
}
$result = mysqli_query($conn,"SELECT * FROM users WHERE id='" . $_GET['userid'] . "'");
$row= mysqli_fetch_array($result);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>update query in MySQL  - phpcodingstuff.com</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.css">
<style type="text/css">
.wrapper{
width: 500px;
margin: 0 auto;
}
</style>
</head>
<body>
<div class="wrapper">
<div class="container-fluid">
<div class="row">
<div class="col-md-12">
<div class="page-header">
<h2>Update Record</h2>
</div>
<p>Please edit the input values and submit to update the record.</p>
<form action="<?php echo htmlspecialchars(basename($_SERVER['REQUEST_URI'])); ?>" method="post">
<div class="form-group">
<label>Name</label>
<input type="text" name="name" class="form-control" value="<?php echo $row["name"]; ?>">
</div>
<div class="form-group ">
<label>Email</label>
<input type="email" name="email" class="form-control" value="<?php echo $row["email"]; ?>">
</div>
<div class="form-group">
<label>Mobile</label>
<input type="mobile" name="mobile" class="form-control" value="<?php echo $row["mobile"]; ?>">
</div>
<input type="hidden" name="id" value="<?php echo $row["id"]; ?>"/>
<input type="submit" class="btn btn-primary" value="Submit">
<a href="users.php" class="btn btn-default">Cancel</a>
</form>
</div>
</div>        
</div>
</div>
</body>
</html>
Conclusion

We learn update query in mysql database in PHP. Here you have learned how to fetch and update data in MySQL using PHP code.

This tutorial, very basic and easy example of fetching and updating the data into a MySQL database using a PHP script.


I hope it can help you...

Leave a Reply

Your privacy will not be published. Required fields are marked *

We'll share your Website Only Trusted.!!

close