为什么我的其他声明不起作用,但第一个声明是?

时间:2018-05-06 19:10:51

标签: php mysql sql

我不明白即使第一个if语句为false,第二个if语句也不会起作用。当然,其他声明应该是“如果上述陈述是真的,那就什么都不做。如果我上面的陈述是假的,那么就这样做”。我曾经一起尝试过,但仍然没有任何喜悦:S

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

/*This is the code that refuses to work for me with the else statement. 
  It works if I take it out but the problem is it allows to go -tokens 
  on the user balance even though the first if error message shows.*/

else {
  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');
?>

0 个答案:

没有答案