I am trying to make a registration and login form for my college assignment but the registration form doesn't seem detect a password in the password field, it always throws the 'A password is needed' error, which is what it should do if nothing is entered into the field.
Here's the code: server.php:
<?php
$username = '';
$email = '';
$errors = array();
//connect to the database
$db = mysql_connect('localhost', 'root', '', 'registration');
//if the register button is clicked
if (isset($_POST['register'])) {
$username = mysql_real_escape_string($_POST['username']);
$email = mysql_real_escape_string($_POST['email']);
$password_1 = mysql_real_escape_string($_POST['password_1']);
$password_2 = mysql_real_escape_string($_POST['password_2']);
//ensure that form fields are filled properly
if (empty($username)) {
array_push($errors, 'Username is required');
}
if (empty($email)) {
array_push($errors, 'An Email is required');
}
if (empty($password_1)) {
array_push($errors, 'A password is required');
}
if ($password_1 != $password_2) {
array_push($errors, 'The two passwords do not match');
}
if (count($errors) == 0) {
$password = md5($password_1);
$sql = "INSERT INTO users (username, email, password)
VALUES ('$username', '$email', '$password')";
mysql_query($db, $sql);
}
}
?>
register.php:
<?php include('server.php'); ?>
<!DOCTYPE html>
<html>
<head>
<title>User Registration using PHP and MySQL</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div class="header">
<h2>Register</h2>
</div>
<form method="post" action="register.php">
<?php include('errors.php'); ?>
<div class="input-group">
<label>Username</label>
<input type="text" name="username" value="<?php echo "$username";?>">
</div>
<div class="input-group">
<label>Email</label>
<input type="text" name="email" value="<?php echo "$email";?>">
</div>
<div class="input-group">
<label>Password</label>
<input type="password" name="password">
</div>
<div class="input-group">
<label>Confirm password</label>
<input type="password" name="password">
</div>
<div class="input-group">
<button type="submit" name="register" class="btn">Register</button>
</div>
<p>
Already have an account? <a href="login.php">Sign in</a>
</p>
</form>
</body>
</html>