How can PHP be used to store user input from multiple input fields into a database?
To store user input from multiple input fields into a database using PHP, you can first retrieve the input values using $_POST or $_GET, sanitize the data to prevent SQL injection, and then insert the values into the database using SQL queries.
<?php
// Retrieve user input from multiple input fields
$field1 = $_POST['field1'];
$field2 = $_POST['field2'];
$field3 = $_POST['field3'];
// Sanitize the input to prevent SQL injection
$field1 = mysqli_real_escape_string($conn, $field1);
$field2 = mysqli_real_escape_string($conn, $field2);
$field3 = mysqli_real_escape_string($conn, $field3);
// Insert the values into the database
$sql = "INSERT INTO table_name (field1, field2, field3) VALUES ('$field1', '$field2', '$field3')";
if (mysqli_query($conn, $sql)) {
echo "Record inserted successfully";
} else {
echo "Error inserting record: " . mysqli_error($conn);
}
mysqli_close($conn);
?>