How can text fields be created in PHP that are linked to a database for user input and updates?

To create text fields in PHP linked to a database for user input and updates, you can use HTML form elements combined with PHP to handle the database interactions. You will need to establish a database connection, retrieve and display the existing data in the text fields, and update the database with the new user input upon form submission.

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

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

// Retrieve existing data from the database
$sql = "SELECT * FROM table_name WHERE id = 1";
$result = $conn->query($sql);
$row = $result->fetch_assoc();

// Display form with text field linked to database
echo "<form method='post'>";
echo "<input type='text' name='data' value='" . $row['data_column'] . "'>";
echo "<input type='submit' name='submit' value='Submit'>";
echo "</form>";

// Update database with user input
if(isset($_POST['submit'])) {
    $newData = $_POST['data'];
    $updateSql = "UPDATE table_name SET data_column = '$newData' WHERE id = 1";
    $conn->query($updateSql);
    echo "Data updated successfully!";
}

$conn->close();
?>