从HTML表单中选择动态值并存储在PHP变量中 [英] Picking Dynamic values from HTML form and store in PHP varibles

查看:132
本文介绍了从HTML表单中选择动态值并存储在PHP变量中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做在线态度测试,它会从数据库中选择2个随机问题,并将其显示在网页上以供回答。

I am doing online aptitude test, which will pick up 2 random questions from database and display them on webpage for answering.

问题是,在数据库中(当将问题和答案存储到数据库中时,它会变得混乱,有些甚至不存储)。任何人都可以帮我解决这个问题,

Problem is that it's not properly storing values in the database (While Storing Questions and answers into the database it's getting jumbled up and some are not even storing). Can anyone please help me with this issue,

下面的代码正在从候选人得到答案(简单的演示只拾取2个随机问题)。

The code below is getting answers from candidate ( Simple demo picks up only 2 random questions).

<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>

下一部分是在用户点击SUBMIT按钮之后将它们存储到数据库中。

Next part is storing them into database, after user clicks on SUBMIT button.

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());

?>


推荐答案

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

I think you just need to post the question ID like this :

<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']; 
}

完成代码


Complete code

// 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());
    }
}
?>

这篇关于从HTML表单中选择动态值并存储在PHP变量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆