在If语句后添加php代码

时间:2012-12-12 20:05:05

标签: php mysql database

我是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>";
}
?>

2 个答案:

答案 0 :(得分: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');
    }

答案 1 :(得分:0)

由于unbalanced} `. 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)."'";