如何通过php在数据库中存储复选框项

如何通过php在数据库中存储复选框项

问题描述:

i am coading huge html form which store data in database through php but my checkbox code is not working

////check box code////

<li>Mother </li><table><tr><td><input type="checkbox" name="mdeceased"               value="deceased">deceased</td>
<td><input type="checkbox" name="malive" value="alive">alive</td></tr>
<tr><td><input type="checkbox" name="mretired" value="retired">retired</td>
<td><input type="checkbox" name="minservice" value="inservice">in service</td>
<td><input type="checkbox" name="mbusiness" value="business">business</td></tr>

///php code for checkbox for making variable and then using in the query ///

$mysql = new mysqli('localhost','root','ramsha','scholarships_system') or die ('you\'re     dead');
if(!empty($_POST['submit'])) {
$mdeceased =$_POST['mdeceased'];
$malive = $_POST['malive'];
$mretired = $_POST['mretired'];
$minservice = $_POST['minservice'];
$mbusiness = $_POST['mbusiness'];


$query = "INSERT INTO student _details      VALUES('$mdeceased','$malive','$mretired','$minservice','$mbusiness')";

if($updateDb = $mysql->query($query) or die($mysql->error)) {
        echo "Congrats!";
    }

///error is coming /// Notice: Undefined index: mdeceased in C:\wamp\www\student form filling\htmlform.php on line 91

only $alive is accepting what to do

I ended the open <input> tags and just did a null check. It started working.

Problem was you were trying to save a variable that was never set/initialized.

<?php
$mysql = new mysqli('localhost','root','Subha#143','test') or die ('you\'re     dead');
if(isset($_POST['submit'])) {
$mdeceased =isset($_POST['mdeceased'])?$_POST['mdeceased']:null;
$malive = isset($_POST['malive'])?$_POST['malive']:null;
$mretired = isset($_POST['mretired'])?$_POST['mretired']:null;
$minservice = isset($_POST['minservice'])?$_POST['minservice']:null;
$mbusiness = isset($_POST['mbusiness'])?$_POST['mbusiness']:null;

$query = "INSERT INTO student_details VALUES('$mdeceased','$malive','$mretired','$minservice','$mbusiness')";

if($updateDb = $mysql->query($query) or die($mysql->error)) {
        echo "Congrats!";
    }
}else{echo("post is not set");}
?>