How to update MySQL data using PHP

Home > Search > How-to

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.

$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.

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

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

Add a Comment

We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.

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