Php 多个复选框插入
以下是包含多个复选框选项和文本区域输入的简单表单:Php 多个复选框插入,php,mysql,forms,Php,Mysql,Forms,以下是包含多个复选框选项和文本区域输入的简单表单: <form method="post" name="form1" action="<?php echo $editFormAction; ?>"> <table> <tr><th colspan="2">BREATHING CIRCULATION</th></tr> <tr><th>#</th>
<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>
如果我没弄错的话:
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);
}
}
}
您可以根据自己的首选项编辑查询,这只是将多行插入数据库的一种快速方法。
编辑:我误读了您的第一个HTML表单,您应该将文本区域的名称编辑为其他内容,然后选择
InstrCheck[]
,并将其改为注释
,然后您可以使用上面给出的示例。您可以使用此HTML表单
<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>
保存到数据库中的空单元格是文本区域中的值
代码只保存textarea
的原因是因为它是由mysql\u query
执行的唯一查询。您的foreach
循环正在覆盖每个查询,$insertSQL
的最后一个值是textarea查询。因此,要修复此问题,必须在foreach
循环中移动mysql\u查询
,并在循环开始之前连接到数据库
/**
* 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());
}
其他答案同样好,并指出了一些值得考虑的问题。我只想让您了解原始代码出错的原因。您是否能够向数据库中写入至少一个选中值?否,检查数据库上的表时,没有错误消息出现。我发现空单元格,但插入了另一个值。我完全按照您在这里所做的操作,在Dreamweaver或db中都没有收到任何错误消息,但根本没有插入任何内容,我想知道输入丢失在哪里???尝试添加echo mysql_error();在mysql_查询语句之后,看看SQL是否有问题。我需要将它们全部插入同一列(选中选项和文本输入)
<?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')
");
}
}
}
}
?>
/**
* 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());
}