Jump to content
MakeWebGames

Little Direction


MNG

Recommended Posts

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 by MNG
updated case sensitive
Link to comment
Share on other sites

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 by MNG
Link to comment
Share on other sites

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?

 

Screenshot 2020-06-06 at 13.32.31.png

Edited by sniko
  • Like 4
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...