简体   繁体   中英

Database comparison won't 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 received but didn't 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 won't 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"; }
    if($res2_email == $email) { $emailissame = ""; }
    else { $emailissame = "no"; }
    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.

That's a piece of my code right now which does not work:

$name = $_POST['name'];
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.

This example is for mysqli, that fits with

$sqlr = $verbindung->prepare("SELECT name, email FROM nutzer WHERE name=? OR email=?");
$sqlr->bind_param("ss", $name, $email);
$sqlr->execute();
$sqlr->execute();
$result = $sqlr->get_result();
$user = $result->fetch_array(MYSQLI_ASSOC);
while ($user = $result->fetch_assoc()) {
    if($user['email'] == $email || $user['name'] == $name) {
    ...
    }
 }

$result->close();

I also changed the AND on your select, because your if also indicates that you don't want either to be true, But this could lead to more than one row

If you really wnat that neither name nor email isthe same ( You must nake the columns unique, that makes it impossible to have them twice

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM