I’ve tried numerous things to make this functionality work. I’ve searched all over stackoverflow to see if there was a solution for the particular issue that I’m having and I’m getting no where. I was wanting to reset the session variable to have the value of 0 when the user hit’s the reset button, which is simply just a submit button. I’ve tried changing it to a get and then setting the if statement to pull from the server to see if the request is GET. I did the same with POST and had no luck. The session just remains and won’t destroy at all. It’s like it’s just neglecting the if statement because I’ve tried doing a test with an echo statement and get nothing in response. Any ideas of what might be causing the issue? I am populating the session variable then setting it to a variable called $db_value. The reason I did that is so that I can write the result to the database and then pull again from the database when the user continues playing.
<?php session_start();
$host = "localhost";
$user = "username here";
$pass = "";
mysql_connect($host, $user, $pass) or die(mysql_error());
mysql_select_db("RPS") or die(mysql_error());
mysql_query("SET SQL_SAFE_UPDATES=0;"); // allows updating of table
mysql_query("Create table if not exists RPS (score int);");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Rock, Paper, scissors</title>
</head>
<body>
</body>
</html>
<?php
if (isset($_SESSION['Score'])) {
if ($_POST['user_choice']) {
$user_choice = $_POST['user_choice'];
$Choosefrom = array(Rock, Paper, Scissors);
$Choice = rand(0, 2);
$Computer = $Choosefrom[$Choice];
//create a variable for the database to use
$q = mysql_query("SELECT * FROM RPS;");
$db_array = mysql_fetch_array($q);
$db_value = $db_array[0];
if ($user_choice == $Computer) {
echo 'Result : Draw +0';
$_SESSION['Score'] = (int) $_SESSION['Score'];
$db_value = $_SESSION['Score'];
mysql_query("UPDATE RPS SET score=$db_value;");
} else if ($user_choice == 'Rock' && $Computer == 'Scissors') {
echo 'Result : Win +1';
$_SESSION['Score'] = (int) $_SESSION['Score'] + 1;
$db_value = $_SESSION['Score'];
mysql_query("UPDATE RPS SET score=$db_value;");
} else if ($user_choice == 'Rock' && $Computer == 'Paper') {
echo 'Result : Lose -1';
$_SESSION['Score'] = (int) $_SESSION['Score'] - 1;
$db_value = $_SESSION['Score'];
mysql_query("UPDATE RPS SET score=$db_value;");
} else if ($user_choice == 'Scissors' && $Computer == 'Rock') {
echo 'Result : Lose -1';
$_SESSION['Score'] = (int) $_SESSION['Score'] - 1;
$db_value = $_SESSION['Score'];
mysql_query("UPDATE RPS SET score=$db_value;");
} else if ($user_choice == 'Scissors' && $Computer == 'Paper') {
echo 'Result : Win +1';
$_SESSION['Score'] = (int) $_SESSION['Score'] + 1;
$db_value = $_SESSION['Score'];
mysql_query("UPDATE RPS SET score=$db_value;");
} else if ($user_choice == 'Paper' && $Computer == 'Rock') {
echo 'Result : Win +1';
$_SESSION['Score'] = (int) $_SESSION['Score'] + 1;
$db_value = $_SESSION['Score'];
mysql_query("UPDATE RPS SET score=$db_value;");
} else if ($user_choice == 'Paper' && $Computer == 'Scissors') {
echo 'Result : Lose -1';
$_SESSION['Score'] = (int) $_SESSION['Score'] - 1;
$db_value = $_SESSION['Score'];
mysql_query("UPDATE RPS SET score=$db_value;");
}
echo ' You\'re score is currently: ' . $_SESSION['Score'];
echo '<a href="rps.php">Play Again ?</a>';
echo '<form method="POST" action="rps.php"><input type="hidden" name="hidden" /><input type="submit" value ="Reset" name="reset" /></form>';
if ($_POST['reset']) {
$_SESSION['Score'] = 0;
$db_value = $_SESSION['Score'];
unset($_SESSION['Score']);
session_start();
session_destroy();
mysql_query("UPDATE RPS SET score=$db_value;");
header('Location:rps.php');
}
} else if (!$_POST['user_choice']) {
echo 'Your Current Score is: ' . $_SESSION['Score'] . '<form action="rps.php" method="post" />
<input type="image" src="images/Rock.png" alt="Rock" name="user_choice" value="Rock" title="Rock" height="115" /> <br /><br />
<input type="image" src="images/Paper.png" alt="Paper" name="user_choice" value="Paper" title="Paper" height="115"/> <br /><br />
<input type="image" src="images/Scissors.png" alt="Scissors" name="user_choice" value="Scissors" title="Scissors" height="115"/> <br /><br />
</form> ';
}
} else if (!isset($_SESSION['Score'])) {
$_SESSION['Score'] = 0;
echo 'Your Current Score is: ' . $_SESSION['Score'] . '<form action="rps.php" method="post" />
<input type="image" src="images/Rock.png" alt="Rock" name="user_choice" value="Rock" title="Rock" height="115" /> <br /><br />
<input type="image" src="images/Paper.png" alt="Paper" name="user_choice" value="Paper" title="Paper" height="115"/> <br /><br />
<input type="image" src="images/Scissors.png" alt="Scissors" name="user_choice" value="Scissors" title="Scissors" height="115"/> <br /><br />
</form>';
}
?>
echo'<form method="POST" action="">
<input type="hidden" name="hidden" />
<input type="submit" value ="Reset" name="reset" />
</form>';
if (isset($_POST['reset']) && ($_POST['reset'] == "Reset")) {
$_SESSION['Score'] = 0;
$db_value = $_SESSION['Score'];
unset($_SESSION['Score']);
session_start();
session_destroy();
mysql_query("UPDATE RPS SET score=$db_value;");
header('Location:rps.php');
}
You might try $_REQUEST[‘Reset’] instead of get or post to cover your bases. Use the following sequence at the very top of your code only to destroy a session…
by and by, your form is way wrong. I doubt you’re even getting data from the values here. change it to this:
…
then on the top of the page do what i put plus a little of yours…
where you use this:
that will always return true if you write it without a condition. for the value. Plus PHP is case sensitive, so use Reset if you do it how I wrote it.
You’re not using XHTML validation, you’re using HTML 4.01 Transitional so don’t do this:
do this:
also that goes for the / on your images as well.
Final thought, use a unique value when doing your update, because your statement will change every record in the entire table. See my example with the WHERE clause.