2015-07-11 93 views
0

在我的网站上我有一个注册表。用户只需输入电子邮件,密码并重新输入密码即可。PHP不提交用户的详细信息到数据库?

但是,即使填写了所有字段,我仍然收到一个错误,表示我没有填写表格。我不明白我做错了什么(PHP相对较新)

请注意我没有尝试清理用户输入,因此我知道我的代码将容易受到SQL注入!

<?php 
include("database.php"); 
include("requirements.php"); 

if(isset($_POST['register'])) { 
$email = $_POST['email']; 
$password = $_POST['password']; 
$confpassword = $_POST['confpassword']; 
} 

if(isset($password) && isset($email) && isset($confpassword)){ 
if($password != '' && $email != '' && $confpassword != ''){ 
    if($password == $confpassword){ 
     if(strlen($password) <= 20 && strlen($password) >= 6){ 
      if(strlen($email) > 6){ 
       $hash = password_hash($password, PASSWORD_DEFAULT);    
       $insert_user = mysqli_query($con, "INSERT INTO users (email, password, salt) VALUES ('".$email."', '".$password."', '".$salt."')"); 
        if($insert_user) 
        { 
        echo "Thank you for registering, you can now login to your account and create a listing."; 
        } 
        else 
        { 
        echo "Sorry there was an error - please try again later. If the issue continues please contact support."; 
        } 
       } 
       else 
       { 
       echo "You need to insert an email."; 
       } 
      } 
      else 
      { 
      echo "The entered email address needs to be above 6 characters."; 
      } 
     } 
     else 
     { 
     echo "The password must be between 6 and 20 characters long."; 
     } 
    } 
    else 
    { 
    echo "The two passwords you have entered do not match."; 
    } 
} 
else 
{ 
echo "You have not inserted all of the required fields that were needed."; 
} 

它只是跳到最后说“你没有插入所有需要的字段。”甚至不尝试查询数据库。

database.php中只是有数据库信息(表,密码等)

感谢您的连接!

回答

0

顺便说一句,我做你的代码的副本,并把它称为使用:

<form action="test.php" method="post"> 
    <input type="text" name="email" /> 
    <input type="text" name="password" /> 
    <input type="text" name="confpassword" /> 
    <input type="submit" name="register" /> 
</form> 

它完美。我认为你的错误是在你的HTML表单中。

+0

原来,这是HTML - 在我的表单中,我的“Value =”注册“”在我的按钮上,而不是“占位符=”注册“”......菜鸟错误。多谢你们! – PublicDisplayName

+0

不客气;) – Lemurantin

0

为了理解发生了什么,您应该在代码的开始处使用“var_dump($ variable)”函数。 这是一个调试功能,显示变量的内容(对数组有用)。

1)如果只有一个字段为空,则该字段的名称在某处存在问题。

2)如果所有的字段都是空的,你的表单或你的提交按钮有问题。

只是一个提示:不要使用“isset”,然后检查你的字段填写,您可以使用“空($变量)!”,但它确实exactely同样,在一个函数调用;)

狐猴

+0

没有任何字段是空的,我已经正确填写了所有字段,我不明白我出错的地方。我将添加var_dump($变量) - 谢谢!编辑:如果我将两个密码字段留空,我会得到“您输入的密码不匹配” – PublicDisplayName

1

我会尝试手动将值分配给PHP变量($ email,$ password,$ confpassword)。

if(isset($_POST['register'])) { 
$email = $_POST['email']; 
$password = $_POST['password']; 
$confpassword = $_POST['confpassword']; 
} 

//Test variables 
$email = '[email protected]'; 
$password = 'testpassword'; 
$confpassword = 'testpassword'; 

if(isset($password) && isset($email) && isset($confpassword)){ 
if($password != '' && $email != '' && $confpassword != ''){ 

一旦您提交表单,如果工作正常,您知道问题出在您的$ _POST数据被分配给PHP变量。如果这不起作用,你知道你的逻辑存在一个问题,以及它如何处理这些变量。这至少会缩小你需要看的地方。

+0

好的,问题出在将$ _POST数据分配给PHP变量。感谢你 - 我如何进一步缩小问题的范围? – PublicDisplayName

相关问题