我已经尝试了很多方法来使这个功能正常工作。我已经搜索了整个stackoverflow,看看是否有针对我遇到的特定问题的解决方案,但我无处可去。当用户点击重置按钮时,我想将会话变量重置为 0,这只是一个提交按钮。我尝试将其更改为 get,然后将 if 语句设置为从服务器中提取以查看请求是否为 GET。我对 POST 做了同样的事情,但没有运气。会话只是保留,根本不会破坏。就像它只是忽略了 if 语句,因为我尝试使用 echo 语句进行测试,但没有得到任何响应。关于可能导致问题的任何想法?我正在填充会话变量,然后将其设置为一个名为$db_value
. 我这样做的原因是我可以将结果写入数据库,然后在用户继续播放时再次从数据库中提取。
<?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');
}