duanhoupeng6642
2016-03-14 00:04 阅读 8
已采纳

返回的注册表已经是注册用户

So I have a registration form that is supposed to check if the email exists and if it does it doesn't register but if it doesn't it registers. The problem is that everytime I get you are already a registered user even though I don't even have any data in database.

here is code

<?php
error_reporting(E_ALL); 
define('DB_HOST', 'localhost');
define('DB_NAME', 'Users');
define('DB_USER', 'root');
define('DB_PASSWORD', '');

$con = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); //Use MySQLi as MySQL is depreciated.

 if (isset($_POST['submit'])) {
//No need for functions, do it all here as otherwise you'll have to declare globals.
  if (!empty($_POST['email'])) {
    $query = mysqli_prepare($con, "SELECT * FROM Websiteusers WHERE email = ?"); //Preparing the SQL query (We don't insert values directly into the query)
    mysqli_stmt_bind_param($query, "s", $_POST['email']); //Bind the email to the SQL query
    mysqli_stmt_execute($query); //Execute the query
    mysqli_stmt_store_result($query); //Store the results
    if(!mysqli_stmt_num_rows($query)) //Is the number of rows 0?
    {
        //Yes
        echo "SORRY...YOU ARE ALREADY REGISTERED USER...";
    }
    else
    {
        //No
        $query = mysqli_prepare($con, "INSERT INTO WebsiteUsers (firstname,efternamn,email,password) VALUES (?, ?, ?, ?)"); //Preparing the SQL  query (We don't insert values directly into the query)
         mysqli_stmt_bind_param($query, "ssss", $_POST['firstname'],  $_POST['efternamn'], $_POST['email'], $_POST['password']); //Bind the params to  the SQL query
        mysqli_stmt_execute($query); //Execute the query
        echo "YOUR REGISTRATIOfafa";
     } 
    }
   }
   ?>
<!DOCTYPE HTML> 
<html> 
<head> 
<title>Registrera Dig</title> 
<link rel="stylesheet" type="text/css" href="style.css">
</head> 
<body> 
 <table>
    <form method="POST" action="test.php"> 
        <tr> <td>Namn</td><td> <input type="text" name="firstname"></td> </tr> 
        <tr> <td>Efternamn</td><td> <input type="text" name="efternamn"></td>    </tr> 
        <tr> <td>Email</td><td> <input type="text" name="email"></td></tr> 
        <tr> <td>Lösenord</td><td> <input type="password" name="password"> </td> </tr> 
        <tr> <td>Bekräfta lösenord </td>
        <td><input type="password" name="cpass"></td> </tr>
       <tr> <td><input id="button" type="submit" name="submit" value="Skapa konto"></td> </tr> 
    </form> 
    </table>
   </body>
    </html>

what can be the problem?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    drl971115 drl971115 2016-03-14 00:16

    Take a look at the following line

    if(!mysqli_stmt_num_rows($query)) 
    

    and consider the case when you get no rows returned from the database. Your code would then in principle look like

    if (!0)
    

    which equals true, because the exclamation-mark is the not-operator - it reverses it. if (1) would also be true, but if (0) would be false (and skip to the else-block of your statement).

    The solution is to simply remove this !, as your logic is currently reversed. So that line would look like

    if (mysqli_stmt_num_rows($query)) 
    

    To make your code even more readable, you can check if it's greater than zero, it basically does the same thing, but it's quicker to understand exactly what's going on. It's a personal preference though.

    if (mysqli_stmt_num_rows($query) > 0)
    
    点赞 评论 复制链接分享