-3

I am creating a page where i want to show the div based on the if condition of fetched data from database

values in database:

username formvalue
user1 11
user1 12
user1 13

Php code

sql = "SELECT formvalue, username  FROM demotable where username = '".$_SESSION['username']."' ";
      $execute = mysqli_query($con, $sql);
      if(mysqli_num_rows($execute) >= 1){
          while ($row = mysqli_fetch_assoc($execute)) {
                $checkcon = $row['formvalue'];
                
                
            }  
          }

output code i need(but it is showing only div third)

<?php if($checkcon == '11' ){ ?>
<div>
Div first
</div>
}
?>

<?php if($checkcon == '12' ){ ?>
<div>
Div second
</div>
}
?>

<?php if($checkcon == '13' ){ ?>
<div>
Div third
</div>
}
?>

b

Ankit Bajpai
  • 11,992
  • 4
  • 23
  • 40
  • 2
    _"(but it is showing only div third)"_ - presumably, because your _last_ record contained that value? You are _overwriting_ `$checkcon` in each iteration of your while loop, so if you placed the output generating code _after_ the loop, of course you would get such a result. – CBroe Jun 03 '22 at 09:47
  • 1
    **Warning:** You are wide open to [SQL Injections](https://php.net/manual/en/security.database.sql-injection.php) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/32391315) – Dharman Jun 03 '22 at 09:57

0 Answers0