2016-01-24 235 views
0

我試圖從多個單選按鈕發送數據與 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> 
+0

能否請你告訴'的var_dump輸出($數據)'放在循環? –

+0

你如何提交價值..我沒有看到代碼,,, – scaisEdge

回答

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

}?>