doutuanxiao4619 2013-09-03 16:24
浏览 65
已采纳

如何通过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

  • 写回答

1条回答 默认 最新

  • du512053619 2013-09-03 17:43
    关注

    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");}
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据