gpt4 book ai didi

php - 随机选取变量并将其存储在 SQL 数据库中

转载 作者:行者123 更新时间:2023-11-29 23:43:38 25 4
gpt4 key购买 nike

我正在为我的公司进行在线能力倾向测试,该测试将从数据库中随机抽取 20 个问题并将其显示在网页上以供回答。

问题是,它没有正确地将值存储在数据库中(将问题和答案存储到数据库中时会变得困惑),请任何人都可以帮助我解决这个问题,

下面的代码是从候选人那里获取答案(简单的演示,例如只选择 3 个随机问题),..

<form id="form1" name="quest" method="POST" action="" style="margin-left:60px;">


<?php

$connect = mysql_connect("localhost","root","")
or die(mysql_error());
$sel=mysql_select_db("demo");

$query = mysql_query("SELECT * FROM `questions` ORDER BY RAND() LIMIT 3 ");


$rows = mysql_fetch_array($query);
$q1 = $rows['QNo'];
$qus1 = $rows['Question'];
$a = $rows['Opt1'];
$b = $rows['Opt2'];
$c = $rows['Opt3'];
$d = $rows['Opt4'];
$ans = $rows['Ans'];


echo " <b>Question:-<br></b>$qus1 <br>";
echo " <input type=radio name = 'answer$q1' value = '$a'></input>$a &nbsp &nbsp";
echo " <input type=radio name = 'answer$q1' value = '$b'></input>$b &nbsp &nbsp";
echo " <input type=radio name = 'answer$q1' value = '$c'></input>$c &nbsp &nbsp ";
echo " <input type=radio name = 'answer$q1' value = '$d'></input>$d <br><br> ";



$rows = mysql_fetch_array($query);
$q2 = $rows['QNo'];
$qus2 = $rows['Question'];
$a = $rows['Opt1'];
$b = $rows['Opt2'];
$c = $rows['Opt3'];
$d = $rows['Opt4'];
$ans = $rows['Ans'];


echo " <b>Question:-<br></b>$qus2 <br>";
echo " <input type=radio name = 'answer$q2' value = '$a'></input>$a &nbsp &nbsp";
echo " <input type=radio name = 'answer$q2' value = '$b'></input>$b &nbsp &nbsp";
echo " <input type=radio name = 'answer$q2' value = '$c'></input>$c &nbsp &nbsp ";
echo " <input type=radio name = 'answer$q2' value = '$d'></input>$d <br><br> ";




$rows = mysql_fetch_array($query);
$q3 = $rows['QNo'];
$qus3 = $rows['Question'];
$a = $rows['Opt1'];
$b = $rows['Opt2'];
$c = $rows['Opt3'];
$d = $rows['Opt4'];
$ans = $rows['Ans'];


echo " <b>Question:-<br></b>$qus3 <br>";
echo " <input type=radio name = 'answer$q3' value = '$a'></input>$a &nbsp &nbsp";
echo " <input type=radio name = 'answer$q3' value = '$b'></input>$b &nbsp &nbsp";
echo " <input type=radio name = 'answer$q3' value = '$c'></input>$c &nbsp &nbsp ";
echo " <input type=radio name = 'answer$q3' value = '$d'></input>$d <br><br> ";

?>


<input type="submit" id="submit_id" name="SUBMIT" value="SUBMIT">
</form>

下一步是存储到数据库中,..

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

if (isset($_POST['SUBMIT']))
{


$opt1=$_POST["answer1"];
$opt2=$_POST["answer2"];
$opt3=$_POST["answer3"];

$username=$_GET['username']; // getting this value from last webpage pls dont worry about this


$connect = mysql_connect("localhost","root","")
or die(mysql_error());
$sel=mysql_select_db("demo");


mysql_query("insert into $username values('$qus1','$opt1')")
or die(mysql_error());
mysql_query("insert into $username values('$qus2','$opt2')")
or die(mysql_error());
mysql_query("insert into $username values('$qus3','$opt3')")
or die(mysql_error());


print "<script>window.close('techtest.php'); window.location = \"final.html\";</script>";

}

?>

最佳答案

问题编号是多少?这就是问题

请尽量不要重复代码片段。因此将其重写为如下所示:

<form id="form1" name="quest" method="POST" action="" style="margin-left:60px;">
<?php

function input_option($QNo,$Opt)
{
echo "<input type=radio name='answer$QNo' value='$Opt'>$Opt</input>&nbsp&nbsp";
}

$count = 3;
$connect = mysql_connect("localhost","root","") or die(mysql_error());
$sel = mysql_select_db("demo");
$query = mysql_query("SELECT * FROM `questions` ORDER BY RAND() LIMIT $count");

while ($row = mysql_fetch_assoc($query))
{
extract($row);
echo "<b>Question:-<br></b>$Question <br>".
input_option($QNo,$Opt1).
input_option($QNo,$Opt2).
input_option($QNo,$Opt3).
input_option($QNo,$Opt4);
}

?>
<input type="submit" id="submit_id" name="SUBMIT" value="SUBMIT">
</form>

现在我没有发现该页面有任何其他问题,所以让我们看看另一段代码。我认为我们不知道问题的数量,请记住数据库中有多个问题,并且您随机选择三个。他们可以有任何数字。所以下面的代码必须处理这个问题:

<?php
error_reporting(E_ALL);
ini_set('display_errors',1);

if (isset($_POST['SUBMIT']))
{
$connect = mysql_connect("localhost","root","") or die(mysql_error());
$sel = mysql_select_db("demo");
$username = $_GET['username'];
foreach ($_POST as $post)
if (substr($post,0,6) == 'answer')
{
$question = substr($post,6);
$option = ${$post};
$sql = "insert into $username values('$question','$option')";
mysql_query($sql) or die(mysql_error());
}
}

?>
<script>
window.close('techtest.php');
window.location = \"final.html\";
</script>

正如你所看到的,我确实使用了问题编号。请注意,编写此示例代码时并未考虑安全性。有足够的空间来破解这个。

关于php - 随机选取变量并将其存储在 SQL 数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26053615/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com