How can PHP be used to display and edit data from MySQL tables in a web application?

To display and edit data from MySQL tables in a web application using PHP, you can establish a connection to the MySQL database, query the database for the desired data, display the data in a table format on a webpage, and provide options for editing the data through forms or input fields. You can then update the database with the edited data by executing SQL queries based on user input.

<?php
// Establish connection to MySQL database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";

$conn = new mysqli($servername, $username, $password, $dbname);

// Query the database for data
$sql = "SELECT * FROM table_name";
$result = $conn->query($sql);

// Display data in a table format
echo "<table>";
while($row = $result->fetch_assoc()) {
    echo "<tr>";
    echo "<td>" . $row['column1'] . "</td>";
    echo "<td>" . $row['column2'] . "</td>";
    // Add more columns as needed
    echo "</tr>";
}
echo "</table>";

// Provide options for editing data
echo "<form action='update_data.php' method='post'>";
echo "<input type='text' name='column1'>";
echo "<input type='text' name='column2'>";
// Add more input fields as needed
echo "<input type='submit' value='Update'>";
echo "</form>";

// Update database with edited data
if($_SERVER['REQUEST_METHOD'] == 'POST') {
    $column1 = $_POST['column1'];
    $column2 = $_POST['column2'];
    // Add more variables as needed

    $update_sql = "UPDATE table_name SET column1='$column1', column2='$column2' WHERE id=1";
    $conn->query($update_sql);
}
?>