mardi 8 septembre 2020

Database comparison wont work correctly in if() statement

i am sitting at this problem for a lot of time and asked other people around me for help, which i recieved but didnt solved the problem. I am trying to compare if the username which someone choose in his registration is already in the database, same for the email.

The Problem: If one statement (for example the name) is not in the database, but the email is, it will still insert it into the database. If both statements are equal, it wont insert it.

What i did: I tried 4 main things which i write more or less like this:

  1. using if($res2_email == $email) { $emailissame = ""; }
    else { $emailissame = "no"; }
    (same for the name)
    I rewrote the other code to: if(!empty($emailissame && $nameissame)) { insert into database }
  2. if(!$res2_email == $email) {
    if(!$res2_name == $name) { insert into database }
    else { echo "Name is the same"; }
    }
    else { echo "Email is the same"; }
  3. Echoing the code via $res2_email $res2_name $email $name where the results were as expected the same $res2_email and $email were the same, $res2_name and $name also.

Thats a piece of my code right now which does not work:

if(filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
    $email = $_POST['email'];
}
else {
        echo "<span class=\"phpfehlermeldung\">Die angegebene E-Mail Adresse ist keine gültige E-Mail Adresse!</span>";
        include "../footer.php";
        exit;
    }
$passwort = $_POST['passwort'];

$sqlr = $verbindung->prepare("SELECT name, email FROM nutzer WHERE name=? AND email=?");
            $sqlr->bind_param("ss", $name, $email);
            $sqlr->execute();
            $sqlr->bind_result($res2_name, $res2_email);
            $sqlr->fetch();

            if($res2_email == $email || $res2_name == $name) {
                echo "<span class=\"phpfehlermeldung\">Die E-Mail Adresse oder Name ist bereits genutzt: $name $email.</span>";
            }
            else {
                $insertr = $verbindung->prepare("INSERT INTO nutzer (name, email, passwort) VALUES (?, ?, ?)");
                $insertr->bind_param("sss", $name, $email, $passwort);
                $insertr->execute();
                
                echo "<span class=\"phpfehlermeldung\">Sie haben sich registriert mit $name, $email, $passwort.</span>";
                
                /* Verbindungen schließen */
                mysqli_close($verbindung);
                $sqlr->close();
                $insertr->close();
            }

Yes until now the passwords are not hashed, which i will work on in the next days. Thanks for the answers in advance.

Aucun commentaire:

Enregistrer un commentaire