duanhoupeng6642 2016-03-14 00:04
浏览 10
已采纳

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

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 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)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 数据库数据成问号了,前台查询正常,数据库查询是?号
  • ¥15 算法使用了tf-idf,用手肘图确定k值确定不了,第四轮廓系数又太小才有0.006088746097507285,如何解决?(相关搜索:数据处理)
  • ¥15 彩灯控制电路,会的加我QQ1482956179
  • ¥200 相机拍直接转存到电脑上 立拍立穿无线局域网传
  • ¥15 (关键词-电路设计)
  • ¥15 如何解决MIPS计算是否溢出
  • ¥15 vue中我代理了iframe,iframe却走的是路由,没有显示该显示的网站,这个该如何处理
  • ¥15 操作系统相关算法中while();的含义
  • ¥15 CNVcaller安装后无法找到文件
  • ¥15 visual studio2022中文乱码无法解决