dongye9182 2011-07-12 19:01
浏览 128
已采纳

多个复选框插入

Here is simple form contain multiple checkboxes options and textarea input:

    <form method="post" name="form1" action="<?php echo $editFormAction; ?>">
    <table>
    <tr><th colspan="2">BREATHING CIRCULATION</th></tr>
    <tr><th>#</th><th>Instruction Name</th></tr>
    <tr><td><input name="InstrCheck[]" type="checkbox" id="InstrCheck" value="Respirations normal rate"></td><td>Respirations normal rate</td></tr>
    <tr><td><input name="InstrCheck[]" type="checkbox" id="InstrCheck" value="Respirations effort normal"></td><td>Respirations effort normal</td></tr>
<tr><td><input name="InstrCheck[]" type="checkbox" id="InstrCheck" value="Breath Sounds-normal"></td><td>Breath Sounds-normal</td></tr>
    <tr><td><input name="InstrCheck[]" type="checkbox" id="InstrCheck" value="Skin colour-normal"></td><td>Skin colour-normal</td></tr>
    <tr><td><input name="InstrCheck[]" type="checkbox" id="InstrCheck" value="Heart rhythm & rate normal"></td><td>Heart rhythm & rate normal </td></tr>
    <tr><td><input name="InstrCheck[]" type="checkbox" id="InstrCheck" value="No Oedema"></td><td>No Oedema </td></tr>
    </table>
    <textarea name="InstrCheck[]" id="InstrCheck" placeholder="set your own instruction">                          </textarea>
<input type="hidden" name="MM_insert" value="form1">
<input type="hidden" value="<?php echo $_GET['a'];?>" name="pat_id">
</form>

My problem is How can I insert checked options (could be more than one) AND textarea value (if entered) and insert all these input values into same column in DB but each input in "separate row". I tried this code but not work insert nothing (empty cell):

<?php
$editFormAction = $_SERVER['PHP_SELF'];
    if (isset($_SERVER['QUERY_STRING'])) {
    $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
    }
    $pat_id = $_GET['a'];
    $Date = date("d-m-Y");
    $Time = date("H:i:s");
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
    foreach ($_POST['InstrCheck'] as $value) {
  $insertSQL = "INSERT INTO instruction (instName, instTime, instDate, pat_id) VALUES ('$value', '$Time', '$Date', '$pat_id')";
    }
  mysql_select_db($database_PPS, $PPS);
  $Result1 = mysql_query($insertSQL, $PPS) or die(mysql_error());


  $insertGoTo = "Patint_selection.php?a=$pat_id";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));
}
?>
  • 写回答

3条回答 默认 最新

  • doulu4534 2011-07-12 21:28
    关注

    The empty cell that is being saved to the database is the value from your textarea.

    The reason your code is only saving the textarea is because it is the only query being executed by mysql_query. Your foreach loop is overwriting each query and the last value of $insertSQL is the textarea query. So, to fix you have to move the mysql_query inside the foreach loop and connect to the database before the loop starts.

    /** 
     *  Moving the mysql_query inside the loop requires you connect to the database 
     *  before the loop starts
     */
    mysql_select_db($database_PPS, $PPS);
    
    /** Move your mysql_query inside your foreach loop **/
    foreach ($_POST['InstrCheck'] as $value) {
        $insertSQL = "INSERT INTO instruction (instName, instTime, instDate, pat_id) VALUES ('$value', '$Time', '$Date', '$pat_id')";
    
        // Moved inside the loop...now this will run for each loop
        $Result1 = mysql_query($insertSQL, $PPS) or die(mysql_error());
    
    }
    

    The other answers are just as good and pointed out some things worth considering. I just want you to learn why your original code was wrong.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 qgcomp混合物线性模型分析的代码出现错误:Model aliasing occurred
  • ¥100 已有python代码,要求做成可执行程序,程序设计内容不多
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥15 小红薯封设备能解决的来
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助
  • ¥15 STM32控制MAX7219问题求解答