everything works,
except for the Insert statement (creatUser()) which returns no errors but the data doesn't get inserted.. all error handling checks out with the form, and the emailTaken
function also works.
What am i doing wrong?
form:
</form>
<form class="signup" action="includes/signUpManager.php" method="post">
<div class="field">
<input type="text" name='email' placeholder="Email Address" required/>
</div>
<div class="field">
<input type="password" name='pwd' placeholder="Password" required/>
</div>
<div class="field">
<input type="password" name='pwdRepeat' placeholder="Confirm password" required/>
</div>
<div class="field btn">
<div class="btn-layer"></div>
<input type="submit" name = "submit" value="Sign up">
</div>
</form>
dbh.php:
<?php
$serverName = "127.0.0.1";
$dBUserName = "root";
$dBPassword = "";
$dBName = "Quiz1dB";
$conn = mysqli_connect($serverName, $dBUserName, $dBPassword, $dBName);
if (!$conn){
die("Connection failed: " .mysqli_connect_error());
}
signUpManager.php:
<?php
if (isset($_POST["submit"])) {
$email = $_POST['email'];
$pwd = $_POST['pwd'];
$pwdRepeat = $_POST['pwdRepeat'];
require 'dbh.php';
require 'functions.php';
if (invalidEmail($email) !== false) {
header("location:../index.php?error=invalidemail");
exit();
}
if (pwdMatch($pwd, $pwdRepeat) !== false) {
header("location:../index.php?error=passworddontmatch");
exit();
}
if (emailTaken($conn, $email) !== false) {
header("location:../index.php?error=emailtaken");
exit();
}
creatUser($conn, $email, $pwd);
}
else{
header("location:../index.php?error=elsebum");
exit();
}
functions.php:
<?php
function invalidEmail($email){
$result;
if (!filter_var($email, FILTER_VALIDATE_EMAIL)){
$result = true;
}
else {
$result = false;
}
return $result;
}
function pwdMatch($pwd, $pwdRepeat){
$result;
if ($pwd !== $pwdRepeat){
$result = true;
}
else {
$result = false;
}
return $result;
}
function emailTaken($conn, $email){
$sql = "SELECT * FROM users WHERE email = ?;";
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $sql)) {
header("location:../index.php?error=stmtfailed");
exit();
}
mysqli_stmt_bind_param($stmt, "s", $email);
mysqli_stmt_execute($stmt);
$resultData = mysqli_stmt_get_result($stmt);
if ($row = mysqli_fetch_assoc($resultData)) {
return $row;
}
else{
$result = false;
return $result;
}
mysqli_stmt_close($stmt);
}
function creatUser($conn, $email, $pwd) {
$sql = "INSERT INTO users (email, userPassword) VALUES (?, ?);";
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $sql)) {
header("location:../index.php?error=stmtfailed");
exit();
}
$hashedPwd = password_hash($pwd, PASSWORD_DEFAULT);
mysqli_stmt_bind_param($stmt, "ss", $email, $hashedPwd);
mysqli_stmt_execute($stmt);
mysqli_stmt_close($stmt);
header("location:../index.php?error=none");
exit();
}