This is both for PHP and MySQL but I'm going to post this in the PHP section.
I am not fluent in PHP, and really don't have time to go about learning it, I'm just asking for what I think is simple help...
Okay... On to my question. My website has the whole member login system, and I modified some scripts for adding features etc (I know my way around a programming language). So the next task I wanted to do was add a user control panel.
It is a gaming website, and when they save it generates a password, so when they visit the website again, they can pickup where they left off. So what I want to do is save the passwords to their account (and what game it goes to) with a max of like 10 - 20 saved passwords.
I have no idea on how to even beginning how to do this.
If you need anymore information, please ask.
I would begin by making the SQL tables. But first, make sure you have an ERD or some kind of plan going into it. I find it easiest if the tables are normalized also, but that's entirely up to you.
Once you have that set up it's really up to you about your style of programming, but a few functions you'll be needing (for php) are:
Those are the ones that come to mind. You might also want to make use of sessions, since I know you have people logging in and messing with a control panel. You can put a lot of data into sessions if you serialize() it, then reuse that data (on another page) if you unserialize() it. That might come in handy.
For saving the game state (you do save the game state?) if the games were made in php you can serialize the object (if it's an object) and stick that into MYSQL alongside a token and user_ID.
Edit: be careful about the tokens and user_ID. If there are users I would make them log in first before putting the token in, otherwise you might end up with someone assuming the role of another user if they can generate a valid token that doesn't belong to them (make strong tokens).
Last edited by eval(BadCode); 12-07-2010 at 06:58 PM.
Thanks for replying.
The game spits out a string that it displays on a separate page.
Like this:
User Plays -> He Saves -> Save Processes -> Redirected to a page that prints the string out
I know about connecting to the database etc.
And having it store the name of the game each code is for and each code, would that require like 10 - 20 entries (per user) (depending on how many "save slots" i decide to use) one for each slot?
Yes I would recommend using 1 row/"entry" per saved game. Otherwise you're asking for trouble. If you normalize the tables to 3N it would just be
gamesave_ID, user_ID, game_ID (to join on for game_name), gamesave_data
------------
gamesave_ID = (primary key../ token)
user_ID .. do i need to explain that?
game_ID is for joining on games table to get the game name
gamesave_data is that string the game spits out (i think?)
upon saving the games, if they're already up to 20 rows then tell them to delete some or "overwrite" one (delete + insert)...
Last edited by eval(BadCode); 12-07-2010 at 08:23 PM.
I already have my table i use now called "members". In this i have, I'll use the demo account for this:
id = 2
username = demo
password = demo
email = {blank}
now i should add them like this?
slotOneGame = Adventure
slotOneSave = rjkl32kl12
Okay. I really hate asking people to do things for me... So I'm not going to...
But I do have sessions in my login scripts. Now, how would I go about saving these to their accounts, and displaying a page like this (a table, but made using the information from the MySQL table):
Game Name | Load Button
As I said, I do not know much PHP, I can figure things out, but I'm a little lost looking at long scripts as of now.
edit: I have already made the table in phpMyAdmin.
edit2: The load button would link to index.php?game=gamename&save=savecode
Last edited by brianmaurer42; 12-12-2010 at 02:22 PM.
FROM members INNER JOIN saved_games ON members.member_ID = saved_games.member_ID INNER JOIN games ON saved_games.game_ID = games.game_ID
WHERE members.username = '$user'
ORDER BY gamesave_ID ASC";
$res = mysql_query($query);
if (mysql_num_rows($res)) { $src = '<table><tr><td>Saved Game Name:</td><td>Click To Load</td></tr>';
while($row = mysql_fetch_array($res)) { $src .= '<tr><td>' . $row['game_name'] . '</td><td><a href="index.php?game=${row['game_name']}&save=${row['gamesave_ID']}" >Load Game</a></td></tr>'; } $src .= "</table>"; echo $src; } else { echo "You curren't do not have any saved games."; }
dont put the gamesave data in the URL: it's a candidate key, not primary or foreign. When the person saves their game again, you will not know which row in the table it came from. Plus people could manipulate the URL to cheat, I would just stick with "saved_games.gamesave_ID".
Last edited by eval(BadCode); 12-12-2010 at 04:29 PM.
//if user does not have save for x game $query = "INSERT INTO saved_games (save_id, mem_id, game_id, save_data) VALUES (no clue aboot the auto incrememnt thing..., $member_id, $game_id, $my_save);
Am I correct about that? I know its super simplified, because I have a Western Civilization project due Tuesday I'm trying to get as much done as i can with everyhthing lol.
And i get this when i go to the script in my browser after i uploaded it
"Parse error: syntax error, unexpected T_STRING in /home/a1656760/public_html/mysaves.php on line 25"
Last edited by brianmaurer42; 12-12-2010 at 05:25 PM.
Columns with "int(11) PRIMARY KEY AUTO_INCREMENT" will automatically insert the correct value (an incremented integer) if none is specified. You can also specify "NULL", with the same result.
If you specify one and it's a duplicate, you have more options:
INSERT IGNORE
or
INSERT ON DUPLICATE KEY UPDATE
I would not use INSERT IGNORE for saving a game (this command just ignores the duplicate insert and continues by dismissing the warning that would result from a duplicated key).
I would offer the option for the user to overwrite a game instead of deleting a game and inserting a game, so INSERT ... ON DUPLICATE KEY UPDATE is probably one you'd like to use.
It would look like this if the user wanted to use an "empty"/new "save slot":
So say I wanted to access the save_id they are using atm (to get which row to overwrite). I'd use the variable... What? Would I have it register the save_id to a variable when they load their game, for minimum effort? Or would I have to take another approach to it?
Edit: short answer is that you should save the gamesave_ID variable in the session when they load a game. It would make overwriting the same gamesave data a lot easier (instead of them having to select it).
You should maybe let them save the game with a name of their own, and a date (its less confusing that way).
Code:
ALTER TABLE saved_games
ADD gamesave_name VARCHAR( 100 ) NOT NULL,
ADD date_saved TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
// you dont need this 2nd index, but it would keep people from using duplicate gamesave_names (not that it matters).
ALTER TABLE saved_games
ADD UNIQUE (gamesave_name , member_ID);
Then when they choose to save the game (however you're going to let them do that).
I would add a div (lets call it "game_save_menu" with css something like this:
display:none;
z-index:999;
background-color: rbga(50,50,50,0.5);
position:fixed;
width:100%;
height:100%;
When the menu pops up (you just change display:none to display:block) I would add a button to dismiss "game_save_menu" (just change display:none again).
inside of the "game_save_menu" I would give them an option to overwrite a pervious game. You can display the previous gamesave_name now (that way they know what they're selecting.
If they don't want to overwrite it, it would be a simple form... Enter the new name of the saved game.
Otherwise you would have to populate a table (or something like a table) using ajax. Then let them select which game to overwrite.
Your table would already have the gamesave_ID filled into
the onclick="overwrite_game(gamesave_ID)"
Don't pass the gamesave data here. I would keep that behind the scenes (using sessions).
jquery would make it look nice, maybe have the menu fade in (it also makes ajax kind of easy).
Last edited by eval(BadCode); 12-13-2010 at 01:10 AM.
$link is your established mysql connection. You should put this line of code right after your sql_query() statement.
I'm assuming you know that you have to assign the variables the corresponding values.
The first query inserts a new "game slot".
The 2nd query overwrites a "game slot".
If you use the schema I posted. You can let people save as many games as they want (it doesn't make much of a difference to you, right?). But if you want to give that "N64 limited memory" feeling, you can limit them to 20 slots/rows for saved games. In which case the 2nd query might be useful to use when they're forced to overwrite a game_save.
Edit: if you order the select query by the primary key, it will always display the slots in the same order. Kind of a neat bonus. You could also make a game called "Empty" and write your code to handle that as an exception. Then go ahead and use the overwrite query on all 20 slots. That way when they're looking through their game_save slots, they can see 3 saved games and 17 "empty" slots, opposed to just 3 saved games. If you go this route you might benefit from reading: http://www.charles-reace.com/blog/20...ast_insert_id/
Last edited by eval(BadCode); 12-15-2010 at 12:44 AM.
Bookmarks