duan051347 2014-02-06 09:41
浏览 53
已采纳

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

Finally after spending two days on this and with help from everyone from Stack Overflow we've manage to get this working. Thank you for all your hard work guys and talking me through step by step!

We are trying to get the following code to work currectly,

It needs to A. Check Activation code is NULL and if so move the user to one of the forms B. If Check Activation comes back other than NULL it should tell the user to try another activation code. I know this is pretty simple but we can't seem to see the issue.

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条回答 默认 最新

  • douxie9347 2014-02-06 09:58
    关注

    The problem is that your query most likely doesn't return anything to begin with.

    You are using the & operator, which is a bitwise operator.

    Instead of

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

    use

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

    Also, remove the ; at the end of the query.

    To check if your query actually returns data, use

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

    Another note: Don't use $_POST values in queries, make sure you use mysql_real_escape on them first. Or even better, use prepared statements with PDO or mysqli.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?