Tags
Asked 3 years ago
17 Jun 2021
Views 258
Roscoe

Roscoe posted

Updating information on a MySQL database from a PHP site

Updating information on a MySQL database from a PHP site
denyy

denyy
answered Apr 28 '23 00:00

To update information on a MySQL database from a PHP site, you can follow these steps:

Connect to the database:
You need to establish a connection between your PHP script and the MySQL database. This can be done using the mysqli_connect() function, which takes four parameters: the database server host, username, password, and database name.



<?php
$servername = "localhost";
$username = "yourusername";
$password = "yourpassword";
$dbname = "yourdatabasename";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);

// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>

Retrieve data to be updated:
You need to retrieve the data that you want to update from the database. This can be done using an SQL SELECT statement.



<?php
// Retrieve data
$id = $_GET['id'];
$sql = "SELECT * FROM mytable WHERE id = $id";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
    while ($row = mysqli_fetch_assoc($result)) {
        $name = $row['name'];
        $email = $row['email'];
    }
} else {
    echo "0 results";
}
?>

In the above code, we first retrieve the id of the data to be updated using the $_GET variable. We then use an SQL SELECT statement to retrieve the data from the database. If the query returns any rows, we use a while loop to loop through the result set and retrieve the name and email fields.

Create a form:
You need to create a form that will allow users to update the data. You can use HTML to create the form and pre-populate the form fields with the retrieved data.



<form method="post" action="update.php">
    <label for="name">Name:</label>
    <input type="text" id="name" name="name" value="<?php echo $name; ?>"><br><br>
    <label for="email">Email:</label>
    <input type="email" id="email" name="email" value="<?php echo $email; ?>"><br><br>
    <input type="hidden" name="id" value="<?php echo $id; ?>">
    <input type="submit" value="Update">
</form>

In the above code, we create a form that has two fields for the name and email, and we pre-populate the fields with the retrieved data. We also include a hidden field for the id, so that we can pass it to the update script.

Handle the form submission:
You need to handle the form submission using PHP. When the user submits the form, the data is sent to a PHP script that will update the database.



<?php
// Check if the form was submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Get the form data
    $id = $_POST["id"];
    $name = $_POST["name"];
    $email = $_POST["email"];

    // Update the database
    $sql = "UPDATE mytable SET name='$name', email='$email' WHERE id=$id";
    if (mysqli_query($conn, $sql)) {
        echo "Record updated successfully";
    } else {
        echo "Error updating record: " . mysqli_error($conn);
    }
Post Answer