0

我目前正在处理我网站的登录/注册页面,但我遇到的问题是 PHP 脚本没有将行添加到数据库中。几天来我一直试图解决这个问题,但我还没有想出解决方案。提交时页面上也没有错误...

<?php

    mysql_connect("localhost", "username", "password") or die(mysql_error());
    mysql_select_db("test") or die(mysql_error());

    if(isset($_POST['f_name']) && !empty($_POST['f_name']) && 
       isset($_POST['l_name']) && !empty($_POST['l_name']) && 
       isset($_POST['reg_email']) && !empty($_POST['reg_email']) && 
       isset($_POST['conf_email']) && !empty($_POST['conf_email']) && 
       isset($_POST['password']) && !empty($_POST['password']) && 
       $_POST['birthday-day'] != 0 && 
       $_POST['birthday-month'] != 0 && 
       $_POST['birthday-year'] != 0) 
    {

    $f_name = mysql_real_escape_string($_POST['f_name']);
    $l_name = mysql_real_escape_string($_POST['l_name']);
    $reg_email = mysql_real_escape_string($_POST['reg_email']);
    $conf_email = mysql_real_escape_string($_POST['conf_email']);
        $password = mysql_real_escape_string($_POST['password']);
    $b_day = $_POST['birthday-day'];
    $b_month = $_POST['birthday-month'];
    $b_year = $_POST['birthday-year'];

    $search = mysql_query("SELECT email FROM users WHERE email='".$reg_email."'");
    $match = mysql_num_rows($search);

    if($reg_email !== $conf_email) {

        $msg = "The emails dont match.";

    }

    if (!preg_match("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$^", $reg_email)) {

        $msg = "The email you have entered is invalid. Please try again.";
    }

    if ($match > 0) {

        $msg = "Email address already registered. Please enter a different one.";
    } else {

        mysql_query("INSERT INTO userstest (f_name, l_name, email, password) VALUES(
            '".$f_name."',
    '".$l_name."',
    '".$reg_email."',
    '".$password."') ") or die(mysql_error());

        }

}

else {

    //code

}

形式...

<form name="register" action="<?php print ($_SERVER['PHP_SELF']); ?>" method="post" class="regform" >

    <input type="text" name="f_name" class="f_name" placeholder="First Name" />
    <input type="text" name="l_name" class="l_name" placeholder="Last Name" />
    <input type="text" name="reg_email" class="reg_email" placeholder="Email" />
    <input type="text" name"conf_email" class="conf_email" placeholder="Re-enter Email" />
    <input type="password" name="password" class="reg_pass" placeholder="Password" />

    <div id="birthday">

        <h1>
            BIRTHDAY
        </h1>

        <select name="birthday-day" id="day" class="day">

            <option value="0">Day</option>
            <option value="01">1</option>
            <option value="02">2</option>
            <option value="03">3</option>
            <option value="04">4</option>
            <option value="05">5</option>
            ...etc


        </select>

        <select name="birthday-month" class="month">

            <option value="0">Month</option>
            <option value="01">Jan</option>
            <option value="02">Feb</option>
            <option value="03">Mar</option>
            <option value="04">Apr</option>

        </select>

        <select name="birthday-year" class="year">

            <option value="0">Year</option>
            <option value="1994">1994</option>

        </select>

    </div>

    <div id="submitbutton">

        <input type="submit" name="submit" class="reg_submit" value="REGISTER" />

    </div>

</form>

这可能是一个我没有看到的简单语法错误,但我感谢我得到的任何帮助。

4

1 回答 1

0

以下是我的原始答案,它产生了导致答案的线程。由于这不会帮助许多人寻找解决方案,这将使事情变得更清楚:

在表单的顶部,在它显示之后但在提交之前,添加以下代码:

    $super_test = (
        isset($_POST['f_name'])     && !empty($_POST['f_name']) 
    &&  isset($_POST['l_name'])     && !empty($_POST['l_name']) 
    &&  isset($_POST['reg_email'])  && !empty($_POST['reg_email']) 
    &&  isset($_POST['conf_email']) && !empty($_POST['conf_email']) 
    &&  isset($_POST['password'])   && !empty($_POST['password']) 
    &&  $_POST['birthday-day'] != 0 
    &&  $_POST['birthday-month'] != 0 
    &&  $_POST['birthday-year'] != 0
    ) ;

    dump($_POST);
    die(dump($super_test));

这立即显示了 $_POST 变量 (conf_email) 中缺少的字段,然后在这种情况下缩小为缺少的“=”符号。

魔术功能dump()是从@PhilSturgeon 中删除的:

function dump()
{
    list($callee) = debug_backtrace();
    $arguments = func_get_args();
    $total_arguments = count($arguments);

    echo '<fieldset style="background: #fafafa !important; border:2px cornflowerblue solid; padding:6px; font-size: 10px;">';
    echo '<legend style="background:lightgrey; padding:5px;">'.$callee['file'].' @ line: '.$callee['line'].'</legend><pre>';

    $i = 0;
    foreach ($arguments as $argument)
    {
        echo '<br/><strong>Debug #'.(++$i).' of '.$total_arguments.'</strong>: ';

        if ( (is_array($argument) || is_object($argument)) && count($argument))
        {
            print_r($argument);
        }
        else
        {
            var_dump($argument);
        }
    }

    echo '</pre>' . PHP_EOL;
    echo '</fieldset>' . PHP_EOL;
}

-- 总结结束 --


你有

$reg_email = mysql_real_escape_string($_POST['reg_email']);

然后你有

$search = mysql_query("SELECT email FROM users WHERE email='".$reg_email."'");

查询可能永远不会匹配,因为 $reg_email 发生了一些事情,使其与数据库列的值不同。

无论如何,我会:

  • mysql_select在that read下添加一行die(print_r($_POST))以查看来自输入页面的内容(第一个调试步骤)
  • if match ...在to之前添加一行,echo $msg因为它可能被覆盖。
  • 我会将查询构建分为两个阶段并在那里强制输出,我会将其移到上方,if ($match以便您始终可以看到查询,而不管逻辑如何:

    $query = "INSERT INTO userstest (f_name, l_name, email, password) VALUES('".$f_name."', '".$l_name."', '".$reg_email."', '".$password ."')"; 回声$查询;mysql_query($查询) ...

于 2013-08-18T14:55:58.623 回答