私は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>";
}
?>