-1

我已经尝试了很多方法来使这个功能正常工作。我已经搜索了整个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');
         }
4

2 回答 2

1

您可以尝试使用 $_REQUEST['Reset'] 而不是 get 或 post 来覆盖您的基础。在代码的最顶部使用以下序列仅用于销毁会话...

$session_id = session_id();

if(empty($session_id)){
 session_start();
}

if(isset($_REQUEST['Reset'])){
 session_unset();
 session_destroy();
 $_SESSION = array();
 session_start();
 session_regenerate_id();
}

渐渐地,你的表格是错误的。我怀疑你甚至从这里的值中获取数据。将其更改为:

echo ' You\'re score is currently: ' . $_SESSION['Score'];
echo '<form method="POST" action="rps.php"><input type="hidden" name="hidden" /><input type="submit" value ="Play Again" name="Reset" /></form>';

...

然后在页面顶部做我放的加一点你的...

$session_id = session_id();

if(empty($session_id)){
 session_start();
}

if(isset($_REQUEST['Reset'])){
 $_SESSION['Score'] = 0;
 $db_value = $_SESSION['Score'];
 session_unset();
 session_destroy();
 $_SESSION = array();
 session_start();
 session_regenerate_id();

 mysql_query("UPDATE RPS SET score = $db_value WHERE unique_field = $unique_field");
 header('Location:rps.php');
 exit;
}

你在哪里使用这个:

if ($_POST['reset']) {

如果您在没有条件的情况下编写它,那将始终返回 true。为价值。另外 PHP 是区分大小写的,所以如果你按照我写的方式进行操作,请使用 Reset。

你没有使用 XHTML 验证,你使用的是 HTML 4.01 Transitional 所以不要这样做:

<br />

做这个:

<br>

你的图像上的 / 也是如此。

最后的想法,在进行更新时使用唯一值,因为您的语句将更改整个表中的每条记录。请参阅我的带有 WHERE 子句的示例。

于 2012-12-07T10:35:40.773 回答
0

抱歉,这是你重写的代码……(抱歉缩进,这个所见即所得的编辑器很差而且没用……)

<?php
//error_reporting(E_ALL);
$session_id = session_id();

if(empty($session_id)){

session_start();
}
$DB_Server = "主机"; //你的 MySQL 服务器
$DB_Username = "user"; //你的 MySQL 用户名
$DB_Password = "pass"; //你的 MySQL 密码
$DB_DBName = "db"; //你的 MySQL 数据库名称
$db = mysql_connect($DB_Server, $DB_Username, $DB_Password);
// 在选择基础上
$host = $DB_Server;
$user = $DB_Username;
$pass = $DB_Password;
$db = mysql_connect($host, $user, $pass) 或死(mysql_error());
mysql_select_db($DB_DBName,$db) 或死(mysql_error());
//mysql_query("SET SQL_SAFE_UPDATES=0;"); // 允许更新表

//mysql_query("如果不存在则创建表 RPS (score int);");
/*
设置 FOREIGN_KEY_CHECKS=0;
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";

如果不存在 RPS(
得分 int(11)默认 NULL
)引擎 = MyISAM 默认字符集 = utf8,则创建表;

插入 RPS(分数)值(0);
设置 FOREIGN_KEY_CHECKS=1;*/

?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 过渡//FR" " 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-15" />

<title>石头、纸、剪刀</title>
<style type="text/ css">
.rock {

背景图片:url(pierre.png);
高度:159px;

宽度:159px;
}
.paper {

背景图片:url(feuille.png);
高度:159px;

宽度:159px;
}


剪刀{背景图像:url(ciseaux.png);
高度:159px;

宽度:159px;
}
</style>

</head>
<body>
<?php
if (isset($_SESSION['Score'])) {
if (isset($_GET['user_choice'])) {
$user_choice = $_GET['用户选择'];

$Choosefrom = array('Rock', 'Paper', 'Scissors');

srand((双)微时间()* 1000000);
$Choice = rand(0, 2); $Computer = $Choosefrom[$Choice];

//为数据库创建一个变量以使用
$q = mysql_query("SELECT score 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."'");



$_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 ' 你当前的分数是:' 。$_SESSION['分数'];
echo '<form method="GET" action="shifumi.php"> <input type="hidden" name="reset" value="Reset" />
<input type="submit" value ="再玩一次" / >

</form>';
if ($_GET['reset']) {

$_SESSION['Score'] = '0';
$db_value = $_SESSION['Score']; mysql_query("UPDATE RPS SET score='".$db_value."'");

未设置($_SESSION['Score']);
//session_start();

session_destroy();
if (!headers_sent()){

header('Location:shifumi.php');
}else{
echo '<脚本语言=\"JavaScript\">window.location=\'shifumi.php\';</script>'; }
}
} else if (!isset($_GET['user_choice'])) { echo '你的当前分数是:' . $_SESSION['分数'] 。'<form action="shifumi.php" method="GET" />

<input type="hidden" name="user_choice" value="Rock" />
<input type="submit" class="rock" value= "" alt="Rock" />
</form>
<br /><br />

<form action="shifumi.php" method="GET" />
<input type="hidden" name="user_choice" 值="Paper" />

<input type="submit" class="paper" value="" alt="Paper" />

</form>
<





</form> ';
}
} else if (!isset($_SESSION['Score'])) {
$_SESSION['Score'] = '0';

echo '您当前的分数是:' . $_SESSION['分数'] 。'<form action="shifumi.php" method="GET" />

<input type="hidden" name="user_choice" value="Rock" />
<input type="submit" class="rock" value= "" alt="Rock" />
</form>
<br /><br />
<form action="shifumi.php" method="GET" />

<input type="hidden" name="user_choice" 值="Paper" />
<input type="submit" class="paper" value="" alt="Paper" />
</form>
<br /><br />
<
form action="shifumi.php" method="GET" /> <input type="hidden" name="user_choice" value="Scissors" />

<input type="submit" class="scissors" value="" alt="剪刀" /> <br /><br />
</form>';
}

echo'<form method="GET" action="shifumi.php">

<input type="hidden" name="reset" value="Reset" />
<input type="submit" value="Reset" />

</form>';
if (isset($_GET['reset']) && ($_GET['reset'] == "Reset")) {
$_SESSION['Score'] = '0';
$db_value = $_SESSION['Score'];

mysql_query("UPDATE RPS SET score='".$db_value."'");

未设置($_SESSION['Score']);
//session_start();

session_destroy();
if (!headers_sent()){

header('Location:shifumi.php');
}else{
echo '<脚本语言=\"JavaScript\">window.location=\'shifumi.php\';</script>'; }

}

?>

</body>
</html>

于 2013-09-30T17:07:13.887 回答