php - 如何使用动态单选按钮将数据从表单插入数据库..?

标签 php mysql forms

我正在尝试从带有测验页面动态生成的单选按钮的表单中插入数据。我无法访问名称为数组的表单发布的数据。请帮助我做到这一点。如果您发现任何错误,那一定是很大的帮助.谢谢

        <?php
        $rs=mysql_query("select * from question where testid=$tid order by quesid ",$cn) or die(mysql_error()); 
        $n=0;
        while($row= mysql_fetch_row($rs)){?>
        <form name=myfm method=post action=Quiz.php>
        <table width=100%> <tr> <td width=30><td></td></td></tr> <table border=0>
        <?php $n=$n+1; ?>
        <tr><td>Question  <?php echo $n." "; echo $row[2]; ?></td></tr>
        <tr><td class=style8><input type="radio" name="ques['<?php echo $n; ?>'][]" value=1><?php echo $row[3]; ?></td></tr>
        <tr><td class=style8> <input type="radio" name="ques['<?php echo $n; ?>'][]" value=2><?php echo $row[4];?></td></tr>
        <tr><td class=style8><input type="radio" name="ques['<?php echo $n; ?>'][]"  value=3><?php echo $row[5];?></td></tr>
        <tr><td class=style8><input type="radio" name="ques['<?php echo $n; ?>'][]"  value=4><?php echo $row[6];?></td></tr>

    <?php 
        }
        echo "<tr><td><input type=submit name=submit id='result' value='Get Result'></form>";
        ?>
        </table></table>
        </form>

数据插入页面

        <?php 
        $rs=mysql_query("select * from question where testid=$tid",$cn) or die(mysql_error());
        if($submit=='Get Result')
        { $n=0;
        while($row= mysql_fetch_row($rs)){
        for($i=0;$i<count($_POST['ques']);$i++)
        {$n=$n+1;
        $ans=$_POST['ques'][$n][$i];
        mysql_query("insert into useranswer(sessid, testid, ques, ans1,ans2,ans3,ans4,correctans,yourans) values ('".session_id()."',       $tid,'$row[2]','$row[3]','$row[4]','$row[5]', '$row[6]','$row[7]','$ans')") or die(mysql_error());
        }
        ?>

最佳答案

你有一个变量$tid。那么应该是这样的。

$rs=mysql_query("select * from question where testid=".$tid,$cn)

不要忘记添加 mysql_real_escape_string在您的查询中。

mysql_real_escape_string — Escapes special characters in a string for use in an SQL statement.But this extension was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. Instead, the MySQLi or PDO_MySQL extension should be used.

更新:::

mysql_real_escape_string

 // We have not connected to MySQL

 $lastname  = "O'Reilly";
 $_lastname = mysql_real_escape_string($lastname);

 $query = "SELECT * FROM actors WHERE last_name = '$_lastname'";

更新 2::

$tid = mysql_real_escape_string($tid);
$ans = mysql_real_escape_string($ans);

mysql_query("insert into useranswer(sessid, testid, ques, ans1,ans2,ans3,ans4,correctans,yourans) values ('".session_id()."',       '". $tid ."','$row[2]','$row[3]','$row[4]','$row[5]', '$row[6]','$row[7]','". $ans .'")") or die(mysql_error());

关于php - 如何使用动态单选按钮将数据从表单插入数据库..?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35307786/

相关文章:

javascript - 如何将文本附加到同一 html 类中的多个用户输入?

php - 如何在 laravel 中获取列值的平均值

php - 另一个日期时间到 GMT 转换问题

php - 显示包含多个结果的数据

mysql - 如何存储不可变、唯一、有序的列表?

c# - 使用表单将新列表项添加到 MVC、Razor、ASP.NET 中的 List<T>

ruby-on-rails - 语法错误,意外的 keyword_ensure,期待 $end,在 HAML 中使用 form_tag

php - 数据库问题的 HTML 输出

php - 为每个用户生成随机唯一代码

mysql - 检查MYSQL中的记录是否存在