PHP - 将数据$ _POST数组保存到数据库mysql中

时间:2016-01-24 07:59:20

标签: php

我正在尝试从多个单选按钮发送数据 name="answer<?php echo$data[id]?>"。 使用此$_POST[answer[]]如何保存数据?

这是代码。 感谢。

`<form method="post" action="proses.php">`<tbody>
<?php
$no=1;
$getdata = mysql_query("SELECT * FROM pertanyaan where kategori='pekerjaan' order by kategori desc");
        while($data=mysql_fetch_array($getdata)){
        ?>
<tr>
<td><?php echo $no ?></td>
<td><?php echo $data[pertanyaan]?></td>
<td><input type="radio" name="answer<?php echo $data[id] ?>" value="ss"></td>
<td><input type="radio" name="answer<?php echo $data[id] ?>" value="s"></td>
<td><input type="radio" name="answer<?php echo $data[id] ?>" value="b"></td>
<td><input type="radio" name="answer<?php echo $data[id] ?>" value="ts"></td>
<td><input type="radio" name="answer<?php echo $data[id] ?>" value="sts"></td>
</tr>
</form>

2 个答案:

答案 0 :(得分:1)

您需要在收音机按钮的名称中添加square []括号。

<form method="post" action="proses.php">`<tbody>
<?php
$no=1;
$getdata = mysql_query("SELECT * FROM pertanyaan where kategori='pekerjaan' order by kategori desc");
    while($data=mysql_fetch_array($getdata)){
    ?>
<tr>
<td><?php echo $no ?></td>
<td><?php echo $data[pertanyaan]?></td>
<td><input type="radio" name="answer[<?php echo $data[id] ?>]" value="ss"></td>
<td><input type="radio" name="answer[<?php echo $data[id] ?>]" value="s"></td>
<td><input type="radio" name="answer[<?php echo $data[id] ?>]" value="b"></td>
<td><input type="radio" name="answer[<?php echo $data[id] ?>]" value="ts"></td>
<td><input type="radio" name="answer[<?php echo $data[id] ?>]" value="sts"></td>
</tr>
</form>

答案 1 :(得分:1)

    <form method="post" action="proses.php">`<tbody>
    <?php
      $no=1;
      $getdata = mysql_query("SELECT * FROM pertanyaan where kategori='pekerjaan' order by kategori desc");
    while($data=mysql_fetch_array($getdata)){
    ?>
    <tr>
    <td><?php echo $no ?></td>
    <td><?php echo $data[pertanyaan]?></td>
    <td><input type="radio" name="answer[<?php echo $data[id] ?>]" value="ss"></td>
   <td><input type="radio" name="answer[<?php echo $data[id] ?>]" value="s"></td>
    <td><input type="radio" name="answer[<?php echo $data[id] ?>]" value="b"></td>
    <td><input type="radio" name="answer[<?php echo $data[id] ?>]" value="ts"></td>
    <td><input type="radio" name="answer[<?php echo $data[id] ?>]" value="sts"></td>
    </tr>
  </form>
   //process.php

    <?php 
     if(isset($_POST)) {

         $getRadio = $_POST['answer'];
            if(count($getRadio) > 0) {

             // While updating data

              foreach($getRadio as $key => $val) {

               $query = "Update  pertanyaan set kategori = $val  WHERE id = $key";
               mysql_query($query);
        }
       //Insert Data

       foreach($getRadio as $key => $val) {

         //$key Reference Id of Table
         // val get checked radio button value
         $query  = "insert into  TABLENAME values('NULL','$val','$key')"; 
         mysql_query($query);
      }
   }

 }?>