简体   繁体   中英

IF and ELSE statement not working

I am trying to award a user a badge if their points are 10,000. There is a field in the table called badge1 with a default value set to locked and a points row. I am running and if statement that if the users points are 10,000 then UPDATE the badge1 row from locked to unlocked. My code seems correct but It is neither updating the the field nor showing any errors.

<?php
$db = new PDO('mysql:host=hostname;dbname=databasename;charset=UTF-8', 'username', 'password');
$username = $_SESSION['username'];
$q = "SELECT Points FROM login_users WHERE username ='$username'");
$r = mysql_query($q);
$row = mysql_fetch_assoc($r);
$Points = $row['Points'];


if($Points == "10000") {
   $awardBadge = $db->exec("UPDATE login_users SET badge1=unlocked WHERE username=?");
$Points->execute(array($username))
} else {
    print "";
}

?> 

UPDATE:

I managed to get it working.. however the problem is I am a bit new to converting old sql to PDO so this is not very secure but this is what works:

<?php
$connect = mysql_connect("host","username","password");
mysql_select_db("databasename");
$username = $_SESSION['jigowatt']['username'];
$q = "SELECT Points FROM login_users WHERE username = ('$username')";
$r = mysql_query($q);
$row = mysql_fetch_assoc($r);
$Points = $row['Points'];

?> 

// Place somewhere
<?php
if($Points >= "10000") {
    $result = mysql_query("UPDATE login_users SET maneki='unlocked' WHERE username='$username'");
} else {
    print "Badge has not been unlocked";

}
?>

"10000" string should be an 10000 int

And also, you might want to make a choice here too. You're using 2 types of setting up a mysql-database connection. the old-fashioned mysql_function() way and the new fancy PDO method .

I think working with the PDO version is safer, since newer PHP versions will not support the old methods anymore... That... and it just looks dirty ;P

Try this:

<?php

session_start();

$dbSession = new PDO('mysql:host=***;dbname=***', '***', '***');

$selectQuery = $dbSession->prepare('
  SELECT `User`.`Points`
  FROM `login_users` AS `User`
  WHERE `User`.`username` = :username
');
$selectQuery->bindParam(':username', $_SESSION['username'], PDO::PARAM_STR);

$user = $selectQuery->fetch(PDO::FETCH_ASSOC);

if ( !empty($user) && $user['Points'] == 10000 ) {
  $updateQuery = $dbSession->prepare('
    UPDATE `login_users`
    SET `badge1` = \'unlocked\'
    WHERE `username` = :username');
  $updateQuery->bindParam(':username', $_SESSION['username'], PDO::PARAM_STR);
  $updateQuery->execute();
}

?> 

Usefull resources:

Better check if >= 10000 and not yet awarded. That could you also be done in SQL so you don't need that logic in PHP.

UPDATE login_users SET badge1=unlocked WHERE points >= 10000 and badget1 <> unlocked

Try this if($Points == 10000) instead of if($Points == "10000")

mysql_query() sends a unique query (multiple queries are not supported) to the currently active database on the server that's associated with the specified link_identifier.

if($Points==10000){
    $awardBadge = $db->prepare("UPDATE login_users SET badge1=unlocked WHERE username=?");
    $awardBadge->execute(array($username));
}

The issue is caused by $point value which actually is not equal to 10000 , but is NULL .

So I propose to always use var_dump() to get the actual value of the variable in such cases.

one tip: check the PDO docs, before you write php code! You use PDO and mysql commands on same time for same job!?? why???

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM