try {
$results = $db->prepare("INSERT INTO Form_Data
(first_name, last_name, email, gender, comment)
VALUES
(?,?,?,?,?)
");
$results->bindParam(1,$fname,PDO::PARAM_STR);
$results->bindParam(2,$lname,PDO::PARAM_STR);
$results->bindParam(3,$email);
$results->bindParam(4,$gender);
$results->bindParam(5,$comment,PDO::PARAM_STR);
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$results->execute();
}
} catch (Exceptions $e) {
echo "Unable to insert data";
exit;
}
I want to add form data to a database using, SQL, PHP and PDO.
Do I need to use the if($_SERVER["REQUEST_METHOD] == "POST")
statement so that every time someone submits the form I can collect the data into the database? Or can I just use $results->execute();
code block once ? And why?