SQL / PHP如果为NULL显示其他显示

时间:2014-02-06 09:41:02

标签: php sql database

最后花了两天时间,在Stack Overflow的每个人的帮助下,我们都设法让这个工作。感谢所有辛勤工作的人,一步一步地和我说话!

我们正试图让以下代码正常工作,

它需要 A。检查激活码是否为NULL,如果是,则将用户移动到其中一个表单 B。如果检查激活返回非NULL,则应该告诉用户尝试其他激活码。我知道这很简单,但我们似乎无法看到这个问题。

Database Screenshot

<?php

$username = $_POST['username'];
$activation_code = $_POST['activation_code'];
$activation_codeurl = $activation_code;
$usernameurl = $username;

$db_host = "localhost";
$db_name = "aardvark";
$db_use = "aardvark";
$db_pass = "aardvark";

$con = mysql_connect("localhost", $db_use, $db_pass);
if (!$con){
    die('Could not connect: ' . mysql_error());
}
mysql_select_db($db_name, $con);

    $checkcustomer = mysql_query("SELECT `Check_Activation` FROM `members` WHERE `Username` = '".mysql_real_escape_string($username)."' AND `Activation` = '".mysql_real_escape_string($activation_code)."'; ");
    $array = mysql_fetch_array($checkcustomer);
    if (!$array === false)
        {

            $username = substr($username, 0, 1);
            if($username == '1') { 
                $redirect_url='form-one.php?membernumber='.$usernameurl.'&activation_code='.$activation_codeurl;
            } elseif($username == '2') { 
                $redirect_url='form-two.php?membernumber='.$usernameurl.'&activation_code='.$activation_codeurl;              
            } elseif($username == '3') { 
                $redirect_url='form-three.php?membernumber='.$usernameurl.'&activation_code='.$activation_codeurl;
            }
            header("Location:". $redirect_url);
    } 
    else 
    {

?>
<html>
    <head>
        <link rel='stylesheet' id='style-css'  href='css/style.css' type='text/css' media='all' />
        <meta name="viewport" content="width=960, initial-scale=0.32">
        <META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">
        <link rel="shortcut icon" href="http://welovebarrio.com/favicon.gif">
        <link rel="icon" href="http://welovebarrio.com/favicon.gif" type="image/gif">
        <title>Friends of BARRIO</title>
        <script type="text/javascript">
                    var _gaq = _gaq || [];
                    _gaq.push(['_setAccount', 'UA-35015193-1']);
                    _gaq.push(['_setDomainName', 'welovebarrio.com']);
                    _gaq.push(['_trackPageview']);
                    (function() {
                        var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
                        ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
                        var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
                    })();
        </script>
    </head>
    <body>
        <div class="inner-wrapper stage-one">
            <div class="barrio-logo">Friends of Barrio</div>
            <div class="barrio-wel-message">
                <h1>Welcome Friends of Barrio</h1>
                <span>-</span>
                <h2>Enter a valid membership number<br/> and activation code to continue</h2>
            </div>
            <form name="form1" method="post" action="check-activation.php" class="membership-form">
                <h3>Your membership number</h3>
                <input name="username" type="text" id="username" value="<?php echo $username; ?>" class="membership-number">

                <h3>our activation code</h3>
                <input name="activation_code" type="text" id="activation_code" value="<?php echo $activation_code; ?>" class="activation-code">

                <input type="submit" name="Submit" value="Continue" class="membership-continue">
            </form>

        </div>
        <div class="error-message">
            <span>Your membership number &amp; activation code <br/>is not valid, please check and re-enter</span>
        </div>

    <div class="background-tl"></div>
    <div class="background-tr"></div> 
    <div class="background-bl"></div>
    <div class="background-br"></div> 
</body>
</html>
<?php
}
$con->close();
?>

enter image description here

4 个答案:

答案 0 :(得分:0)

替换if (is_null($array['Check_Activation'])

if(!$array['Check_Activation'])

答案 1 :(得分:0)

如果未找到激活行, mysql_fetch_array 将返回FALSE。 试试这个:

if (empty($array['Check_Activation']))  {

它会检查是否找到任何行以及Check_Activation字段是否为空。

在查询中添加AND而不是&

您应该通过一些转义函数处理$username$activation_code以防止SQL注入。

... WHERE `Username` = '" . mysql_real_escape_string($username) . "' ...

答案 2 :(得分:0)

您必须仅匹配名称以检查值是否为空

$checkcustomer = mysql_query("SELECT `Check_Activation` FROM `members`
                              WHERE `Username` = '".$username."'");

然后你必须检查

    $array = mysql_fetch_array($checkcustomer);
    if (is_null($array['Check_Activation']))  {
            $username = substr($username, 0, 1);
            if($username == '1') { 
                $redirect_url='form-one.php?membernumber='.$usernameurl.'&activation_code='.$activation_codeurl;
            } elseif($username == '2') { 
                $redirect_url='form-two.php?membernumber='.$usernameurl.'&activation_code='.$activation_codeurl;              
            } elseif($username == '3') { 
                $redirect_url='form-three.php?membernumber='.$usernameurl.'&activation_code='.$activation_codeurl;
            }
            header("Location:". $redirect_url);
    } 
    elseif($array['Check_Activation']==$activation_code )
    {
      /*your code */
     }else{ 
?>

答案 3 :(得分:0)

问题是您的查询很可能不会返回任何内容。

您正在使用&运算符,它是一个按位运算符。

而不是

SELECT `Check_Activation` FROM `members`
WHERE `Username` = '".$username."' & `Activation` = '".$activation_code."';

使用

SELECT `Check_Activation` FROM `members`
WHERE `Username` = '".$username."' AND `Activation` = '".$activation_code."'

另外,删除;在查询结束时。

要检查您的查询是否实际返回数据,请使用

$array = mysql_fetch_array($checkcustomer);
if ($array === false)
{
   // Do something if the query failed to return anything, i.e.
   echo "Invalid username/activation code
}

另一个注意事项:不要在查询中使用$ _POST值,请确保首先在它们上使用mysql_real_escape。或者甚至更好,使用PDO或mysqli准备好的语句。

相关问题