JACKPOT页面:
Username: <input type="text" name="user_name">
<select name="number1">
<option value="0">0</option>
<option value="100">1</option>
<option value="200">2</option>
<option value="300">3</option>
<option value="400">4</option>
<option value="500">5</option>
<option value="600">6</option>
<option value="700">7</option>
<option value="800">8</option>
<option value="900">9</option>
</select>
<select name="number2">
<option value="00">0</option>
<option value="10">1</option>
<option value="20">2</option>
<option value="30">3</option>
<option value="40">4</option>
<option value="50">5</option>
<option value="60">6</option>
<option value="70">7</option>
<option value="80">8</option>
<option value="90">9</option>
</select>
<select name="number3">
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
</select>
<input type="submit" value="Submit">
</form>
结果页面:
<?php
$date = new DateTime();
define('DB_NAME', 'test');
define('DB_USER', 'root');
define('DB_PASSWORD', '');
define('DB_HOST', 'localhost');
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if (!$link)
{
die('Could not connect: '. mysql_error());
}
$db_selected = mysql_select_db(DB_NAME, $link);
if (!$db_selected)
{
die('Can\'t use '.DB_NAME.': '.mysql_error());
}
$data = mysql_query("SELECT * FROM config") or die(mysql_error());
$number1 = mysql_real_escape_string($_POST['number1']);
$number2 = mysql_real_escape_string($_POST['number2']);
$number3 = mysql_real_escape_string($_POST['number3']);
$username = mysql_real_escape_string($_POST['user_name']);
while($info = mysql_fetch_array( $data ))
{
if($info['jackpot_code'] == ($_POST['number1'] + $_POST['number2'] + $_POST['number3']))
{
echo 'CORRECT!';
}
else if(($info['jackpot_code'] - ($_POST['number1'] + $_POST['number2'] + $_POST['number3'])) <= 30 && ($info['jackpot_code'] - ($_POST['number1'] + $_POST['number2'] + $_POST['number3'])) >= 0 or ($info['jackpot_code'] - ($_POST['number1'] + $_POST['number2'] + $_POST['number3'])) >= -30 && ($info['jackpot_code'] - ($_POST['number1'] + $_POST['number2'] + $_POST['number3'])) <= 0){
$code = $_POST['number1'] + $_POST['number2'] + $_POST['number3'];
echo "Guessed code: $code<br>";
die('The code u guessed is at the range of +/- 30 to jackpot.');
} else
{
die('CODE FALSE!');
}
}
$number = $_POST['number1'] + $_POST['number2'] + $_POST['number3'];
echo "<br>Guessed code: $number";
$sql = "INSERT INTO jackpot_log (username, code, time) VALUES ('".$username."','".$number."','".$date->getTimestamp()."')";
if (!mysql_query($sql))
{
die('Error: '. mysqli_error($con));
}
?>
这是我的代码,它完全有效。问题是:我想从我的数据库的“配置”表中更改我的“jackpot_code”。有没有办法随机化这个表,并在每次成功尝试后生成一个新的3位数代码。
这是我的数据库: 我需要做的就是在每次正确尝试后随机化“jackpot_code”。 CLICK TO OPEN THE PHOTO OF DATABASE!
答案 0 :(得分:1)
试试这个......
$random =rand(100,999);
答案 1 :(得分:0)
如果您的数据库已满,并且您希望获得随机数据,请使用
SELECT * FROM config ORDER BY rand() LIMIT 1
或者如果您想要生成新的,只需在回显“正确”之前添加它
INSERT INTO config (jackpot_code) VALUES (rand())
并更改选择
SELECT * FROM config ORDER BY id ASC LIMIT 1
答案 2 :(得分:0)
成功插入命令后使用以下代码
如果jackpot_code为int数据类型
<?php
mysql_query("UPDATE `config` SET `jackpot_code`= ".rand(100,990)." WHERE `id`=1");
?>
如果jackpot_code为varchar数据类型
<?php
mysql_query("UPDATE `config` SET `jackpot_code`= '".rand(100,990)."' WHERE `id`=1");
?>