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

时间:2013-09-03 16:24:37

标签: php mysql

我正在使用巨大的html表单,通过php将数据存储在数据库中但我的复选框代码无效

////复选框代码////

<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代码,然后在查询中使用///

$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!";
    }

///错误即将来临/// 注意:未定义的索引:mdeceased在第91行的C:\ wamp \ www \ student表格填写\ htmlform.php

只有$ alive正在接受 怎么做

1 个答案:

答案 0 :(得分:1)

我结束了打开的<input>标记,只是进行了空检查。它开始运作了。

问题是你试图保存一个从未设置/初始化的变量。

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