具有相同名称和不同名称的多个输入

时间:2014-08-06 12:12:53

标签: php html forms foreach

如何在php文件中接收2个多输入表格

这是php和html的代码

<?php 
    $select_being = @mysql_query('SELECT * FROM `being_asked`');
    $show_qu = '';
    while($select_being_result = @mysql_fetch_array($select_being))
    {
        $ask_id = $select_being_result['id'];
        $ask_que = $select_being_result['question'];
        $show_qu .= ' <tr>
        <td>'.$ask_que.'</td>
        <td><input type="radio" name="home['.$ask_id.']" value="1" />Yes</td>
        <td><input type="radio" name="home['.$ask_id.']" value="0" />No
        <input type="hidden" name="hid['.$ask_id.']" value="'.$ask_id.'" />

        </td>
        </tr>'; 

        }
?>
        <form id="Decision" action="sub_being.php?id=<?php echo $manuscript_id ?>" method="post">
        <table class="table table-bordered">
        <tr class="trd">
        <td>Question</td>
        <td colspan="2">Answer</td>
         </tr>
        <?php echo $show_qu  ?>
        <tr>
       <td colspan="5"><input type="submit" name="submit" value="Submit" /></td>
       </tr>
        </table>
        </form>

我试过这个代码ro但是它给了我错误

$answer = $_POST['home'];
$id = $_POST['hid'];
foreach($answer as $value) {
$insert = @mysql_query('INSERT INTO `being_asked_report`(`re_id`,`manu_id`,`answer`,`que_id`)VALUE('.$reviewer_id.','.$manuscript_id.','.$value.')');
if($insert)
{
    echo 'done <br />';
    foreach($id as $que_id)
    {

    }
}

}  

我知道这个问题会重复,但我无法理解我的案例中的任何决心

2 个答案:

答案 0 :(得分:0)

首先:请避免将@ -signs与mysql_ *函数一起使用。当某些东西在将来不起作用时,你会隐藏错误。应该让你免于沮丧。

其次,你走在正确的轨道上。我想改变

foreach($answer as $value) {

foreach($answer as $key => $value) {

将帮助您获得正确的价值。 您的问题的ID将是$ key。如果这不起作用,请在$ _POST(带标签)上尝试print_r,以使自己理解。

答案 1 :(得分:0)

你可以试试这个。如果这对您有用,请告诉我。

<?php 
$select_being = mysql_query('SELECT * FROM `being_asked`');
?>

<form id="Decision" action="sub_being.php?id=<?php echo $manuscript_id; ?>" method="post">
    <table class="table table-bordered">
        <tr class="trd">
            <td>Question</td>
            <td colspan="2">Answer</td>
        </tr>
        <?php
        while($select_being_result = mysql_fetch_assoc($select_being))
        {
            $ask_id = $select_being_result['id'];
            $ask_que = $select_being_result['question'];
            echo '<tr>';
                echo '<td>'.$ask_que.'</td>';
                echo '<td><input type="radio" name="home['.$ask_id.']" value="1" />Yes</td>';
                echo '<td><input type="radio" name="home['.$ask_id.']" value="0" />No';
                echo '<input type="hidden" name="hid['.$ask_id.']" value="'.$ask_id.'" />';
                echo '</td>';
            echo '</tr>'; 
        }
        ?>
        <tr>
            <td colspan="5"><input type="submit" name="submit" value="Submit" /></td>
        </tr>
    </table>
</form>

我假设的第二个块是sub_being.php

<?php
$manu_id = $_GET['id'];
$answers = $_POST['home'];
$id = $_POST['hid'];
foreach($answers as $key => $value) 
{
    $query = 'INSERT INTO `being_asked_report` (`re_id`,`manu_id`,`answer`,`que_id`) VALUES ('.$reviewer_id.','. $manu_id .','.$value.','.$key.')';
    $insert = mysql_query($query);
    if($insert)
    {
        echo $query . ' succesful.</br>'
    }else
    {
        echo mysql_errno() . '</br>';
    }
} 
?>