多个复选框插入
这是包含多个复选框选项和文本区域输入的简单表单:
<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>
我的问题是如何插入选中的选项(可能多个)和文本区域值(如果输入)并将所有这些输入值插入到数据库中的同一列中,但每个输入“单独的行”。我尝试了这段代码,但不起作用插入任何内容(空单元格):
<?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));
}
?>
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));
}
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
保存到数据库的空单元格是
textarea
中的值。您的代码仅保存
textarea
的原因是因为它是mysql_query
执行的唯一查询。您的foreach
循环将覆盖每个查询,并且$insertSQL
的最后一个值是 textarea 查询。因此,要解决此问题,您必须将mysql_query
移至foreach
循环内,并在循环开始之前连接到数据库。其他答案也同样好,并指出了一些值得考虑的事情。我只是想让你知道为什么你的原始代码是错误的。
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 bymysql_query
. Yourforeach
loop is overwriting each query and the last value of$insertSQL
is the textarea query. So, to fix you have to move themysql_query
inside theforeach
loop and connect to the database before the loop starts.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.
如果我的理解是正确的:
您可以根据自己的喜好编辑查询,这只是将多行插入数据库的快速方法。
编辑:我误读了您的第一个 HTML 表单,您应该将 textarea 的名称编辑为其他内容,然后
InstrCheck[] 并将其改为
comment
,然后您可以使用我上面给出的示例。If I get you right:
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 itcomment
instead, then you can use my example given above.您可以使用此 HTML 表单...
...并使用此 PHP 脚本处理它:
注意:
You could use this HTML form...
...and treat it with this PHP script:
N.B: