How can PHP be used to interact with a MySQL table for data input and updates?

To interact with a MySQL table for data input and updates using PHP, you can establish a connection to the database, write SQL queries to insert or update data, and execute those queries using PHP functions like mysqli_query().

<?php
// Establish connection to MySQL database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "dbname";
$conn = mysqli_connect($servername, $username, $password, $dbname);

// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

// Insert data into table
$sql = "INSERT INTO table_name (column1, column2) VALUES ('value1', 'value2')";
if (mysqli_query($conn, $sql)) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}

// Update data in table
$sql = "UPDATE table_name SET column1='new_value' WHERE id=1";
if (mysqli_query($conn, $sql)) {
    echo "Record updated successfully";
} else {
    echo "Error updating record: " . mysqli_error($conn);
}

// Close connection
mysqli_close($conn);
?>