I want to add extra fields in my registration form like date of birth, city, country, and marital status. I am new to PHP and PDO.
Here is my code:
PHP:
<?php
require 'core/init.php';
$general->logged_in_protect();
if (isset($_POST['submit'])) {
if (empty($_POST['username']) || empty($_POST['password']) || empty($_POST['email'])) {
$errors[] = 'All fields are required.';
} else {
if ($users->user_exists($_POST['username']) === true) {
$errors[] = 'That username already exists';
}
if (!ctype_alnum($_POST['username'])) {
$errors[] = 'Please enter a username with only alphabets and numbers';
}
if (strlen($_POST['password']) <6) {
$errors[] = 'Your password must be atleast 6 characters';
} else if (strlen($_POST['password']) >18) {
$errors[] = 'Your password cannot be more than 18 characters long';
}
if (filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) === false) {
$errors[] = 'Please enter a valid email address';
} else if ($users->email_exists($_POST['email']) === true) {
$errors[] = 'That email already exists.';
}
}
if (empty($errors) === true) {
$username = htmlentities($_POST['username']);
$password = $_POST['password'];
$email = htmlentities($_POST['email']);
$users->register($username, $password, $email);
header('Location: register.php?success');
exit();
}
}
HTML:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" href="css/style.css">
<title>Register</title>
</head>
<body>
<div class="nav-bar" style="box-shadow:0 0 5px 0 rgba(0, 0, 0, 0.4);">
<?php include 'includes/menu.php'; ?>
</div>
<!-- NAV BAR DIV closes here -->
<div id="main-wrap" style="box-shadow:0 0 5px 0 rgba(0, 0, 0, 0.4);">
<div id="container">
<h1>Register</h1>
<?php if (isset($_GET['success']) && empty($_GET['success'])) { echo 'Thank you for registering. Please check your email.'; } ?>
<form method="post" action="">
<h4>Username:</h4>
<input type="text" name="username" value="<?php if (isset($_POST['username'])) echo htmlentities($_POST['username']); ?>">
<h4>Password:</h4>
<input type="password" name="password" />
<h4>Email:</h4>
<input type="text" name="email" value="<?php if (isset($_POST['email'])) echo htmlentities($_POST['email']); ?>" />
<br>
<input type="submit" name="submit" value="Register" />
</form>
<?php if (empty($errors) === false){ echo '<p>' . implode('</p><p>', $errors) . '</p>'; } ?>
</div>
<!-- Container DIV closes here -->
</div>
<!-- Main Wrap DIV closes here -->
</body>
</html>
How do I add the extra fields?