gpt4 book ai didi

php - 无法找到 if/else 函数的精确值

转载 作者:行者123 更新时间:2023-11-29 11:05:05 25 4
gpt4 key购买 nike

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

我收到此错误:

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] =>

这段代码有什么问题,或者也许有更好的方法来做到这一点?

最佳答案

这里有一些值得尝试的事情。创建一些函数来整理脚本并在表单中提交所需的变量或分配给 $_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']--;
}
}

关于php - 无法找到 if/else 函数的精确值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41541261/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com