Tenaciousmug Posted June 17, 2010 Share Posted June 17, 2010 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); } ?> Quote Link to comment Share on other sites More sharing options...
Psycho Posted June 17, 2010 Share Posted June 17, 2010 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); } ?> Quote Link to comment Share on other sites More sharing options...
severndigital Posted June 17, 2010 Share Posted June 17, 2010 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". ** Quote Link to comment Share on other sites More sharing options...
Tenaciousmug Posted June 17, 2010 Author Share Posted June 17, 2010 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. Quote Link to comment Share on other sites More sharing options...
Psycho Posted June 17, 2010 Share Posted June 17, 2010 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. Quote Link to comment Share on other sites More sharing options...
Tenaciousmug Posted June 18, 2010 Author Share Posted June 18, 2010 EDIT I got it fixed. Nevermind. 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"); ?> Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted June 18, 2010 Share Posted June 18, 2010 It may be in the include()ed file, but you need to have session_start(); at the beginning of any script that will use $_SESSIONs. Quote Link to comment Share on other sites More sharing options...
Tenaciousmug Posted June 18, 2010 Author Share Posted June 18, 2010 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? Quote Link to comment Share on other sites More sharing options...
Soldier Jane Posted June 18, 2010 Share Posted June 18, 2010 Try changing your query to UPDATE rather than SET. Quote Link to comment Share on other sites More sharing options...
Tenaciousmug Posted June 18, 2010 Author Share Posted June 18, 2010 Try changing your query to UPDATE rather than SET. Changing SET to UPDATE didn't help. Hmmm... I'm super stumped. Quote Link to comment Share on other sites More sharing options...
Soldier Jane Posted June 18, 2010 Share Posted June 18, 2010 Sorry, in my tired state I misread something, you should ignore what I said before, haha. Are you getting any error messages or just observing the the data in the database? Quote Link to comment Share on other sites More sharing options...
Tenaciousmug Posted June 18, 2010 Author Share Posted June 18, 2010 I'm not getting any error messages at all. I'm just trying to see what I'm doing wrong to make it only go through the first time you get a six.. The RP was originally at 0 and then jumps up to 500 and then stops no matter if you roll a six again. And it's ok Quote Link to comment Share on other sites More sharing options...
Soldier Jane Posted June 18, 2010 Share Posted June 18, 2010 Just noticed something, try this: $sql = "UPDATE `Member` SET `rp` = 'rp + $winnings' WHERE `username` = '$username'"; Quote Link to comment Share on other sites More sharing options...
Tenaciousmug Posted June 18, 2010 Author Share Posted June 18, 2010 Ok when I do that, it subtracts it back to 0. When I take those little apostrophes away before and after the plus sign. I gave myself 5000 and when I rolled a six, it set it back down to 0.. Quote Link to comment Share on other sites More sharing options...
Psycho Posted June 18, 2010 Share Posted June 18, 2010 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. Quote Link to comment Share on other sites More sharing options...
Tenaciousmug Posted July 28, 2010 Author Share Posted July 28, 2010 The whole statement is in double quotes so you have to put that in single quotes or it will give you an error.. :| Quote Link to comment Share on other sites More sharing options...
Psycho Posted July 28, 2010 Share Posted July 28, 2010 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. Quote Link to comment Share on other sites More sharing options...
Tenaciousmug Posted July 28, 2010 Author Share Posted July 28, 2010 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 Quote Link to comment Share on other sites More sharing options...
Psycho Posted July 28, 2010 Share Posted July 28, 2010 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. Quote Link to comment Share on other sites More sharing options...
Tenaciousmug Posted July 28, 2010 Author Share Posted July 28, 2010 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 Quote Link to comment Share on other sites More sharing options...
Psycho Posted July 28, 2010 Share Posted July 28, 2010 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. Quote Link to comment Share on other sites More sharing options...
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.