mysqli查询结果未插入数据库中

时间:2015-04-08 20:18:19

标签: php mysql mysqli

我正在为我的网站注册页面。我在PHPMyAdmin中维护了7个字段的数据库(id,用户名,密码,电子邮件,活动,代码和日期)。我使用代码和活动字段来实现电子邮件激活。注册表使用post方法获取4个参数(名称,电子邮件,密码和重新输入密码),并将提交按钮的信息传递给PHP代码,以检查用户是否是新的,电子邮件ID应该是新的并在单击提交按钮后检查空字段。 PHP代码如下所示:

if($_POST['registerbtn']){
//checking each fields
$getuser = $_POST['user'];
$getemail = $_POST['email'];
$getpass = $_POST['pass'];
$getretypepass = $_POST['retypepass'];

if($getuser){
    if($getemail){
        if($getpass){
            if($getretypepass){
                if($getpass === $getretypepass){
                    if((strlen($getemail) >= 7)&& (strstr($getemail, "@")) && (strstr($getemail, "."))){
                        $con= mysqli_connect("localhost", "root", "", "web" );
                        $query= mysqli_query($con, "SELECT * FROM users WHERE username='$getuser'");
                        $numrows= mysqli_num_rows($query);
                        if($numrows == 0){
                            $query= mysqli_query($con, "SELECT * FROM users WHERE email='$getemail'");
                            $numrows= mysqli_num_rows($query);
                        if($numrows == 0){

                            $password = md5(md5("dfhXjh".$getpass."12asdss"));
                            $date = date("F d, Y");
                            $code = md5(rand());
                            mysqli_query($con, "INSERT INTO users (`id`, `username`, `password`, `email`, `active`, `code`, `date`) VALUES(
                            '', '$getuser', '$password', $getemail', '0', '$code', '$date')"); //This statement
                            $query= mysqli_query($con, "SELECT * FROM users WHERE username = '$getuser'");
                            $numrows= mysqli_num_rows($query);
                            if($numrows == 1){
                                $site ="http://localhost/web";
                                $webmaster ="Ace <ace8@gmail.com>";
                                $headers ="From: $webmaster";
                                $subject ="Activate your account";
                                $message = "Thanks for registering. Click the link below to activate your account.\n";
                                $message .="$site/activate.php?user=$getuser&code=$code \n";
                                $message .="You must activate your account to login.";

                                if( mail($getemail, $subject, $message, $headers) ){
                                    $errormsg = "You have been registered. You must activate your account from the activation link sent to <b>$getemail</b>";
                                    $getuser = "";
                                    $getemail = "";

                                }
                                else
                                    $errormsg = "An error has occured. Activation mail not sent.";

                            }
                            else
                            $errormsg= "An error occured. Account was not created.";
                        }
                        else
                        $errormsg="A user with that e-mail already exists.";    
                        }
                        else
                        $errormsg="A user with that username already exists.";

                        mysqli_close($con);
                    }
                    else
                    $errormsg= "You must enter a valid e-mail address.";
                }
                else
                $errormsg= "Passwords did not match.";
            }
            else
            $errormsg= "You must re-type your password.";
        }
    else
    $errormsg= "You must enter your password.";
    }
        else
        $errormsg= "You must enter your e-mail.";
}
else
$errormsg = "You must enter your user name.";}

当我在加密密码后插入新用户,生成激活链接的代码并将活动设置为零时,它不会插入到数据库中(请参阅此语句注释)。我无法弄清楚并最终显示消息&#34;发生错误。帐户未创建。&#34;

2 个答案:

答案 0 :(得分:0)

我会假设“&#39; id&#39; column是此表的主键,因此设置为自动递增。鉴于此 - 您不应该在insert语句中指定它。它也可能是数据库中的必填字段,因此尝试将其设置为&#39;&#39;,正如您在此处所做的那样,将导致错误。

答案 1 :(得分:0)

终于找到了错误。在insert语句之前,我没有检查连接是否存在。除此之外,insert语句必须稍微修改一下,如图所示。

if($con){mysqli_query($con, "INSERT INTO users (`id`, `username`, `password`, `email`, `active`, `code`, `date`) VALUES('', '".$getuser."', '".$password."', '".$getemail."', '0', '".$code."', '".$date."')");} else echo "Connection lost.";