如何将多个复选框及其文本字段插入MySQL数据库

时间:2014-04-07 08:03:46

标签: php mysql

我是PHP领域的新手。  我正在制作一份表格,以便从患者那里获得有关特定疾病的信息。 在这种形式我有每个复选框的多个复选框和文本字段。如果选中了一个检查bos,则必须在数据库中插入其文本字段和复选框值的值。 请告诉我将检查值和文本字段插入数据库的代码。

<form>
<table>
<tr>
<td colspan="4">Past Medical History:</td>
</tr>
<tr valign="top">
<td colspan="4" height="290"><table border="0" width="100%">
<tbody>
<tr>
<td width="26%"><div align="center">Problem</div></td>
<td width="18%"><div align="center">From (Year)</div></td>
<td width="56%"><div align="center">Details</div></td>
</tr>
<tr>
<td><input name="chkBP" id="chkBP" value="BP" type="checkbox" />
 Blood Pressure</td>
 <td><div align="center">
 <input name="txtBPfrom" id="txtBPfrom" size="15" value="" type="text" />
 </div></td>
 <td><input name="txtBPDetail" id="txtBPDetail" size="40" value="" type="text" /></td>
 </tr>
 <tr>
 <td><input name="chkDiabetes" id="chkDiabetes" value="Diabetes" type="checkbox" />
 Diabetes</td>
 <td><div align="center">
 <input name="txtDiabetesfrom" id="txtDiabetesfrom" size="15" value="" type="text" />
 </div></td>
 <td><input name="txtDiabetesDetail" id="txtDiabetesDetail" size="40" value=""   type="text" /></td>
 </tr>
 <tr>
 <td><input name="chkHighCholes" id="chkHighCholes" value="HighCholesterol" type="checkbox" />
 High Cholesterol</td>
 <td><div align="center">
 <input name="txtHighCholesfrom" id="txtHighCholesfrom" size="15" value="" type="text"/>
 </div></td>
 <td><input name="txtHighCholesDetail" id="txtHighCholesDetail" size="40" value="" type="text" /></td>
 </tr>
 <tr>
 <td><input name="chkArthritis" id="chkArthritis" value="Arthritis" type="checkbox" />
 Arthritis</td>
 <td><div align="center">
 <input name="txtArthritisfrom" id="txtArthritisfrom" size="15" value="" type="text" />
 </div></td>
 <td><input name="txtArthritisDetail" id="txtArthritisDetail" size="40" value="" type="text" /></td>
 </tr>
 <tr>
 <td><input name="chkAsthma" id="chkAsthma" value="Asthma" type="checkbox" />
 Asthma</td>
 <td><div align="center">
 <input name="txtAsthmafrom" id="txtAsthmafrom" size="15" value="" type="text" />
 </div></td>
 <td><input name="txtAsthmaDetail" id="txtAsthmaDetail" size="40" value="" type="text" /></td>
 </tr>
 <tr>
 <td><input name="chkCirculation" id="chkCirculation" value="Circulation" type="checkbox" />
Circulation</td>
<td><div align="center">
<input name="txtCirculationfrom" id="txtCirculationfrom" size="15" value="" type="text" />
</div></td>
<td><input name="txtCirculationDetail" id="txtCirculationDetail" size="40" value="" type="text" /></td>
</tr>   
</table></td>
</tr>
</form>

3 个答案:

答案 0 :(得分:0)

您需要建立与数据库的连接。

发布表单时,收集此数据并使用$ _POST相应地插入数据库。

有用的示例可以在connect

找到

并插入数据

$link = mysqli_connect("localhost","root","","web_table");

mysqli_query($link,"INSERT INTO web_formitem (`ID`, `formID`, `caption`, `key`,    `sortorder`, `type`, `enabled`, `mandatory`, `data`)
VALUES (105, 7, 'Tip izdelka (6)', 'producttype_6', 42, 5, 1, 0, 0)") 
or die(mysqli_error($link));

答案 1 :(得分:0)

首先将任何方法属性添加到表单标记,例如getpost

<form>

<form action= "" method="post">

并在表单中添加提交按钮 现在提交您的表格将发布您的表格价值 并且您可以通过php捕获它们以插入数据库

<?php
if(isset($_POST['submit_btn_name']))
{
//your database connect
//catch all value, for example
$val=$_POST['check_value'];

//your insert query
}
?>

答案 2 :(得分:0)

复选框仅在选中后发布。 即使文本字段为空,也会始终发布文本字段。

使用表格:

<form name="contactform" method="post" action="<?php echo $_SERVER["PHP_SELF"];?>">

添加提交按钮:

        <tr>
            <td><input type="submit" value="Submit"></td>
        </tr>

PHP:

    <?php
 //var_dump($_POST);
 $chkBP = $_POST['chkBP'];
 $txtBPfrom = $_POST['txtBPfrom'];
 $txtBPDetail = $_POST['txtBPDetail'];
 //echo "-- $txtBPfrom $txtBPDetail --";
 if ($chkBP == "BP"){
     //echo"Bloodpressure = checked";
     $sql="INSERT INTO patient_details (from, detail)
    VALUES ('$txtBPfrom', '$txtBPDetail')";
    mysql_query($sql);
 }else{
     echo"Bloodpressure = not checked";
 }
?>

演示:here