how to update data from database in mysql php?

Today, We want to share with you update query in mysql php.In this post we will show you update multiple columns query in php, hear for dynamic update query in php we will give you demo and example for implement.In this post, we will learn about PHP crud Mysqli Select Insert Update and Delete query with an example.

how to fetch and update data from database in php?

The update mysql database keyword is basically used to modify/change or edit the existing some records in the database table.
PHP MySQL Update Query : Syntax

UPDATE table_name SET column1=value1,column2=value2... WHERE column = value 

simple you can run or execure on your server following full source code to understand update operation. You need to provide an players ID to update an players salary.

Read Also:  Mysql update query statement with example

index.php

<html>
   
   <head>
      <title>Update a Record in MySQL Database</title>
   </head>
   
   <body>
      <?php
         if(isset($_POST['update'])) {
            $dbhost = 'yourhostname';
            $dbuser = 'mainusername';
            $dbpass = 'DSF7#$57f7ff';
            
            $conn = mysql_connect($dbhost, $dbuser, $dbpass);
            
            if(! $conn ) {
               die('Could not connect: ' . mysql_error());
            }
            
            $player_id = $_POST['player_id'];
            $player_salary = $_POST['player_salary'];
            
            $sql = "UPDATE players ". "SET player_salary = $player_salary ". 
               "WHERE player_id = $player_id" ;
            mysql_select_db('players_v1');
            $retval = mysql_query( $sql, $conn );
            
            if(! $retval ) {
               die('Could not update data: ' . mysql_error());
            }
            echo "Updated data successfully\n";
            
            mysql_close($conn);
         }else {
            ?>
               <form method = "post" action = "<?php $_PHP_SELF ?>">
                  <table width = "400" border =" 0" cellspacing = "1" 
                     cellpadding = "2">
                  
                     <tr>
                        <td width = "100">Players ID</td>
                        <td><input name = "player_id" type = "text" 
                           id = "player_id"></td>
                     </tr>
                  
                     <tr>
                        <td width = "100">Players Salary</td>
                        <td><input name = "player_salary" type = "text" 
                           id = "player_salary"></td>
                     </tr>
                  
                     <tr>
                        <td width = "100"> </td>
                        <td> </td>
                     </tr>
                  
                     <tr>
                        <td width = "100"> </td>
                        <td>
                           <input name = "update" type = "submit" 
                              id = "update" value = "Update">
                        </td>
                     </tr>
                  
                  </table>
               </form>
            <?php
         }
      ?>
      
   </body>
</html>

This example updates some data in the “playerInfo” table

 <?php
//connect database 
$con=mysqli_connect("localhost","root","","Players") or die(mysqli_error());

//update values of playerInfo table
$data="UPDATE playerInfo SET name='rohitsharma',mobile=98256062*** WHERE email='[email protected]'";
mysqli_query($con,$data);			
?>

I hope you get an idea about how to update data in php using form mysqli.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.