What are some best practices for passing variables from an HTML form to a PHP script for table manipulation?

When passing variables from an HTML form to a PHP script for table manipulation, it is best practice to use the POST method to securely send the data. You can access the form data in the PHP script using the $_POST superglobal array. Sanitize and validate the input data to prevent SQL injection attacks or other security vulnerabilities. Finally, use prepared statements when interacting with a database to further secure the data manipulation process.

<?php
// Retrieve form data using POST method
$name = $_POST['name'];
$email = $_POST['email'];

// Sanitize and validate input data
$name = filter_var($name, FILTER_SANITIZE_STRING);
$email = filter_var($email, FILTER_VALIDATE_EMAIL);

// Connect to database and perform table manipulation
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";

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

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$stmt = $conn->prepare("INSERT INTO users (name, email) VALUES (?, ?)");
$stmt->bind_param("ss", $name, $email);

$stmt->execute();

echo "New record inserted successfully";

$stmt->close();
$conn->close();
?>