2017-01-09 14 views
0

我有這段代碼,並試圖將$bonval轉換爲mysql插入函數,但它返回null。我在這裏做錯了什麼?無法找到if/else函數的確切值

<?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; 
     } 
    } 
} 
?> 

我得到這個錯誤:

insert into log (`user_id`,`amount`) values (?,?) Array ([0] => 9 [1] =>) Array ([0] => 23000 [1] => 1048 [2] => Column 'amount' cannot be null) Array ([0] => Array ([file] =>

什麼在此代碼是錯誤的,或者也許有更好的方法來做到這一點?

+0

$ _SESSION ['views']的值是什麼?我也會建議從代碼中刪除您的密鑰。 –

+0

感謝您的重要建議......我不明白您的價值$ SESSION ['views']是什麼意思? – john

+0

看起來bonval並不總是被設置 - 因此它通常是'null',因爲它是單元化的$ SESSIONS的許多值['views'] –

回答

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']--; 
    } 
} 
+0

無法阻止用戶重新提交頁面刷新....任何幫助 – john

+0

如果你願意,我可以看看它,但我不能真正遇到麻煩沒有看到它,並先測試它韓d .. – Rasclatt

+0

如何顯示... – john