简体   繁体   中英

How to Insert Multiple Checkboxes and their Textfields Into MySQL Database

I am new in the field of PHP. I am working on a form to get information from a patient regarding a specific disease. In this form i have multiple check-boxes and text fields with each check-box. If one check bos is checked then values of its text fields and checkbox values has to insert in database. Please tell me the code to insert checked values along with textfields into database.

<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>

You will need to establish a connection to the database.

When the form is posted collect this data and insert into the database accordingly using $_POST.

Helpful example can be found here to connect

And to insert data

$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));

first add any method attribute to your form tag like, get or post

<form>

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

and add a submit button too in your form now on submit your form will post your form value and you can catch them by php as to insert in database

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

//your insert query
}
?>

A checkbox will only post when it's checked. A textfield will always get posted even when it's empty.

Use a form:

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

Add a submit button:

        <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";
 }
?>

Demo: here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM