0

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();
}    
  • Make sure PHP error reporting is on, and mysqli error handling is on. See these setup guides for how to do that: https://stackify.com/php-error-logs-guide/ (php error logging/reporting) https://stackoverflow.com/a/14578644/5947043 (mysqli error handling) . Then test it again and see if you get any errors. – ADyson Jan 21 '21 at 14:43
  • Thank you @ADyson, So there's nothing overtly wrong with the code i posted ? – KongFooJew Jan 21 '21 at 14:53
  • There's plenty of things wrong with this code, but you have to be more specific – Dharman Jan 21 '21 at 14:57
  • If you are only starting to learn PHP then you should learn PDO instead of mysqli. PDO is much easier and more suitable for beginners. Start here https://phpdelusions.net/pdo – Dharman Jan 21 '21 at 14:57
  • [How to get the error message in MySQLi?](https://stackoverflow.com/a/22662582/1839439) Once you enable mysqli error reporting please update the question with the error message. – Dharman Jan 21 '21 at 14:59
  • 1. Enable mysqli error reporting 2. Remove all the `if` statements around mysqli functions. 3. `pwdMatch()` function is useless, it is just an if statement. 4. The same with `invalidEmail()`. Just do a normal `if` statement. 5. `mysqli_stmt_close($stmt);` in `emailTaken()` is NOOP code. Remove it. 6. There is absolutely no reason to call `mysqli_stmt_close($stmt);` right before `exit`. – Dharman Jan 21 '21 at 15:09
  • You put that line right before `mysqli_connect` and then you can remove all `if` statements. See how it is done in the manual https://www.php.net/manual/en/mysqli.construct.php – Dharman Jan 21 '21 at 15:17
  • @Dharman, thank you! I keep writing comments and you answer them before i post..So i'll update the code and report back. Thanks again! – KongFooJew Jan 21 '21 at 15:18
  • Nah don't add an answer. If that line helped to solve the problem then let's use that as an answer instead. I am glad that I was able to help. – Dharman Jan 21 '21 at 18:57

0 Answers0