Huntrr / Chess42

[JS/PHP/MySQL] Very simple (read: insecure) online multiplayer chess interface
MIT License
0 stars 0 forks source link

SQL Tables & PHP Error #1

Closed lol123Xb closed 8 years ago

lol123Xb commented 8 years ago

Do I not need any tables or anything? because apparently without the tables in phpmyadmin, the whole stuff will screw up.

By screw up, I meant the error: Fatal error: Call to a member function bind_param() on a non-object in xxxx on line 22

Huntrr commented 8 years ago

Unsure of what you mean by

the whole stuff will screw up

Regardless:

src/play.php and src/start.php have this:

$USERNAME = "FAKE USERNAME";
$PASSWORD = "FAKE PASSWORD";
$HOST = "FAKE HOST";
$MAIN_TABLE = "FAKE TABLE";

which you need to adapt to your set up (filling in credentials for the db, the url for the $HOST and the name of the table to use in $MAIN_TABLE. I haven't worked with this code in a long time, so I don't really remember it, but looking through it now, it doesn't look like I have any code that sets up the table for you, so you will have to create your own table. I think you can do this easily from phpmyadmin. Looking at the code, I see when it creates a new game, it does:

$stmt1 = $db->prepare("INSERT INTO $MAIN_TABLE (ind, pos, side, undos, pgn, sandbox) VALUES (?, ?, ?, ?, ?, ?);");
$stmt1->bind_param('issiii', $uuid, $pos, $side, $undos, $pgn, $sandbox);

Which means whatever table you use for the site needs 6 fields.

  1. An INT field called ind (this is the index of the game, INT32 or INT64 is fine, probably doesn't matter for your purposes).
  2. A VARCHAR field called pos (this is the current position of the game)
  3. A VARCHAR field called side
  4. A INT field called undos which is a boolean switch for whether or not undos are allowed
  5. A INT field called pgn which is a boolean switch for weather or not the pgn is displayed at the bottom of the game screen
  6. A INT field called sandbox which is a boolean switch for whether or not the rules of chess are enforced

Again, haven't touched this code in a while, so I don't really recall the whole thing. It's old and clunky and hacky and not super secure. But hopefully the above stuff helps you do whatever you're doing. If it does—and if you're interested in programming—I recommend you try to do something like this on your own at some point! This was my project for learning php/mysql, and I did a similar thing over at https://github.com/Huntrr/Chess-on-Rails to learn Ruby on Rails. The php here isn't super complicated, and hopefully you can learn something from it! Happy hacking.

Hope that helps!

lol123Xb commented 8 years ago

Ahh yes, thanks. This should hopefully work a lot better.

lol123Xb commented 8 years ago

It works now but the redirecting just brings me to an error 404 page every time i press play now.

Huntrr commented 8 years ago

Yeah, the code is, again, pretty hacky and old and specifically tailored to my particular needs at the time. I'm guessing your problem comes from here:


        $host = $_SERVER['HTTP_HOST'];
        $uri = rtrim(dirname($_SERVER['PHP-SELF']), '/\\');
        $page = 'chess/play.php';
        header("Location: http://$host$uri/$page?id=$uuid&side=$side");

        exit;

that's the redirect after you submit the form. You'll notice $page='chess/play.php'. So this code assumes you've got the game in a chess/ subdirectory of your webserver (i.e. it's at yourdomain.com/chess/index.php. That might not be the case for you, in which case you'll have to change the $page variable to match your specific use case.

lol123Xb commented 8 years ago

yeah, i just realised it xD

thanks tho.

i like your code even tho its hackable 👍

10/10 will follow more of ur stuff.