Jump to content

Recommended Posts

Wow, I have no clue what I'm doing.. I'm trying to make a random game and if you roll a 6, you will win 500 rp and I'm trying to insert the 500 rp into the user who is logged in ($_SESSION['username'). But I... just don't know where to begin.

 

Here is my crappy coding that I just.. am stumped on:

 

<p><?php
$dice = rand(1,6);
if($dice == 1){
echo "You rolled a 
<br><b>1</b>";
}if($dice == 2){
echo "You rolled a
<br><b>2</b>";
}if($dice == 3){
echo "You rolled a
<br><b>3</b>";
}if($dice == 4){
echo "You rolled a
<br><b>4</b>";
}if($dice == 5){
echo "You rolled a
<br><b>5</b>";
}if($dice == 6){
echo "You rolled a
<br><b>6</b>";
}

$winner = "500";
if($dice == 6);
{
include("haha.php");
$cxn = mysqli_connect($dbhost,$dbuser,$dbpassword,$dbdatabase);
$sql = "INSERT INTO Member (rp) VALUES ('$winner')";
mysqli_query($cxn,$sql);
}
?>

Link to comment
https://forums.phpfreaks.com/topic/205080-random-game/
Share on other sites

Well, aside from the fact that you don't needs all those IF statements, your query would simply create a record with teh value 500 in it. I would think you need a value to identify that the record belongs to this user. Not knowing what th eintention is for the database record I can't state how this should work. For example, should there be duplicate records in the database for eadch user if they win multiple times or should there be one record with the total winnings?

 

Here is revised code with all the IF statments removed and the query adjusted based upon some assumptions of the available data and fields in the table

 

<?php
$dice = rand(1,6);
echo "You rolled a<br /><b>{$dice}</b>\n";

if($dice == 6);
{
    $winnings = "500";
    include("haha.php");
    $user_id = $_SESSION['user_id'];
    $cxn = mysqli_connect($dbhost,$dbuser,$dbpassword,$dbdatabase);
    $sql = "INSERT INTO Member (user_id, rp) VALUES ('$user_id', '$winnings')";
    mysqli_query($cxn,$sql);
}
?>

Link to comment
https://forums.phpfreaks.com/topic/205080-random-game/#findComment-1073541
Share on other sites

well let's see if we can get this to work with a lot less code first.

 

how about this

$points = 500;
$winning_number = 6;

$roll = rand(1,6);

$message = '<h1>you rolled a' . $roll . '.'</h1>;
if($roll == $winning_number)
{
//they won .. do what ever you want here.
$message .= '<h1>HEY .. YOU WON 500 points</h1>';
}

try that and see if it does what you want first. then try to insert the db information.

 

P

 

**EDIT .. sorry i didn't catch the "someone already posted message". **

Link to comment
https://forums.phpfreaks.com/topic/205080-random-game/#findComment-1073544
Share on other sites

Well, I already have a login/registration system. So I just don't know how to input it into the database to the person who is logged in.

I'm working on creating a virtual petsite. And they can only play this game 20 times a day, but I also don't know how to code that limit.. If it's simple, please let me know!

But if they roll a six, they will win 500 RP each time. And it will update the database of the RP that is assigned to their account.

I don't know if I'm making sense or not...

But the fields for the Member database are:

username, createDate, password, firstName, email, rp

 

Oh it's fine severndigital, thanks for the reply. (: I love seeing all the ways of coding so I can expand my knowledge.

Link to comment
https://forums.phpfreaks.com/topic/205080-random-game/#findComment-1073546
Share on other sites

Well, if the user is logged in then you should have a session variable set to know who the user is on subsequent pages. Also, you should also be using a userID field in the table instead of relying upon the username.

 

So, using the sample code I posted before, you would get the username from the session variable and instead of an ISERT you would want to do an update. Something like:

    $username = $_SESSION['username'];
    $sql = "UPDATE Member SET rd = rd + $winnings}";

 

Since you want to limit the user to a number of rolls per day, you have a couple of options. 1) You could store every roll into an associated table with fields such as username, roll, date. That would allow you to capture historical data. If you are absolutely sure you won't need that historical data, then you could add two fields to the Member table: lastrolldate, rollcount

 

Then when a user makes a roll you can check if the rolldate is today - if so, add the rollcount by one. If the rolldate is not today, then set it to today and set rollcount to 1.

Link to comment
https://forums.phpfreaks.com/topic/205080-random-game/#findComment-1073561
Share on other sites

EDIT

I got it fixed. Nevermind. :P Gosh I'm so glad!!

 

I'll fix that later. (: Thanks for the suggestion.

Here is how my code looks and it's not giving me any errors, but it won't update:

 

<?php
include("logincheck.php");
?>
<?php include_once("header.php"); ?>
<td width='100%' valign='top' align='center'>
<?php
$dice = rand(1,6);
echo "You rolled a<br /><b>{$dice}</b>\n";

if($dice == 6)
{
    include("haha.php");
    $cxn = mysqli_connect($dbhost,$dbuser,$dbpassword,$dbdatabase);
    $winnings = "500";
    $username = $cxn->real_escape_string($_SESSION['username']);
    $sql = "UPDATE `Member` SET `rp` = 'rp + $winnings' WHERE `username` = '$username'";
    mysqli_query($cxn,$sql);
}
?>
</td></p>
<?php include_once("footer.php"); ?>

Link to comment
https://forums.phpfreaks.com/topic/205080-random-game/#findComment-1073695
Share on other sites

Aww thanks for that. (:

Buuuuut... it will only add the 500 once now.

Here is the code:

 

<?php
session_start();
include("logincheck.php");
?>
<?php include_once("header.php"); ?>
<td width='100%' valign='top' align='center'>
<center><?php
$dice = rand(1,6);
echo "You rolled a<br /><b>{$dice}</b>\n";

if($dice == 6)
{
    include("haha.php");
    $cxn = mysqli_connect($dbhost,$dbuser,$dbpassword,$dbdatabase);
    $winnings = "500";
    $username = $cxn->real_escape_string($_SESSION['username']);
    $sql = "UPDATE `Member` SET `rp` = 'rp' + '$winnings' WHERE `username` = '$username'";
    mysqli_query($cxn,$sql);
}
?></center>
</td></p>
<?php include_once("footer.php"); ?>

 

Anyone know why it only adds once and then stops even if i roll another 6?

Link to comment
https://forums.phpfreaks.com/topic/205080-random-game/#findComment-1073702
Share on other sites

The problem is that you are putting the second "rp" in single quotes!!! That tells MySQL you wat to set the value to a STRING. Since the field is obviously a numeric field the string is being interpreted as 0. You need to reference the MySQL field to be added to the $winnings, not the string "rp"

UPDATE `Member`
SET `rp` = `rp` + $winnings
WHERE `username` = '$username'

 

Of course, you would see the errors if you had added error handling to your database calls.

Link to comment
https://forums.phpfreaks.com/topic/205080-random-game/#findComment-1073751
Share on other sites

  • 1 month later...

The whole statement is in double quotes so you have to put that in single quotes or it will give you an error.. :|

 

What are you smoking? This thread is over a month old and your comment is completely false. There is no reason you can't define the query using double quotes.

Link to comment
https://forums.phpfreaks.com/topic/205080-random-game/#findComment-1091933
Share on other sites

I know. I had to stop coding for a month, but Im back now so I still needed to solve this issue...? No reason to get rude.. 0.o People these days...

 

I used double quotes. Trust me. I wouldn't not use someones advice and say it doesn't work without testing it. 0.o

Link to comment
https://forums.phpfreaks.com/topic/205080-random-game/#findComment-1091937
Share on other sites

I used double quotes. Trust me. I wouldn't not use someones advice and say it doesn't work without testing it. 0.o

 

Nice use of a double negative. Anyway, I didn't say your advice wouldn't work only that saying it HAD to use single quotes was a false statement. You can create any string with single or double quotes (or even the heredoc method). You just have to be aware of how variables and escaped characters are handled differently between the two.

Link to comment
https://forums.phpfreaks.com/topic/205080-random-game/#findComment-1092141
Share on other sites

Haha I dont care. And cant you see this thing is solved? No reason to post anymore.

It was actually because I had 'rp' + $winnings when it should have been rp+$winnings so it was one string. So it wasnt any of your suggestions.. 0.o And I have a book and it says you cant use double quotes inside double quotes. You have to use single quotes and I used double quotes and it gives you an error message so maybe you guys are wrong 0.o

Anywhoo, this is solved so just let it die. Jeeezz. xD

Link to comment
https://forums.phpfreaks.com/topic/205080-random-game/#findComment-1092168
Share on other sites

It was actually because I had 'rp' + $winnings when it should have been rp+$winnings so it was one string. So it wasnt any of your suggestions.. 0.o And I have a book and it says you cant use double quotes inside double quotes.

 

I identified the exact problem back on reply #14

The problem is that you are putting the second "rp" in single quotes!!! That tells MySQL you wat to set the value to a STRING.

and I provide an example of how a correct query might look.

 

You have a book? Most programming books are riddled with errors and should not be considered definitive by any means. Case in point:

...it says you cant [sic] use double quotes inside double quotes.

 

That statement is false. You CAN use double quotes in double quotes - if you know what you are doing and properly escape the quotes.

 

$str = "Here is a string with \"double quotes\" inside \"double quotes\".";
echo $str;
//output: Here is a string with "double quotes" inside "double quotes".

 

I just don't want to just "let this die" if there is inaccurate information that someone else looking at this post for a similar issue may take as fact.

Link to comment
https://forums.phpfreaks.com/topic/205080-random-game/#findComment-1092203
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.