gpt4 book ai didi

php - 当 if 返回 false 时,如何退出 if 语句而不影响 php 的其余部分

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

//这让我发疯,哈哈..下面要执行的代码。我试图在一个 if 语句为 true 时退出脚本,如果为 false 则继续执行

<?php

require('includes/config.php');

$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";

$selectQuery = "SELECT username, email, tokens, tokenstatus, tokensinc, tokenstatus1, tokenswith, tokenstatus2 FROM members WHERE username = '".($_SESSION['username']) ."'";

$updateQuery = "UPDATE members SET tokens=tokens - 10, tokenstatus='(Pending Tokens)' ,tokenswith=tokenswith + 10, tokenstatus1='(Pending Tokens)' WHERE username = '".($_SESSION['username']) ."'";

// Create connection
$db = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$db) {
die("Connection failed: " . mysqli_connect_error());
}

//if not logged in redirect to login page
if(!$user->is_logged_in()) { header('Location: login.php'); exit(); }

//define page title
$title = 'Withdraw ~ Pixel Jag';

//include header template
require('layout/header.php');
?>
<div class="container">
<div class="panel panel-default">
<?php


$result = mysqli_query($db, $selectQuery);

if (mysqli_num_rows($result) > 0) {
// output data of each row
echo "<ul class='list-group'>";
while($row = mysqli_fetch_assoc($result)) {

//下面的if语句是要执行的代码,如果返回true我希望它退出脚本,如果它返回false我希望它继续更新//

  if ($row['tokens'] < 10) {
echo "<li class='list-group-item list-group-item-danger'>You do not have enough tokens!</li>";
}
}
echo "</ul>";

}

//请参阅下面的代码,如果上面返回 false,我希望执行相反的代码//

if (mysqli_query($db, $updateQuery)) {
echo "<ul class='list-group'><li class='list-group-item list-group-item-success'><h4>Record updated successfully!</h4></li></ul>";
} else {
echo "Error updating record: " . mysqli_error($db);
}

mysqli_close($db);
?>

<div class="panel-footer">
<a href="index.php"><h4>Back To Dashboard..</h4></a>
</div>
</div>
</div>
<?php
//include header template
require('layout/footer.php');
?>

最佳答案

请添加一个标志$isValid,默认值为true

$isValid = true;

while(mysqli_num_rows($result) > 0 || $isValid) {
if ($row['tokens'] < 10) {
echo "<li class='list-group-item list-group-item-danger'>You do not have enough tokens!</li>";
$isValid = false;
}
}

好吧,如果标志为真,则进行更新

关于php - 当 if 返回 false 时,如何退出 if 语句而不影响 php 的其余部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50201045/

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