Feedback
if ($_SERVER['REQUEST_METHOD'] === 'POST') { # This is a POST request - so lets handle the POST
$user = isset($_POST['user']) ? $_POST['user'] : ''; # Set $user to the posted value if it exists, otherwise use ''
$email = isset($_POST['email']) ? $_POST['email'] : '';
$comments = isset($_POST['comments']) ? $_POST['comments'] : '';
$spam = isset($_POST['spam']) ? $_POST['spam'] : '';
if (empty($user) || empty($email) || empty($comments) || empty($spam)) { # one of the fields was not supplied or left blank
echo <<Whoops!
Please fill in all fields.
Click here to go bac to the feedback page.
EOVALIDATION;
} else { # all is good - we passed the empty check on all fields so add to the db
$db = mysql_connect("localhost");
if (mysql_errno()) die mysql_error();
mysql_select_db("sample", $db);
if (mysql_errno()) die mysql_error();
$addfeedback = sprintf("INSERT INTO feedback (user, email, spam, comments)
VALUES ('%s','%s', '%s','%s')", # use sprintf to separate out the user supplied data
mysql_real_escape_string($user), # and escape the data to keep the SQL safe from injections
mysql_real_escape_string($email),
mysql_real_escape_string($spam),
mysql_real_escape_string($comments));
;
$result = mysql_query($addfeedback) or die("Cannot query the database.
" . mysql_error());
echo <<
Click here to go back to the feedback page.