In what forum category should a question about basic PHP form handling and database interaction be posted for appropriate assistance and feedback?
Issue: I am having trouble with basic PHP form handling and database interaction. I need assistance in capturing form data and storing it in a database using PHP. Solution: To handle form data and interact with a database using PHP, you can follow these steps: 1. Create a form in HTML with appropriate input fields. 2. Use PHP to capture the form data using $_POST or $_GET depending on the form method. 3. Establish a connection to your database using mysqli or PDO. 4. Insert the form data into the database using prepared statements to prevent SQL injection. PHP Code Snippet:
<?php
// Assuming form method is POST
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Capture form data
$name = $_POST['name'];
$email = $_POST['email'];
// Establish database connection
$conn = new mysqli("localhost", "username", "password", "database_name");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Prepare and execute SQL statement to insert data
$stmt = $conn->prepare("INSERT INTO table_name (name, email) VALUES (?, ?)");
$stmt->bind_param("ss", $name, $email);
if ($stmt->execute()) {
echo "Data inserted successfully";
} else {
echo "Error: " . $conn->error;
}
// Close statement and connection
$stmt->close();
$conn->close();
}
?>