-1

I'm doing an registration form and I'm trying to check if the email that the person inserts in the input is already in another table that has all emails that I allow to be registered. If it is it should register the person. I don't understand where I'm failing. I'm starting now with php. Please help.

<?php

if(isset($_POST['signup-submit'])){


    require 'dbh.inc.php';

    $username = mysqli_real_escape_string($conn, $_POST['uid']);
    $email =  mysqli_real_escape_string($conn,$_POST['mail']);
    $password =  mysqli_real_escape_string($conn,$_POST['pwd']);
    $passwordRepeat =  mysqli_real_escape_string($conn, $_POST['pwd-repeat']);
    $check1 = $_POST['check1'];
    $check2 = $_POST['check2'];
    if(empty($username) || empty($email) || empty($password) || empty($passwordRepeat)) {

        header ("Location: ../header.php?error=emptyfields&uid=".$username."&mail=".$email);
        exit();
    }
    else if (!filter_var($email, FILTER_VALIDATE_EMAIL) && !preg_match("/^[a-zA-z0-9]*$/", $username)){

        header("Location: ../header.php?error=invalidadmail&uid=");
        exit();
    } 
    else if (!filter_var($email, FILTER_VALIDATE_EMAIL)){

        header("Location: ../header.php?error=invalidadmail&uid=".$username);
        exit();
    } 
    else if (!preg_match("/^[a-zA-z0-9]*$/", $username)){

        header("Location: ../header.php?error=invalidaduid&mail=".$email);
    exit();

}
    elseif($password !== $passwordRepeat){
        header("Location: ../header.php?error=passwordcheck&uid=".$username."&mail=".$email);
        exit();
    }
    elseif((!isset($check1)) || (!isset($check2))){
        echo"<script>alert('É necessário confirmar as duas opções :(');
        window.location.href='../header.php'</script>";
exit();
    }

This is the part of the code that is not working

$sql2 = "SELECT * FROM emails WHERE (email_socio = '$email')";
$res = mysqli_query($conn, $sql2); 

if (mysqli_num_rows($res) < 0) {
echo "FAIL";
}

These are other validations and where it will insert the data into final table

    else{


$sql = "SELECT uidUsers FROM users WHERE uidUsers=?";
$stmt = mysqli_stmt_init($conn);
if(!mysqli_stmt_prepare($stmt, $sql)){
    header("Location: ../header.php?error=sqlerror");
    exit();

}
 else{
     mysqli_stmt_bind_param($stmt, "s", $username);
     mysqli_stmt_execute($stmt);
     mysqli_stmt_store_result($stmt);
     $resultCheck = mysqli_stmt_num_rows($stmt);
     if($resultCheck > 0){
        header("Location: ../header.php?error=usertaken&mail=".$email);
        exit();
     }
     else {




        $sql = "INSERT INTO users (uidUsers, emailUsers, pwdUsers) VALUES (?, ?, ?)";
        $stmt = mysqli_stmt_init($conn);
        if(!mysqli_stmt_prepare($stmt, $sql)){
            header("Location: ../header.php?error=sqlerror");
            exit();

        } else {
            $hashedPwd = password_hash($password, PASSWORD_DEFAULT);
            mysqli_stmt_bind_param($stmt, "sss", $username, $email, $hashedPwd);
            mysqli_stmt_execute($stmt);
            $sql ="SELECT * FROM users WHERE uidUsers='$username' AND emailUsers='$email'";
            $result = mysqli_query($conn, $sql);
            if(mysqli_num_rows($result)>0){
                while($row = mysqli_fetch_assoc($result)){
                    $userid = $row['idUsers'];
                    $sql = "INSERT INTO profileimg (userid, status) VALUES ('$userid', 1)";
                    mysqli_query($conn, $sql);
                }
            }
            header("Location: ../header.php?signup=success");
            exit();
        }
     }
 }
 }
 mysqli_stmt_close($stmt);
 mysqli_close($conn);
}

else {
    header("Location: ../header.php");
    exit();
}
  • What do you mean by failing? What errors are you getting, if any? What is the actual result (does it register everyone)? Also, do you really mean in another *database*, not another *table*? – El_Vanja Mar 13 '20 at 21:50
  • I'm sorry, it is in another table. I corrected it now. This code just keeps registering any email, it doesn't just register the ones that are in the other table. – Nayana Ciuro Mar 13 '20 at 21:55
  • 1
    You need to pare down the code you post to only what demonstrates the error. There's too much unrelated code here for anyone to know what is or isn't relevant. – Sammitch Mar 13 '20 at 22:01
  • Why aren't you using a prepared statement for everything? – Funk Forty Niner Mar 13 '20 at 22:11
  • If you want to check if a record exists, the `if (mysqli_num_rows($res) < 0)` should be using `>` and not `<`. – Funk Forty Niner Mar 13 '20 at 22:13

1 Answers1

0

Your condition is wrong:

if (mysqli_num_rows($res) < 0) {
    echo "FAIL";
}

You're checking for less than zero, when in fact it should be less than one.

So, change it to either of the two:

if (mysqli_num_rows($res) === 0) // it logically cannot contain negative values
if (mysqli_num_rows($res) < 1)
El_Vanja
  • 3,660
  • 4
  • 18
  • 21