一尘不染

从HTML表单中选择动态值并将其存储在PHP变量中

sql

我正在进行在线能力倾向测试,它将从数据库中随机抽取2个问题并将其显示在网页上以作答。

问题是它没有在数据库中正确存储值(将问题和答案存储到数据库中时,它变得混乱不堪,有些甚至没有存储)。谁能帮我解决这个问题,

下面的代码从应聘者那里获得答案(简单演示仅抽取2个随机问题)。

<form id="form1" name="quest" method="POST" action="" >


<?php


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

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

    rows1 = mysql_fetch_array($query);
    $q1 = $rows1['QNo'];
    $qus1 = $rows1['Question'];
    $a1 = $rows1['Opt1'];
    $b1 = $rows1['Opt2'];
    $c1 = $rows1['Opt3'];
    $d1 = $rows1['Opt4'];
    $ans1 = $rows1['Ans'];
    echo " <b>Question:-<br></b>$qus1 <br><br>";
    echo " <input type=radio name = 'answer$q1' value = '$a1'></input>$a1 &nbsp &nbsp<br>"; 
    echo " <input type=radio name = 'answer$q1' value = '$b1'></input>$b1 &nbsp &nbsp<br>"; 
    echo " <input type=radio name = 'answer$q1' value = '$c1'></input>$c1 &nbsp &nbsp <br>"; 
    echo " <input type=radio name = 'answer$q1' value = '$d1'></input>$d1 <br><br> ";

    $rows2 = mysql_fetch_array($query);
    $q2 = $rows2['QNo'];
    $qus2 = $rows2['Question'];
    $a2 = $rows2['Opt1'];
    $b2 = $rows2['Opt2'];
    $c2 = $rows2['Opt3'];
    $d2 = $rows2['Opt4'];
    $ans2 = $rows2['Ans'];
    echo " <b>Question:-<br></b>$qus2<br> <br>";
    echo " <input type=radio name = 'answer$q2' value = '$a2'></input>$a2 &nbsp &nbsp<br>"; 
    echo " <input type=radio name = 'answer$q2' value = '$b2'></input>$b2 &nbsp &nbsp<br>"; 
    echo " <input type=radio name = 'answer$q2' value = '$c2'></input>$c2 &nbsp &nbsp<br> "; 
    echo " <input type=radio name = 'answer$q2' value = '$d2'></input>$d2 <br><br> ";

}

?>

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

用户单击“提交”按钮后,下一部分将它们存储到数据库中。

if (isset($_POST['SUBMIT'])) 
{
  $connect = mysql_connect("localhost","root","")
  or die(mysql_error());
  $sel=mysql_select_db("demo");

    $opt1=$_POST["answer$q1"];    // Problem is here

    $id1=array("$q1","$opt1");
    if($ans1==$opt1)             // Out Correcting answer part
    {
    $val1="ct";
    }
    else
    {
    $val1="wg";
    }

    $opt2=$_POST["answer$q2"];    // Problem is here

    $id2=array("$q2","$opt2"); 
    if($ans2==$opt2)              // Out Correcting answer part
    {
    $val2="ct";
    }
    else
    {
    $val2="wg";
    }
mysql_query("insert into $username values('$id1[0]','$id1[1]','$val1')")
or die(mysql_error());
mysql_query("insert into $username values('$id2[0]','$id2[1]','$val2')")
or die(mysql_error());

?>

阅读 150

收藏
2021-05-16

共1个答案

一尘不染

我认为您只需要这样发布问题ID:

<input type="hidden"  name="q1" value="'$q1'">

$opt1=$_POST["answer$q1"];

您必须添加:

$q1=$_POST["q1"];
if (is_numeric($q1)) {
    $query = mysql_query("SELECT * FROM `microsoftq` WHERE QNo=".$q1);
    $rows1 = mysql_fetch_array($query);
    $ans1 = $rows1['Ans']; 
}

完整的代码

// Build Form   
$nbQuestion = 2;
$form = '<form id="form1" name="quest" method="POST" action="" >';
$form .= getQuestion("SELECT * FROM `microsoftq`  ORDER BY RAND() LIMIT ".$nbQuestion);
$form .= '<input type="submit" id="submit_id" name="SUBMIT" value="SUBMIT"></form>';

// Save answer
if (isset($_POST['SUBMIT'])) 
{
    for($i=1;i<=$nbQuestion;$i++){
        saveAnswer($i);
    }
}
function getQuestion($query){
    $question = "";
    $i = 1;
    $result = mysql_query($query);
    while ($row = mysql_fetch_object($result)) {
        $question .= "<b>Question:-<br></b>".$row->Question." <br><br>";
        $question .= "<input type='hidden' name='q".$i."' value='".$row->QNo."'>";
        $question .= "<input type=radio name = 'answer".$row->QNo."' value = '".$row->Opt1."'></input>$a1 &nbsp &nbsp<br>"; 
        $question .= " <input type=radio name = 'answer".$row->QNo."' value = '".$row->Opt2."'></input>$b1 &nbsp &nbsp<br>"; 
        $question .= " <input type=radio name = 'answer".$row->QNo."' value = '".$row->Opt3."'></input>$c1 &nbsp &nbsp <br>"; 
        $question .= " <input type=radio name = 'answer".$row->QNo."' value = '".$row->Opt4."'></input>$d1 <br><br> ";
        $i++;
    }
    mysql_free_result($result);
}
function saveAnswer($nb){
    $qId=$_POST["q".$nb];
    if (is_numeric($qId)) {
        $query = mysql_query("SELECT * FROM `microsoftq` WHERE QNo=".$qId);
        $rows1 = mysql_fetch_array($query);
        $ans = $rows1['Ans'];
        $opt = $_POST["answer".$qId];
        if($ans==$opt)
        {
            $val="ct";
        }
        else
        {
            $val="wg";
        }
        mysql_query("insert into $username values('$qId','$opt','$val')")
        or die(mysql_error());
    }
}
?>
2021-05-16