无法找到if / else函数的确切值

时间:2017-01-09 04:57:58

标签: php mysql

我有这段代码并尝试将$bonval放入mysql插入函数,但它返回null。我在这里做错了什么?

<?php
if (!isset($_SESSION['views'])) { 
    $_SESSION['views'] = 0;
}
$_SESSION['views'] = $_SESSION['views']+1;

$first =  mt_rand(10,20);
$second =  mt_rand(40,50);
$third =  mt_rand(60,70);
if ($_SESSION['views'] == 2 or ($_SESSION['views'] == 5) or ($_SESSION['views'] == 10) or ($_SESSION['views'] == 20) or ($_SESSION['views'] == 50)) { 
?>
    <div class="popup">
        <form role="form" method='post' action='test.php'>
            <fieldset>
                <h2 class="blink_me" style="color:green;font-size:40px;"><?php if ($_SESSION['views'] == 2) { echo $bonval = $first; } elseif ($_SESSION['views'] == 5) { echo $bonval = $third; } elseif ($_SESSION['views'] == 10) { echo $bonval = $second; } elseif ($_SESSION['views'] == 20) { echo $bonval = $second; } elseif ($_SESSION['views'] == 50) { echo $bonval = $first; } ?></h2>

                <div class="form-group">
                    <center><div class="g-recaptcha" data-sitekey="sitekey"></div></center>
                </div>
                <div class="row">
                    <center><input type="submit" name="submit" class="btn btn-lg btn-success btn-block" value="Submit"></center>
                </div>
            </fieldset>
        </form>
    </div>
<?php
 }


if (isset($_POST['submit'])) {
    if(!empty($recaptcha)) {
        require("getCurlData.php");
        $google_url="https://www.google.com/recaptcha/api/siteverify";  
        $secret='6LdriBAUAAAAAFTwQuLozpK9V2qsZAA5gTtBV60G';    
        $ip=$_SERVER['REMOTE_ADDR'];
        $url=$google_url."?secret=".$secret."&response=".$recaptcha."&remoteip=".$ip;
        $resp=getCurlData($url); 
        if($resp['success']) {
            $db->fetchVal("insert into  log (`user_id`,`amount`) values (?,?) ", [$id, $bonval]); } 
        } else {
            $_SESSION['views'] = $_SESSION['views']-1;
        }
    }
}
?>

我收到此错误:

  

插入log(`user_id`,`amount`)值(?,?)数组([0] =&gt; 9 [1] =&gt;)数组([0] =&gt; 23000 [1] = &gt; 1048 [2] =&gt;列&#39;数量&#39;不能为null)数组([0] =&gt;数组([file] =&gt;

此代码有什么问题,或者有更好的方法可以做到这一点?

1 个答案:

答案 0 :(得分:1)

以下是一些可以尝试的事情。创建一些函数以解除脚本杂乱并在表单中提交所需的变量或分配给$_SESSION

function hasView()
    {
        # Use an array instead of a bunch of OR
        $filter =   array(2,5,10,20,50);
        return (in_array($_SESSION['views'],$filter));
    }

# Create a switch here, but you may want a default otherwise it will be null
# if none of the conditions are met
function getBonVal($first,$second,$third)
    {
         switch($_SESSION['views']) {
             case(2):
                return $first;
            case(5):
                return $third;
            case(10):
                return $second;
            case(20):
                return $second;
            case(50):
                return $first;
         }
    }

# Create a cURL function, no reason not to to keep this contained
function getGoogleRecaptcha($secret = 'key-here')
    {
        require_once("getCurlData.php");
        $google_url =   "https://www.google.com/recaptcha/api/siteverify"; 
        $ip         =   $_SERVER['REMOTE_ADDR'];
        $url        =   $google_url."?secret=".$secret."&response=".$recaptcha."&remoteip=".$ip;
        return getCurlData($url); 
    }

if(!isset($_SESSION['views'])) 
    $_SESSION['views'] = 0;
# This will increment by 1
$_SESSION['views']++;
# If you don't need these anywhere else in the page,
# maybe put them into the function
$first  =   mt_rand(10,20);
$second =   mt_rand(40,50);
$third  =   mt_rand(60,70);
# Use the function instead of all the OR clauses
if (hasView()) { 
    # Get the value here instead of in the middle of the html
    $bonval =   getBonVal($first,$second,$third);
    # Set value to session
    $_SESSION['bonval'] = $bonval;
?>
<div class="popup">
    <form role="form" method='post' action='test.php'>
        <fieldset>
            <h2 class="blink_me" style="color:green;font-size:40px;"><?php echo $bonval ?></h2>
            <div class="form-group">
                <center>
                    <div class="g-recaptcha" data-sitekey="sitekey"></div>
                </center>
            </div>
            <div class="row">
                <center>
                    <input type="submit" name="submit" class="btn btn-lg btn-success btn-block" value="Submit">
                </center>
            </div>
        </fieldset>
    </form>
</div>
<?php
}

if(isset($_POST['submit'])) {
    if(!empty($recaptcha)) {
        # Use the recaptcha function here
        $resp   =   getGoogleRecaptcha();
        if($resp['success']) {
            # Capture value from session
            $bonval =   $_SESSION['bonval'];
            # Insert normally
            $db->fetchVal("insert into log (`user_id`,`amount`) values (?,?)", array($id, $bonval));
        }
    }
    else {
        # This will auto-decrease by 1
        $_SESSION['views']--;
    }
}