简体   繁体   中英

PHP - Save data $_POST array into database mysql

I am trying to send data from multiple Radio Button with name="answer<?php echo$data[id]?>" . with this $_POST[answer[]] how I can save data ?

here is the code. thanks.

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

You need to put square [] brackets in the name of radio battons.

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

 }?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM