PHP不向数据库发送数据

时间:2016-02-13 21:26:11

标签: php

我在尝试创建注册表时遇到此错误:

  

在我们的数据库中找不到您的电子邮件

     

无法向您的电子邮件地址发送确认链接

为什么不发送?

这是PHP:

curl -X POST \
-H "Accept: application/json" \
-H "X-Access-Token: ###secureToken###" \
-H "Cache-Control: no-cache" \
-d '{
    "frames": [
        {
            "index": 0,
            "text": "SUCCESS",
            "icon": null
        }
    ]
}' \
https://developer.lametric.com/api/V1/dev/widget/update/com.lametric.###appid###

这是表格:

<?php

    include('config.php');

    //test to see if username is alphanumeric
    $testname = $_POST['username'];

    if (!eregi('([^A-Za-z0-9])',$testname)){
        //test for duplicate names
        $query = "SELECT * FROM users WHERE username='$_POST[username]'";
        $result = mysql_query($query);
        $num = mysql_num_rows($result);

        if ($num == 0){
            //test for duplicate emails
            $query2 = "SELECT * FROM users WHERE email='$_POST[email]'";
            $result2 = mysql_query($query2);
            $num2 = mysql_num_rows($result2);

                if ($num == 0){
                    //test if emails and passwords match up
                    if(($_POST['pass']==$_POST['pass2'])&&($_POST['email']==$_POST['email2'])){

                        //generate random confirmation code
                        $confirm_code = md5(uniqid(rand()));

                        //get rid of all html from hackers
                        $name=strip_tags($_POST['username']);
                        $email=strip_tags($_POST['email']);
                        $pass=strip_tags($_POST['pass']);

                        //insert data into database
                        $sql="INSERT INTO temp SET code='$confirm_code',username='$name',email='$email',pass='$pass'";
                        $result3 = mysql_query($sql);
                            if ($result3){
                                $message = "Your Confirmation link \r\n";
                                $message .= "Click on this link to activate your account \r\n";
                                $message .= "localhost/PHPTUT/confirmation.php?passkey=$confirm_code";

                                $sentmail = mail($email,'Registration Confirmation',$message,'From: email@example.com');
                                header("Location:thankyou.html");
                            }else{
                                echo 'Could not find your email in our database';
                            }

                            //if your email successfully sent
                            if($sentmail){ // **LINE 47**-------------------
                                echo 'Your confirmation link has been sent to your email address.';
                            }else{
                                echo 'Cannot send confirmation link to your email address';
                            }
                    }else{
                        header("Location:badmatch.html");
                    }
            }else{
                header("Location:emailinuse.html");
            }
        }else{
            header("Location:nameinuse.html");
        }
    }else{
        header("Location:invalidname.html");
    }

    ?>

1 个答案:

答案 0 :(得分:1)

在所有if-else分支中都没有设置$sentmail。如果在数据库中找不到电子邮件,则没有人将其设置为false,并且以下行将生成通知:

                        if($sentmail){ // **LINE 47**-------------------

要避免这种情况,请在开头的某处设置默认值false

$sentmail = false;
相关问题