anantamu.com
PHP - Introduction
PHP - Setup
PHP - Syntax
PHP - String
PHP - constants
PHP - Operators
PHP - Superglobals
PHP Forms PHP - Forms
MySQL
MySQL - Create DB
MySQL - Update Data
MySQL - Delete Data


PHP MySQL UPDATE statement


The UPDATE statement is used to update data from table


UPDATE STATEMENT

UPDATE tableName SET columnName=value WHERE columnName=value

Displaying the records and action of update from Profile table.

<?php
$servername = "localhost";
$username1 = "root";
$password1 = "";
$dbname = "sample";
//server connection
$connection = mysqli_connect($servername, $username1, $password1, $dbname);
if (!$connection) {
die("Connection failed: " . mysqli_connect_error());
}
$sqlQuery='select * from profile';
$resultSql=mysqli_query($connection, $sqlQuery);
echo '<table>';
echo '<tr><td>Name</td><td>phonenumber</td><td>Action</td></tr>';

if (@mysqli_num_rows(@$resultSql) >= 1) {
// output data of each row
  while ($rowValue = mysqli_fetch_assoc($resultSql)) {
	  echo '<tr>';
	  echo '<td>'.$rowValue['Name'].'</td>';
	  echo  '<td>'.$rowValue['Address'].'</td>';
	  echo  '<td>'."<a href=profileUpdate.php?id=".$rowValue['id'].">Update</a>".'</td>';
	  echo '</tr>';

  }
}else{	
	echo '<tr><td>no records found</td><td></td></tr>';
}
echo '</table>';
mysqli_close($connection);
?> 
                    

When user click the update then this page will be opened.

Udpate the profile Details

 <html>
<head>
</head>
<body>
<?php
$servername = "localhost";
$username1 = "root";
$password1 = "";
$dbname = "sample";
//server connection
$connection = mysqli_connect($servername, $username1, $password1, $dbname);
if (!$connection) {
die("Connection failed: " . mysqli_connect_error());
}
$resultProfile = mysqli_query($connection, "SELECT * FROM profile   where id='" . $_GET['id'] . "'");
while ($rowProfile = mysqli_fetch_assoc($resultProfile)) {
?>
<form name='profile' method='post' action='updateProfileResult.php'>

Update

Name:<input type='text' name='name' value="<?php echo $rowProfile['Name']; ?>" > PhoneNumber:<input type='text' name='address' value="<?php echo $rowProfile['Address']; ?>" > <input type='hidden' name='id' value="<?php echo $rowProfile['id']; ?>" > <input type='submit' name='submit' value='submit' > </form> <?php } ?> </body> </html>

When user submit the this page updateProfileResult.php then excute the profile details:

<?php
$servername = "localhost";
$username1 = "root";
$password1 = "";
$dbname = "sample";
//server connection
$connection = mysqli_connect($servername, $username1, $password1, $dbname);
if (!$connection) {
die("Connection failed: " . mysqli_connect_error());
}
$sqlQuery="update  profile SET name ='".$_POST['name']."' , Address ='".$_POST['address']."' WHERE id = '" . $_POST['id'] . "' ";


$resultSql=mysqli_query($connection, $sqlQuery);

mysqli_close($connection);
?> 
                    

School