<!DOCTYPE html>
<html lang = "en">
<head>
<meta charset = "UTF-8">
<meta name = "viewport" content = "width=device-width, initial-scale=1.0">
<meta name = "description" content = "PHP basics">
<title>MySQL database access in PHP</title>
</head>
<body>
<?php function form($firstName = "", $lastName = "", $profession = "", $email = "", $mailingChecked = false, $gender = "", $education = "")
{ ?>
<form action = "" method = "post">
<div>
<p>First name:</p>
<input name = "firstName" value = "<?= $firstName; ?>"><br>
<p>Last name:</p>
<input name = "lastName" value = "<?= $lastName; ?>"><br>
<p>Profession:</p>
<input name = "profession" value = "<?= $profession; ?>"><br>
<p>Email address:</p>
<input name = "email" value = "<?= $email; ?>"><br>
<p>Gender:</p>
<input type = "radio" name = "gender" value = "Male" <?php if ($gender === "Male") echo "checked"; ?>><label for = "Male">Male</label><br>
<input type = "radio" name = "gender" value = "Female" <?php if ($gender === "Female") echo "checked"; ?>><label for = "Female">Female</label><br>
<p>Education:</p>
<select name = "education">
<option value = "">-- Select --</option>
<option value = "High School" <?php if ($education === "High School") echo "selected"; ?>>High School</option>
<option value = "Bachelor's" <?php if ($education === "Bachelor's") echo "selected"; ?>>Bachelor's</option>
<option value = "Master's" <?php if ($education === "Master's") echo "selected"; ?>>Master's</option>
<option value = "PhD" <?php if ($education === "PhD") echo "selected"; ?>>PhD</option>
</select><br><br>
<input type = "checkbox" name = "mailing" <?php if ($mailingChecked) echo "checked"; ?>>I want to receive commercial information<br><br>
<input type = "submit" value = "Submit" name = "submit">
</div>
</form>
<?php } ?>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "dbtest";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn -> connect_error)
die("<p style='color: red;'>Connection failed: " . $conn -> connect_error . "</p>");
$conn -> query("CREATE TABLE IF NOT EXISTS users (
id INT AUTO_INCREMENT PRIMARY KEY,
firstName VARCHAR(50),
lastName VARCHAR(50),
profession VARCHAR(50),
email VARCHAR(100),
gender VARCHAR(10),
education VARCHAR(20),
mailing BOOLEAN
)");
if (isset($_POST["submit"])) {
$mailing = isset($_POST["mailing"]);
$gender = isset($_POST["gender"]) ? $_POST["gender"] : "";
$education = isset($_POST["education"]) ? $_POST["education"] : "";
form($_POST["firstName"], $_POST["lastName"], $_POST["profession"], $_POST["email"], $mailing, $gender, $education);
if (empty($_POST["firstName"]) || empty($_POST["lastName"]) ||
empty($_POST["profession"]) || empty($_POST["email"]) ||
empty($gender) || empty($education)) {
echo "<p style='color: red;'>You must fill in all fields!</p>";
} else {
echo "<p>Thank you for filling out the form!</p>";
$stmt = $conn -> prepare("INSERT INTO users (firstName, lastName, profession, email, gender, education, mailing) VALUES (?, ?, ?, ?, ?, ?, ?)");
$stmt -> bind_param("ssssssi", $_POST["firstName"], $_POST["lastName"], $_POST["profession"], $_POST["email"], $gender, $education, $mailing);
$stmt -> execute();
$stmt -> close();
?>
<ul>
<li>First name: <b><?= trim($_POST["firstName"]); ?></b></li>
<li>Last name: <b><?= trim($_POST["lastName"]); ?></b></li>
<li>Profession: <b><?= trim($_POST["profession"]); ?></b></li>
<li>Email address: <b><?= trim($_POST["email"]); ?></b></li>
<li>Gender: <b><?= $gender; ?></b></li>
<li>Education: <b><?= $education; ?></b></li>
<?php
if ($mailing)
echo "<li>You want to receive commercial information.</li>";
else
echo "<li>You do not want to receive commercial information.</li>";
?>
</ul>
<?php
}
} else
form();
$conn -> close();
?>
</body>
</html>