MNG Posted June 6, 2020 Posted June 6, 2020 (edited) Been working on a house (player class) and trying to figure why it's not inserting into the user database. The purpose is once the player come to the file and their not in house then it chooses one for them but right now it keeps selecting blank. <?php include "../globals.php"; global $db, $ir, $c, $userid, $h; if($ir['Houses'] == 'None') { $Houses[] = "Grey"; $Houses[] = "Red Ox"; $Houses[] = "Miltha"; $Houses[] = "Frost"; $Houses[] = "Pleasure"; $n = array_rand($Houses); $input = "Land of ". $Houses[$n]; $sql = sprintf("UPDATE users SET Houses = '%s' WHERE (userid = %u)", $input, $userid); $db->query($sql); echo "You've been chosen to be in The {$input}!"; } else if($ir['Houses']) { echo sprintf("You are already in the %s", $ir['Houses']); $h->endpage(); exit; } $h->endpage(); ?> Edited June 6, 2020 by MNG updated case sensitive Quote
Sim Posted June 6, 2020 Posted June 6, 2020 Are these values correct? Case sensitive ir['Houses'] == 'None' Quote
MNG Posted June 6, 2020 Author Posted June 6, 2020 (edited) 18 minutes ago, Sim said: Are these values correct? Case sensitive ir['Houses'] == 'None' Yeah all correct Does it matter the table is enum? Found the problem using echo, on input I had it saying "Land of $house" so it kept inserting that instead of just $house Edited June 6, 2020 by MNG Quote
sniko Posted June 6, 2020 Posted June 6, 2020 (edited) 1. Remove the `global` stuff on line 4 - it is not needed here (plus the use of global is an antipattern) 2. Format your PHP code to make it easier to read and uniformed - php-cs-fixer is a good tool. 2a. Wtf is this spacing? So many empty lines. Look into PSR-2. 3. Generally speaking, your MySQL column names should be lower case (snake_case if needed) 4. You should have your default value as `NULL` instead of `(string) None` - it will make it easier to read and show intention of the column IMO 5. When doing comparisons, be in the habit of doing type comparisons too (ie: `===`). If doing a string comparison, cast both to the same case (ie: `strtolower()`) so you won't get unintended logic (ie: if somehow the column value is `none` instead of `None`) 6. Try to stay away from hardcoded values. Ideally you'd have a database table to lookup the house_id to the house_name 7. Get in the habit of using `require` instead of `include`. Since globals.php is an important file, you will want the script to halt if it does not exist. > Does it matter the table is enum? Yes, what are the values of this enum? Edited June 6, 2020 by sniko 4 Quote
Dave Posted June 8, 2020 Posted June 8, 2020 On 6/6/2020 at 7:33 AM, sniko said: 3. Generally speaking, your MySQL column names should be lower case (snake_case if needed) Different versions of MySQL and OSs combinations can really mess with capitalization in MySQL. So always do this, not generally 🙂 1 Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.