0

I have been trying to make my login system for a while now but it keeps on failing to check the db or write to the db.

signup page:

<?php
  include_once 'header.php';
?>

<div class="header">
  <h2>Register</h2>
</div>
  
<form method="post" action="inc/signup.inc.php">

  <label>Full Name</label>
  <input type="text" name="name" placeholder="Full Name...">

  <label>Username</label>
  <input type="text" name="username" placeholder="Username">

  <label>Email</label>
  <input type="email" name="email" placeholder="Email">

  <label>Password</label>
  <input type="password" name="pwd">

  <label>Confirm password</label>
  <input type="password" name="pwdR">

  <button type="submit" name="submit">Sign Up</button>

<p>
  Already a member? <a href="login.php">Sign in</a>
</p>
</form>

<?php 
  include_once 'footer.php';
 ?>

Signup backend page

<?php

if (isset($_POST["submit"])){

    $name = $_POST["name"];
    $username = $_POST["username"];
    $email = $_POST["email"];
    $pwd = $_POST["pwd"];
    $pwdr = $_POST["pwdR"];

    require_once 'db.inc.php';
    require_once 'func.inc.php';

    if (emptyInputSignup($name, $username, $email, $pwd, $pwdr) !== False) {
        header("location: ../signup.php?error=emptyinput");
        exit();
    }
    if (invaliduid($username) !== False) {
        header("location: ../signup.php?error=invaliduid");
        exit();
    }
    if (invalidEmail($email) !== False) {
        header("location: ../signup.php?error=invalidemail");
        exit();
    }

    if (pwdMatch($pwd, $pwdr) !== False) {
        header("location: ../signup.php?error=passwordsdontmatch");
        exit();
    }
    if (uidExists($conn, $username, $email) !== False) {
        header("location: ../signup.php?error=usernametaken");
        exit();
    }

    createUser($conn, $name, $username, $email, $pwd);
}

else{
    header("location: ../signup.php");
    exit();
}

Now all the functions are handled on the following page where I think is where I went wrong.

<?php  

function emptyInputSignup($name, $username, $email, $pwd, $pwdr){
    $result;
    if (empty($name) || empty($username) || empty($email) || empty($pwd) || empty($pwdr)) {
        $result = true;
    }
    else{
        $result = false;
    }
    return $result;
}

function invaliduid($username){
    $result;
    if (!preg_match("/^[a-zA-Z0-9]*$/", $username)) {
        $result = true;
    }
    else{
        $result = false;
    }
    return $result;
}

function invalidEmail($email){
    $result;
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)){
        $result = true;
    }
    else{
        $result = false;
    }
    return $result;
}

function pwdMatch($pwd, $pwdr){
    $result;
    if ($pwd !== $pwdr){
        $result = true;
    }
    else{
        $result = false;
    }
    return $result;
}

function uidExists($conn, $username, $email){
    $sql = "SELECT * FROM users WHERE userUid = ? OR userEmail = ?;";
    $stmt = mysqli_stmt_init($conn);
    if (!mysqli_stmt_prepare($stmt, $sql)){
        header("location: ../signup.php?error=smtmfailedex");
        exit();
    }

    mysqli_stmt_bind_param($stmt, "ss" , $username, $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 createUser($conn, $name, $userid, $email, $pwd){
    $sql = "INSERT INTO users (userName, userUid, userEmail, userPwd) VALUES (?, ?, ?, ?);";
    $stmt = mysqli_stmt_init($conn);
    if (!mysqli_stmt_prepare($stmt, $sql)){
        header("location: ../signup.php?error=stmtfailedc");
        exit();
    }
    $hashedPwd = password_hash($pwd, PASSWORD_DEFAULT);

    mysqli_stmt_bind_param($stmt, "ssss" , $name, $username, $email, $hashedPwd);
    mysqli_stmt_execute($stmt);
    mysqli_stmt_close($stmt);
    header("location: ../profile.php?error=none");
    exit();
}

Now the database link that I have made is on the following page: DB Link

<?php

$serverName = "localhost";
$dBUsername = "root";
$dBpassword = "";
$dBname = "sakidc";

$conn = mysqli_connect($serverName, $dBUsername, $dBpassword, $dBname);

if (!$conn){
    die("Connection Failed: " . mysqli_connect_error());
}
  • You'll never execute `mysqli_stmt_close($stmt);` in `uidExists()` because you always return before it. – Barmar Mar 18 '21 at 07:54
  • Lines that are just `$result;` don't do anything, why do you have that? – Barmar Mar 18 '21 at 07:55
  • `if (something !== false)` should just be `if (something)` – Barmar Mar 18 '21 at 07:57
  • 1
    I don't see anything obviously wrong. What debugging steps have you taken so far? Have you added `var_dump($result);` statements to each of the validation functions to see what it's returning? – Barmar Mar 18 '21 at 07:59
  • See https://stackoverflow.com/questions/22662488/how-to-get-mysqli-error-in-different-environments – Barmar Mar 18 '21 at 08:06
  • I will run the var-dump9$result); now and see what I get but when I run the login page it returns the header I have setup - header("location: ../signup.php?error=smtmfailedex"); – Cpt.Fluffy Mar 18 '21 at 08:34
  • So there's a problem with your SQL. `mysqli_error($conn)` will return the error message. – Barmar Mar 18 '21 at 08:37
  • If you enabled the automatic error reporting as shown in the linked question, you would see the error message in the log. – Barmar Mar 18 '21 at 08:39
  • The issue was that the database could not be accessed due to port error on XAMPP, but it sorted now and logging in. but its not writing to the database now. – Cpt.Fluffy Mar 18 '21 at 09:10
  • I don't see anything obviously wrong with `createUser()`. Just keep checking for errors from the query. – Barmar Mar 18 '21 at 09:13
  • There you go. Now you need to figure out whether it should be null or not and fix your code adequately. – Dharman Mar 18 '21 at 09:20
  • Thanks, it seems to all be working and that the user "are" being saved on the db but I cant see them. – Cpt.Fluffy Mar 18 '21 at 09:25

0 Answers0