How to update query in PHP MySQL?

Today, We want to share with you update query in mysql.In this post we will show you update multiple columns in mysql, hear for how to update data in php using form mysqli we will give you demo and example for implement.In this post, we will learn about Bootstrap Toggle switch with ajax update to mysql in PHP with an example.

Update Data in MySQL Using PHP

To update a data that already exist in the mysql database, simple UPDATE statement is used.

In the below easy to best example we update the players data from MySQL database.

we used 2 file for update data

  • database.php– To connecting database.
  • update.php– TO retrieve data from database with a update option.
  • update-players.php– TO update data from database.
Read Also:  How to Insert Data Into MySQL Database Table Using PHP?

database.php

<?php
$url='YourHostname';
$username='mainusername';
$password='df78pakainfo#$%';
$conn=mysqli_connect($url,$username,$password,"crud");f(!$conn){
 die('Could not Connect My Sql:' .mysql_error());
}
?>

update.php

<?php
include_once 'database.php';
$result = mysqli_query($conn,"SELECT * FROM players");
?>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="style.css">
<title>Delete players data</title>
</head>
<body>
<table>
<tr>
<td>Players Id</td>
<td>Player Name</td>
<td>Player Ranking</td>
<td>Player City</td>
<td>Player Email id</td>
<td>Action</td>
</tr>
<?php
$i=0;
while($row = mysqli_fetch_array($result)) {
if($i%2==0)
$dynamic_add_class_css="even";
else
$dynamic_add_class_css="odd";
?>
<tr class="<?php if(isset($dynamic_add_class_css)) echo $dynamic_add_class_css;?>">
<td><?php echo $row["playerid"]; ?></td>
<td><?php echo $row["playername"]; ?></td>
<td><?php echo $row["player_ranking"]; ?></td>
<td><?php echo $row["player_city"]; ?></td>
<td><?php echo $row["email"]; ?></td>
<td><a href="update-players.php?playerid=<?php echo $row["playerid"]; ?>">Update</a></td>
</tr>
<?php
$i++;
}
?>
</table>
</body>
</html>

update-players.php

<?php
include_once 'database.php';
if(count($_POST)>0) {
mysqli_query($conn,"UPDATE players set playerid='" . $_POST['playerid'] . "', playername='" . $_POST['playername'] . "', player_ranking='" . $_POST['player_ranking'] . "', player_city='" . $_POST['player_city'] . "' ,email='" . $_POST['email'] . "' WHERE playerid='" . $_POST['playerid'] . "'");
$message = "Record Modified Successfully";
}
$result = mysqli_query($conn,"SELECT * FROM players WHERE playerid='" . $_GET['playerid'] . "'");
$row= mysqli_fetch_array($result);
?>
<html>
<head>
<title>Update Players Data</title>
</head>
<body>
<form name="frmUser" method="post" action="">
<div><?php if(isset($message)) { echo $message; } ?>
</div>
<div style="padding-bottom:5px;">
<a href="retrieve.php">Players List</a>
</div>
Username: <br>
<input type="hidden" name="playerid" class="txtPlayerinfo" value="<?php echo $row['playerid']; ?>">
<input type="text" name="playerid"  value="<?php echo $row['playerid']; ?>">
<br>
First Name: <br>
<input type="text" name="playername" class="txtPlayerinfo" value="<?php echo $row['playername']; ?>">
<br>
Last Name :<br>
<input type="text" name="player_ranking" class="txtPlayerinfo" value="<?php echo $row['player_ranking']; ?>">
<br>
City:<br>
<input type="text" name="player_city" class="txtPlayerinfo" value="<?php echo $row['player_city']; ?>">
<br>
Email:<br>
<input type="text" name="email" class="txtPlayerinfo" value="<?php echo $row['email']; ?>">
<br>
<input type="submit" name="submit" value="Submit" class="buttom">

</form>
</body>
</html>

I hope you get an idea about dynamic update query in php.
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.