Bootstrap FreeKB - PHP - SQL UPDATE statement in PHP
PHP - SQL UPDATE statement in PHP

Updated:   |  PHP articles

Using PHP to update data in our MySQL table requires at least 2 Web pages, one page that displays the current data and another page which updates the data.  Following is the minimal code needed for the HTML page which displays the current data.  Let's say this page is update_form.php.  Notice $data1 = $row['data1'];, and also value='$data1'.  This is what is necessary in order to display the current data from MySQL.

<?php
$con = mysqli_connect('ip_or_domain','username','password','database');
$select = "select * from tablename";
$query = mysqli_query($con, $select);

while ($row=mysqli_fetch_array($query)){
$data1 = $row['data1'];

echo "<form action='update.php' method='post'>";
echo "<insert type='text' name='data1' value='$data1'>";
echo "<insert type='submit'>";
echo "</form>";
}
?>

 

The following code is then used to update the data in MySQL.  Notice the previous code action='update.php'.  The following code needs to be saved as update.php.  Notice also the update_form.php has method='post'.  This is why our update.php has $_POST.

<?php
$con = mysqli_connect('ip_or_domain','username','password','database');
$update = "update tablename set columnname='$_POST['data1']'";
$processing = mysqli_query($con, $update);
$con->close();

echo "<a href='update_form.php'>Update complete.  Click here to return to previous page.</a>";
?>



Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter a67a8a in the box below so that we can be sure you are a human.