开发者

multiple checkboxes insertion

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" n开发者_StackOverflow社区ame="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));
}
?>


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.


If I get you right:

IF(isset($_POST['InstrCheck']) && isset($_POST['comment'])) {
    $checked = $_POST['InstrCheck'];
    $comment= $_POST['comment'];
    IF(!empty($checked) && is_array($checked)) {
        foreach($checked as $check) {
            $query = 'INSERT INTO table (checkedID, comment) VALUES (\''.mysql_real_escape_string($check).'\', \''.mysql_real_escape_string($comment).'\'';
            mysql_query($query);
        }
    }
}

You can edit the query to your preferences, this is just a quick way of inserting multiple rows into the DB. edit: I misread your first HTML form, you should edit the name of the textarea to something else then InstrCheck[] and make it comment instead, then you can use my example given above.


You could use this HTML form...

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

...and treat it with this PHP script:

<?php

    $Date = date("d-m-Y");
    $Time = date("H:i:s");
    //    Has the form been sent?
    if (isset($_POST["MM_insert"])) {
        //    We can't test what's in $_POST["MM_insert"] before we even know it exists
        if ($_POST["MM_insert"] == "form1")
        {
            //    ONE connection to the database is enough; it shouldn't be in a for or foreach loop
            mysql_select_db($database_PPS, $PPS);
            //    The pattern's id is stored in the pat_id hidden field, right?
            $pat_id = $_POST["pat_id"];
            //    Never trust user input!
            $pat_id = mysql_real_escape_string($pat_id);

            //    Checkboxes & textarea have name attributes going from 0 to 6 (see HTML code)
            for ($i = 0 ; $i < 7 ; $i++)
            {
                //    Is the number $i checkbox checked?
                if (isset($_POST["InstrCheck$i"]))
                {
                    //    Let's get its value
                    $value = $_POST["InstrCheck$i"];
                    //    Again... never trust user input.
                    $value = mysql_real_escape_string($value);
                    //    Insert data
                    mysql_query("
                        INSERT INTO instruction (instName, instTime, instDate, pat_id)
                        VALUES ('$value', '$Time', '$Date', '$pat_id')
                    ");
                }
            }
        }
    }

?>

N.B:

  • You should NOT use the same id attribute for separate HTML elements.
  • Also, if you don't really need id attributes for HTML elements, don't use them; they can be needed if you want to use JavaScript on them, if you want to apply a specific CSS style on them, or if you want to attach <label> elements to them.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜