0

我是 PHP 新手,我正在开发一个随机数游戏。如果用户猜对了数字,他就会得到一分。但是,我无法向用户说明这一点。这是我的游戏代码:

:<!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=utf-8" />
        <title>result</title>
        <link href="style.css" rel="stylesheet" type="text/css" />
    </head>

    <body>
        <div id="main"></div>

        <div id="enterNumber">

            <?php
            $number_actual = rand(1, 20);
            $number_guess = $_REQUEST["inputnumber"];
            echo "You chose: " . $number_guess . "<br />";
            echo "The random number is: " . $number_actual . "<br />";
            if ($number_guess == $number_actual)
                echo "You guessed <b>Correctly!</b>";


            elseif ($number_guess < $number_actual)
            {
                echo "You are too <b>Low</b>!";
                echo "<br />";
                echo "<br />";
                echo "Try guessing again...";
                echo "<form>
                        <input type='text' name='inputnumber' />
                        <input type='submit' value='Guess' />
                    </form>";
            }
            elseif ($number_guess > $number_actual)
            {
                echo "You are too <b>High</b>!";
                echo "<br />";
                echo "<br />";
                echo "Try guessing again...";
                echo "<form>
                        <input type='text' name='inputnumber' />
                        <input type='submit' value='Guess' />
                    </form>";
            }
            ?>
        </div>

    </body>
</html>

这是给用户一个点的代码,但我在将它们加在一起时遇到了问题!

$query = mysql_query("SELECT * FROM quiz WHERE username='$username'");

$insert = "UPDATE `users` SET `points` = (`points` + 1) WHERE `username` = '".$username."'";

mysql_query($insert); 


header('location: succes.php');
}
}
?>

当我尝试添加它们时,我是这样做的:

<?php

$number_actual = rand(1, 20);
$number_guess = $_REQUEST["inputnumber"];
echo "You chose: " . $number_guess . "<br />";
echo "The random number is: " . $number_actual . "<br />";
if ($number_guess == $number_actual)
    $query = mysql_query("SELECT * FROM quiz WHERE username='$username'");

$insert = "UPDATE `users` SET `points` = (`points` + 1) WHERE `username` = '" . $username . "'";

mysql_query($insert);


header('location: succes.php');
}

echo "You guessed <b>Correctly!</b>";


elseif ($number_guess<$number_actual)
{
echo "You are too <b>Low</b>!";
echo "<br />";
echo "<br />";
echo "Try guessing again...";
echo "<form>
            <input type='text' name='inputnumber' />
            <input type='submit' value='Guess' />
        </form>";
}
elseif ($number_guess>$number_actual)
{
    echo "You are too <b>High</b>!";
    echo "<br />";
    echo "<br />";
    echo "Try guessing again...";
    echo "<form>
            <input type='text' name='inputnumber' />
            <input type='submit' value='Guess' />
        </form>";
}
?>
4

2 回答 2

1

你把echo "You guessed <b>Correctly!</b>";if 和 elseif 放在了中间。应该:

<?php
                $number_actual = rand (1,20);
                $number_guess = $_REQUEST["inputnumber"];
                echo "You chose: " . $number_guess . "<br />";
                        echo "The random number is: " . $number_actual . "<br />";
                                if ($number_guess==$number_actual) { // you forgot this curly brace
                                   $query = mysql_query("SELECT * FROM quiz WHERE username='$username'");

    $insert = "UPDATE `users` SET `points` = (`points` + 1) WHERE `username` = '".$username."'";

     mysql_query($insert); 

    echo "You guessed <b>Correctly!</b>"; // this was placed inbetween the if and the elseif

    header('location: succes.php');
    }
于 2012-12-12T20:09:47.240 回答
0

unbalanced由于} auto-indent` 您的代码存在编译问题`. Always try to indent your code. Depending on the IDE you are using you can(顺便说一句,我使用的是 TextMate):

header('location: succes.php');
}

}不需要,所以很可能你忘记了,所以你的{代码可能是这样的:

if ($number_guess==$number_actual) {
    $query = mysql_query("SELECT * FROM quiz WHERE username='$username'");

    $insert = "UPDATE `users` SET `points` = (`points` + 1) WHERE `username` = '".$username."'";

    mysql_query($insert); 
    header('location: succes.php');
    echo "You guessed <b>Correctly!</b>";
} else {
    echo "Sorry, you wrong!</b>";
}

一旦你尝试缩进你的代码,你自己就会发现很多问题。并且请不要将用户输入的数据直接传递给 MySQL,否则您可能会成为SQL INJECTION. 因此,请像这样重写您的查询:

    $query = mysql_query("SELECT * FROM quiz WHERE username='".mysql_real_escape_string($username)."'");

    $insert = "UPDATE `users` SET `points` = (`points` + 1) WHERE `username` = '".mysql_real_escape_string($username)."'";
于 2012-12-12T20:12:47.540 回答