www.webdeveloper.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 17

Thread: Help with PHP + SQL

  1. #1
    Join Date
    Dec 2010
    Posts
    9

    Help with PHP + SQL

    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.

  2. #2
    Join Date
    Jul 2010
    Location
    /ramdisk/
    Posts
    865
    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:

    $link = mysql_connect("mysql_server[: port]", "mysql_user", "mysql_password");

    mysql_select_db("YOUR_DATABASE_NAME");

    echo mysql_errno($link) . ": " . mysql_error($link). "\n";

    $resource = mysql_query("SELECT * from table")
    while($sql_record = mysql_fetch_array($resource)) {
    print_r($sql_record);
    }
    mysql_num_rows($resource);

    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.

  3. #3
    Join Date
    Dec 2010
    Posts
    9
    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?

  4. #4
    Join Date
    Jul 2010
    Location
    /ramdisk/
    Posts
    865
    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.

  5. #5
    Join Date
    Dec 2010
    Posts
    9
    Okay. So let me get this straight.

    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

    And go on and on?

  6. #6
    Join Date
    Jul 2010
    Location
    /ramdisk/
    Posts
    865
    Like this:

    Code:
    CREATE TABLE games (
      game_ID int(11) NOT NULL auto_increment,
      game_name varchar(250) default NULL,
      genre varchar(250) default NULL,
      PRIMARY KEY  (game_ID)
    );
    
    INSERT INTO games (game_ID, game_name, genre) VALUES
    (1, 'stickman', '1'),
    (2, '10th Frame Bowling', '2'),
    (3, 'Alien Cabal', '3'),
    (4, 'Alien Carnage - Halloween Harry', '4'),
    (5, 'Alley Cat', '5'),
    (6, 'Astrofire', '5'),
    (7, 'Batman: The Movie', '4'),
    (8, 'Big Rig', '6'),
    (9, 'Blackthorne', '4'),
    (10, 'Blake Stone: Aliens of Gold', '3'),
    (11, 'Blockout', '5'),
    (12, 'Body Blows', '5'),
    (13, 'Bombs and Bugs', '5'),
    (14, 'Boppin''', '7'),
    (15, 'Bubble Pop', '5'),
    (16, 'California Games II', '2'),
    (17, 'Cannon Fodder', '5'),
    (18, 'Capture the Flag', '6'),
    (19, 'Carmageddon', '8'),
    (20, 'Commander Keen 1: Marooned on Mars', '4'),
    (21, 'Commander Keen 4: Secret of the Oracle', '4'),
    (22, 'Commander Keen 6: Aliens Ate My Babysitter', '4'),
    (23, 'Cosmo''s Cosmic Adventure', '4'),
    (24, 'Countdown', '9'),
    (25, 'Crystal Caves', '4'),
    (26, 'Cyberdogs', '5'),
    (27, 'Cybersphere', '5'),
    (28, 'Dangerous Dave', '4'),
    (29, 'Death Rally', '8'),
    (30, 'Doom', '3'),
    (31, 'Duke Nukem I', '4'),
    (32, 'Duke Nukem II', '4'),
    (33, 'Duke Nukem 3D', '3'),
    (34, 'Dune 2', '6'),
    (35, 'Dungeon Master', '10'),
    (36, 'DX Ball', '5'),
    (37, 'Executioners', '5'),
    (38, 'Extacy', '11'),
    (39, 'Face Off!', '2'),
    (40, 'Farocar', '7'),
    (41, 'Fuzzy''s World of Miniature Space Golf', '2'),
    (42, 'Gladiator', '10'),
    (43, 'Gobliiins', '9'),
    (44, 'God of Thunder', '7'),
    (45, 'Grandmaster Chess', '6'),
    (46, 'Grand Theft Auto', '8'),
    (47, 'Heartlight', '7'),
    (48, 'Hocus Pocus', '4'),
    (49, 'Indiana Jones and the Last Crusade', '9'),
    (50, 'Invaders 1978', '5'),
    (51, 'Iron Blood', '4'),
    (52, 'Isle Wars', '6'),
    (53, 'Jazz Jackrabbit', '4'),
    (54, 'Jetpack', '5'),
    (55, 'KGB', '9'),
    (56, 'Kings of the Beach', '2'),
    (57, 'Krusty''s Super Funhouse', '4'),
    (58, 'Lakers vs. Celtics and the NBA Playoffs', '2'),
    (59, 'Laser Light', '7'),
    (60, 'Lemmings', '7'),
    (61, 'LHX Attack Chopper', '11'),
    (62, 'Liero', '5'),
    (63, 'Loader Larry', '7'),
    (64, 'Lord of the Rings, Volume One', '10'),
    (65, 'Lotus 3 - The Ultimate Challenge', '8'),
    (66, 'Mad TV', '6'),
    (67, 'Mansion', '5'),
    (68, 'Math Rescue', '11'),
    (69, 'Maupiti Island', '9'),
    (70, 'Micro Machines 2: Turbo Tournament', '8'),
    (71, 'Monopoly', '11'),
    (72, 'Monster Bash', '4'),
    (73, 'Nethack', '10'),
    (74, 'Oilcap', '7'),
    (75, 'One Must Fall 2097', '5'),
    (76, 'One on One: Michael Jordan vs. Larry Bird', '2'),
    (77, 'Pacman', '5'),
    (78, 'Paperboy 2', '5'),
    (79, 'Pinball Dreams', '5'),
    (80, 'Prince of Persia', '4'),
    (81, 'Raptor: Call of the Shadows', '5'),
    (82, 'Risk', '6'),
    (83, 'Roadrash', '8'),
    (84, 'Rodeo Games', '2'),
    (85, 'Sam and Max Hit the Road', '9'),
    (86, 'Sango Fighter', '5'),
    (87, 'Scorched Earth', '11'),
    (88, 'Secret Agent Man', '4'),
    (89, 'Sensible World of Soccer', '2'),
    (90, 'Shadow Knights', '4'),
    (91, 'Sherlock', '7'),
    (92, 'Shooting Gallery', '5'),
    (93, 'Simcity 2000: Second Edition', '6'),
    (94, 'Skyroads', '5'),
    (95, 'Skyroads Xmas', '5'),
    (96, 'Solar Hockey League', '2'),
    (97, 'Space Junkie', '5'),
    (98, 'Speedball 2: Brutal Deluxe', '2'),
    (99, 'Stone Age', '7'),
    (100, 'Street Rod', '8'),
    (101, 'Super Nibbly', '7'),
    (102, 'Tank Wars', '11'),
    (103, 'Teen Agent', '9'),
    (104, 'Tetris Pro', '5'),
    (105, 'The Catacomb Abyss', '3'),
    (106, 'The Elder Scrolls - Chapter One: The Arena', '10'),
    (107, 'The Lost Vikings', '4'),
    (108, 'The Oregon Trail Deluxe', '6'),
    (109, 'The Simpsons: Bart vs. The Space Mutants', '4'),
    (110, 'Tomb Raider', '3'),
    (111, 'Trog', '5'),
    (112, 'Ugh!', '5'),
    (113, 'Ultima 1', '10'),
    (114, 'Virtual Pool', '2'),
    (115, 'Wacky Wheels', '8'),
    (116, 'Warcraft I: Orcs and Humans', '6'),
    (117, 'Wayne''s World', '9'),
    (118, 'Wheel of Fortune', '11'),
    (119, 'Wolfenstein 3D ', '3'),
    (120, 'WormWars 3', '5'),
    (121, 'Worms', '6'),
    (122, 'XQuest 2', '5'),
    (123, 'Zona 0', '5');
    
    CREATE TABLE members (
      member_ID int(11) NOT NULL auto_increment,
      username varchar(50) default NULL,
      `password` varchar(20) default NULL,
      email varchar(100) default NULL,
      PRIMARY KEY  (member_ID)
    );
    
    INSERT INTO members (member_ID, username, `password`, email) VALUES
    (1, 'one', '5f4dcc3b5aa765d61d83', 'email@email.com'),
    (2, 'testuser', 'e10adc3949ba59abbe56', 'email1@domain.com'),
    (3, 'demo', 'fe01ce2a7fbac8fafaed', NULL),
    (4, 'firstuser', '5f4dcc3b5aa765d61d83', NULL),
    (5, 'domino_master', '98a6a6255fc62ffa4eac', NULL),
    (6, 'eval(BadCode)', '5fcfd41e547a12215b17', NULL),
    (7, 'someone', '0d107d09f5bbe40cade3', NULL),
    (8, 'thepoet', 'e99a18c428cb38d5f260', NULL);
    
    CREATE TABLE saved_games (
      gamesave_ID int(11) NOT NULL auto_increment,
      member_ID int(11) NOT NULL,
      game_ID int(11) default NULL,
      gamesave_data varchar(1000) default NULL,
      PRIMARY KEY  (gamesave_ID)
    );
    
    INSERT INTO saved_games (gamesave_ID, member_ID, game_ID, gamesave_data) VALUES
    (1, 2, 112, 'VPOSA#O#*SIX@REDS<<UAVMH(HEL&DUBRWLXAC%@RSDAKEQ^@BA%ZNNQYH^WWNAIDLEDIFQFJE(UD*CBF$&SF&L@!JS!H'),
    (2, 5, 67, 'IA'),
    (3, 2, 20, 'ZDH&EXFPK<HI#CNDPC&VAZGOHW#U(THJINEWBSE^SGCDF@Q$QOYOA^@@<G(YSBXEGVZAPHFDCF@IUQMQ!MFGETTEWHEJ'),
    (4, 4, 10, '!&D#BIIDAYQ%RU#CQHBQBVA*M@RBTYA%OAKUEPA*RJAGEA#S<MF%DDJDCEU@R%YG@RFDV(D!B(!G*H&*LIAGH!Z$*%'),
    (5, 1, 61, '$DKA*BDFG<IGC&HOEEBRPAM(TUOO#N^FA&UAVZIOGTBVE*SKO!!Q&GU<##*DCFCUHA*M(MJUAPTLG'),
    (6, 1, 23, 'POABHE*VCX*SV*SBAPFQ*FBE<C<XHKXCEF$WOXMBFXA$HF(C<FFA(WDMIVLAKUHMOI!YZQU$CL%ORKB$LEDB&'),
    (7, 6, 35, 'TO<Z$CPOD!^VEQDOGFOM'),
    (8, 3, 78, 'HG$!<PGEGHBMVISGAIIXFUA&CF'),
    (9, 5, 18, 'HEP<*LIIXC(^MAZ^FIH^'),
    (10, 5, 73, 'CEU^BCEDNURUSXIHEJGSHGRIZ<YFRD^!CA$IXCMQ((O$M<UHFAY#VCU<Q&'),
    (11, 5, 69, 'NRYG!!ZBSS(IV*$KIUY'),
    (12, 6, 45, 'S&TSR(^N@HLGQEEAH'),
    (13, 2, 76, 'DBQMVGO!%'),
    (14, 3, 57, '%XYYBWAJ%NLGOCL^HSBL(G%DVDCHHZCCW'),
    (15, 5, 25, '*BBSMYBEFQGSNC%((HBR(GI#LIE#*Z%SHGSALVB(#J*L(%LXHBGBIRA!HTBBGTH<(<(H<L(AHGD'),
    (16, 5, 85, 'EOQHLYRFDS&^(YITD<GKI'),
    (17, 4, 52, 'DIZP(B%^EW#QVSF!NJRIDX<WZC'),
    (18, 5, 87, 'BF'),
    (19, 5, 98, 'DD^TJ!<II(ZBIINJIDZ#AHHUBG@FAGOBT%CZL!GSHHWUBM!%@CWDJIKD*OXG@CW'),
    (20, 6, 86, 'AZ^G%AAGJX'),
    (21, 1, 39, 'D@YRDKE<T#DA!&^NHOJ&$HDE#MD&&&'),
    (22, 3, 92, 'D(BWS&DYQAVIBWEAIGX'),
    (23, 3, 67, 'C^^O%S%L!'),
    (24, 5, 15, 'TBZTL(HE!P(WQHGQFWA$XCF$OYG@CI<FAVDAYXGOJLY&!FGFFNLHAX<FOM<$Z&G(S$ZYCIXXCGWGHGP<*PFE!JWGYSRXZQE'),
    (25, 1, 12, 'EPU&OMZ#ILCI@B*IIHBHYA(M&CPHA*QYY%KFCHNZPR*!A&FYBCZBENJ#BN(CZHZ&AKBEF@B&HEEAED<IDCCN%B**YIFESZ'),
    (26, 2, 39, '#YUGC^QYXHEBU^IFGZ!ERLWCJIAFDKG*RWIL*I<C&IZJG*MYQZPQGQFDIXGXIMEDTH<^KE(ACPFD'),
    (27, 3, 17, '*D!EC*CW<IE<I%IUB%@H'),
    (28, 6, 36, 'GLIIJXF#K&A<@PRQO!G$W@!((H*KPFCSERE@^FAF#X!GIBHR$FSQG%E'),
    (29, 2, 108, 'I(WM!LFICCCDGRZC%J@FR!JO'),
    (30, 5, 55, 'BEFC%AZEC#KJ'),
    (31, 5, 65, 'QB@GR(EDEOCXRORIGWAAZVGHDLKFJSDG^HTFSEV*$GA#'),
    (32, 1, 5, 'DZE$RDNO@MJUQAFFBFGAU^SQ$<GQMBQOMI!CGHHLB@*N%AP$MB*ECIIEGSE<GE^GE@LU#YMHFGK<NTYVBW#!D'),
    (33, 6, 77, '$IA$F!&!TTU#B^FQHY@H(XCEJGNYDY!#@HRIN*FACIGDEH*&RXBSOMIICH&BUYDC#AR@LSDJS^X@Q^IABIZ&FFDDWUI@ESNA@!'),
    (34, 6, 9, 'I#WCSF($&X(JQWCDPM*XRLQYHIA(IIN^IWY<GW(EIK(HHXOTFEGLVD$(ZMTX#CLG%WB$VRBTR'),
    (35, 6, 107, 'BHP<BU&@TCUHIPHGSM#I**KRCG&LHBNFQVDUY*FGMD#EIXAR#%L^Y&^OQVRKFM&FKGQ*NHRXF@L!RUA(#HD(&Z*D$WHK'),
    (36, 5, 12, 'YCL!MP*BXG$SK%K$QHY(ES@DFYALFHKVLQ(F'),
    (37, 5, 81, '<$Z@^IFT#V&(S%FAHSR*CB^YGAEC#NWG%ALO@BZ!CXADQMBGO$HO'),
    (38, 3, 4, '@G$XI$<GN&WV^YASAE*AJYOI&ZC<TF*I*DJZSR&EI&#AQ&CZBAGS&FEQGAFAAGGDNU#IHEEM$GN#PY(OURDU'),
    (39, 1, 36, 'O(CCEIE'),
    (40, 3, 70, 'A(#JXX(E!CGICZGUHDX#G*#BAYDU#VYB%QRO<EIAKSGCR%<XGE@UD<UY*A<GIJG^!XTBRHKUZCWI&A&L%<'),
    (41, 5, 56, 'OTPEHG!SSR&EUIHS*IGTFSFAYBDUEJON$VV$F@GVTHWGAGBF#E&F'),
    (42, 3, 51, '!PFALSE<EQYABH#*SQ&QA^IE#FA$#CK*QZ&A@FLJUDEAJ'),
    (43, 5, 29, 'M$OG!F!CFDCACADTI%(D<&WTD*JKPIAWWZ&QE(G(JBKSYYFGB#RU&GGLXW'),
    (44, 5, 91, '#DY^BEEELMQDKQFSPSPIS^IE#SMXKP$%YC#XYADPHDI#I#CIAFC!S(LB*FX@J%AC%T&EMC!OZUTIJ$KGIJ'),
    (45, 4, 5, 'HZCAPLGKVC#C%AAN(BLEFBDXENAVJ@AHD#CTWBA!SPUBMFRP*YARIWZEDVNB!RGZ'),
    (46, 6, 31, 'KC<XEC&JGU*MZN#TDYJ#AB%&S*CAY@VHZE#&**EHCBSV'),
    (47, 1, 35, 'W!JQ(I!'),
    (48, 4, 76, 'HOK<FEYG#M!A'),
    (49, 4, 80, 'MI^EFQFF^AK$WEEMR$GMAA^ELA!PQ%FED@*M*BQGESGKLL^D!&AY@SY@*!IW@NBVXKLBA'),
    (50, 5, 84, '%TD<MZWH%OD#&YFBIU*&$F'),
    (51, 4, 25, '@DNPMNACF(&I*U'),
    (52, 3, 20, 'H^B*NSIBIF'),
    (53, 3, 102, 'FGSIUF%DXKHP%RTECHKIB<Q#EMS^LI$U'),
    (54, 2, 24, '$*F@IBAFT@AFEYEY*JQXZBEBH*B%S#FRED@J!ZQ'),
    (55, 4, 83, '#$GPNF'),
    (56, 4, 109, 'ZIBICHI#LIHCXF(L!HGA*$FHAGLNBXIMGGY*D#BIDKBIFCIVAGD*PNP'),
    (57, 1, 104, 'H#BGBSVCV&<$$C*%(%RI^NHHG<<GR'),
    (58, 1, 63, 'R#EBFIRO$FKFS!UQ!PFI$YCHM<SWUHCHBOQFGBCF'),
    (59, 3, 93, 'BR$KGR%@EQEXR*G(DCEBWGOM&!%GG%*(BCPH^CLEVEYGAHI^@CAGUJE$&TR%ZRZC*QWPZX^#II^VF$*B&^C&GUS*GIMEZFQ@F'),
    (60, 2, 119, 'ZMNTKVE#GICHCQ*AE!!A^IV%TPDM#BECYHH*(S#AF!EC<ZQDZCB*G!PHAMDH^#<'),
    (61, 6, 56, '&KAIBEE%M#MSRLN!ADV(!EEAL<E(C'),
    (62, 6, 18, '$QQZ@POC'),
    (63, 4, 23, 'D^#!JBL^<FIPHJEBGN<UDN$@<OGYD$CUEPGS@JM$IGLS*OEIMYF<B&QBHAHDXEDRUGWKGKALMDZDYGQ$!RCDXVFBE'),
    (64, 1, 105, 'IK<UEXO$GIVNO#GSZEE#NCGOO%PEACNUDEM*B(DQVCADMOWUHLPNS&WGPBWE&N%IBB!@$H$O<V%GHS(NE%UFKKR(T#FF%<RJ'),
    (65, 3, 78, '^PUYDJTYWGZOMO<HDZCRDGIGMXDNOOXAIALVCCHF@RCN$((ZN%ALI^MXBBODA'),
    (66, 5, 92, 'AG@DEFDBDGDNUJTM#F(Q');
    
    CREATE TABLE tags (
      tag_ID int(11) NOT NULL auto_increment,
      tag varchar(100) default NULL,
      PRIMARY KEY  (tag_ID)
    );
    
    INSERT INTO tags (tag_ID, tag) VALUES
    (1, '...'),
    (2, 'Sports'),
    (3, '3D Shooter'),
    (4, 'Sidescroller'),
    (5, 'Action/Arcade'),
    (6, 'Strategy'),
    (7, 'Puzzle'),
    (8, 'Racing'),
    (9, 'Adventure'),
    (10, 'RPG'),
    (11, 'Other');

  7. #7
    Join Date
    Dec 2010
    Posts
    9
    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.

  8. #8
    Join Date
    Jul 2010
    Location
    /ramdisk/
    Posts
    865
    Code:
    ALTER TABLE members ADD UNIQUE (
    username
    )

    PHP Code:
    session_start();
    isset(
    $_SESSION['username']) ? $user $_SESSION['username'] : include('library/loginscript.php');

    $query "SELECT 
    games.game_name, 
    saved_games.gamesave_ID 

    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.

  9. #9
    Join Date
    Dec 2010
    Posts
    9
    Okay. And so what about saving from the game?

    I might be able to figure it out... Like...

    PHP Code:
    // login stuff here


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

  10. #10
    Join Date
    Dec 2010
    Posts
    9
    Since I didn't see the edit button... I'm gonna double post (sorry).

    I fixed the script. It now totally works for me... Now... Just the point of saving games to accounts...

  11. #11
    Join Date
    Jul 2010
    Location
    /ramdisk/
    Posts
    865
    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":

    PHP Code:
    $query "
    INSERT INTO saved_games (member_ID, game_ID, gamesave_data) VALUES
    (
    $member_id$game_id$my_save_data); 
    If you were wanting to "overwrite" a game with new data you would need to capture the primary_key for table saved_games and insert using that ID:

    PHP Code:
    $game_save_to_overwrite 5;

    $query "
    INSERT INTO saved_games (gamesave_ID, member_ID, game_ID, gamesave_data) 
    VALUES (
    $game_save_to_overwrite$member_id$game_id$my_save_data)

    ON DUPLICATE KEY UPDATE 
    game_ID=VALUES(game_ID), 
    gamesave_data=VALUES(gamesave_data)"


  12. #12
    Join Date
    Dec 2010
    Posts
    9
    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?

  13. #13
    Join Date
    Jul 2010
    Location
    /ramdisk/
    Posts
    865
    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&#37;;
    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.

  14. #14
    Join Date
    Dec 2010
    Posts
    9
    How exactly do i use these:

    PHP Code:
    $query 
    INSERT INTO saved_games (member_ID, game_ID, gamesave_data) VALUES 
    (
    $member_id$game_id$my_save_data); 
    PHP Code:
    $game_save_to_overwrite 5

    $query 
    INSERT INTO saved_games (gamesave_ID, member_ID, game_ID, gamesave_data) 
    VALUES (
    $game_save_to_overwrite$member_id$game_id$my_save_data

    ON DUPLICATE KEY UPDATE 
    game_ID=VALUES(game_ID), 
    gamesave_data=VALUES(gamesave_data)"

    I tried the first one, but it doesn't work? I also did an "mysql_query($query)"

  15. #15
    Join Date
    Jul 2010
    Location
    /ramdisk/
    Posts
    865
    I would just fill in the variables and run it from a terminal.
    Run it from >mysql and get the real error
    or you can try this:
    PHP Code:
    echo mysql_errno($link) . ": " mysql_error($link) . "\n"
    $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.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles