I am making a signup form. Below is my code. My database name is "users" and has the following fields: id, name, email & password. When I submit form it echos "You've been signed up!" but nothing gets inserted to the database. I am new to php and SQL. If anyone could help me with why my form is not submitting data to database.
<?php
if (isset($_POST['submit'])) {
$error = "";
if (!$_POST['userName'])
$error .= "<br/>Please enter your name";
if (!$_POST['userEmail'])
$error .= "<br/>Please enter your email";
else if (!filter_var($_POST['userEmail'], FILTER_VALIDATE_EMAIL))
$error .= "<br/>Please enter a valid email address";
if (!$_POST['userPassword'])
$error .= "<br/>Please enter your password";
else {
if (strlen($_POST['userPassword']) < 8)
$error .= "<br/>Please enter a password with minimum 8 characters";
if (!preg_match('`[A-Z]`', $_POST['userPassword']))
$error .= "<br/>Please include a capital letter in your password";
}
if ($error)
echo "There were error(s) in your signup details: " . $error;
else {
$link = mysqli_connect("localhost", "username", "password", "database");
if (!$link) {
echo "Failed.";
} else {
$query = "SELECT * FROM `users` WHERE email='$_POST[userEmail])'";
$result = mysqli_query($link, $query);
$results = mysqli_num_rows($result);
if ($results)
echo "That email address already exists. Do you want to log in? ";
else {
$query = "INSERT INTO users (name, email, passsword) VALUES(
'$_POST[userName]', '$_POST[userEmail]', '$_POST[userPassword]')";
mysqli_query($link, $query);
echo "You've been signed up!";
}
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Sign Up</title>
</head>
<body>
<h1>Sign Up</h1>
<form method="post">
<label for="userName">Name</label>
<input type="text" id="userName" name="userName" placeholder="Name"/>
<label for="userEmail">Email address</label>
<input type="email" id="userEmail" name="userEmail" placeholder="Email"/>
<label for="userPassword">Password</label>
<input type="password" id="userPassword"
name="userPassword" placeholder="Password"/>
<input type="submit" id="submit" name="submit" placeholder="Sign Up"/>
</form>
</body>
</html>