【问题标题】:Unable to find exact value of if/else function无法找到 if/else 函数的确切值
【发布时间】:2017-05-23 07:33:59
【问题描述】:

我有这段代码并试图将$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;
        }
    }
}
?>

我收到此错误:

插入日志 (`user_id`,`amount`) 值 (?,?) 数组 ( [0] => 9 [1] => ) 数组 ( [0] => 23000 [1] => 1048 [ 2] => 列“数量”不能为空)数组([0] => 数组([file] =>

这段代码有什么问题,或者有更好的方法吗?

【问题讨论】:

  • $_SESSION['views'] 的值是多少?我还建议从代码中删除您的密钥。
  • 感谢您的重要建议...我不明白您所说的 $SESSION['views'] 值是什么意思?
  • 似乎 bonval 并不总是被设置 - 因此它通常是“null”,因为它是为 $SESSIONS['views'] 的许多值统一化的
  • $bonval 仅在弹出窗口打开时才需要,而不是每次都需要
  • 您是否单击某些内容打开弹出窗口,然后从弹出窗口提交表单?

标签: php mysql


【解决方案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']--;
    }
}

【讨论】:

  • 无法阻止用户通过刷新页面重新提交表单......任何帮助
  • 如果你愿意,我可以看看它,但如果不亲眼看到并测试它,我真的无法解决问题..
猜你喜欢
  • 1970-01-01
  • 2011-02-24
  • 1970-01-01
  • 1970-01-01
  • 2015-03-24
  • 2014-04-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多