SQL / PHP如果等于NULL则显示else

时间:2014-02-05 14:59:01

标签: php sql

我一直在努力这一天,我正在尝试进行简单的SQL检查,我有两个带激活码的字段,如果用户已经激活了帐户,则第二个字段具有从中输入的激活码第一个字段,如果未激活用户,则第二个字段只显示NULL。

我正在尝试整理一个快速代码片段来检查用户是否被激活,只需检查它是否为NULL。

这是我到目前为止所做的:

        <?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` = '".$username."' & `Activation` = '".$activation_code."'; ");
$rows_found = mysql_num_rows($checkcustomer);

if ($rows_found > 0) {  

    // If unactivated value should be NULL allowing the user to follow through to the forms, If Value is anything else this means the user is activated and should not have access to the forms.
    $val = mysql_query($checkcustomer);
    if ($val == 'NULL') 
        {
            // Redirect User to either form 1,2,3 depending on first number of Username

            $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();
?>

Database Activation Form Redirect

1 个答案:

答案 0 :(得分:0)

好的,所以当你发出一个查询并返回一个可能为null的列时,检查它的正确方法是使用is_null

您的代码必须类似于:

<?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` = '".$username."' & `Activation` = '".$activation_code."'; ");
$rows_found = mysql_num_rows($checkcustomer);

if ($rows_found > 0) {
  // If unactivated value should be NULL allowing the user to follow through to the forms, If Value is anything else this means the user is activated and should not have access to the forms.
  $val = mysql_query($checkcustomer);

  if (is_null($val)) {
    // Redirect User to either form 1,2,3 depending on first number of Username
    $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 {
...
}
?>