使用 php pdo 在 sqlite 数据库中插入多个复选框的值

Insert value of multiple checkbox in sqlite database using php pdo

你好,我有多个复选框名字 make 和 female 我想要如果一个人 select make 和不 select female 如果我不输入它现在在数据库中不输入任何内容或 false select 示例:female it return female 行中没有任何内容,但它也会出错

Undefined index: Female in /storage/sdcard1/www/3/signup/index.php on line 11

但我不想要这个错误,我想要如果女性不是 select 它插入 false 并且不显示错误我不想要 selected 复选框值是真的,因为它在数据库中写为 on

代码index.php

<?php 
if(isset($_POST['submit'])){
    
$conn = new PDO("sqlite: sign.db");
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $Email = $_POST['Email'];
    $First = $_POST['First'];
    $Last = $_POST['Last'];
    $Password = $_POST['Password'];
    $Male = $_POST['Male'];
    $Female =$_POST['Female'];
     $Dateofb = $_POST['Dateofb'];
  require_once 'imp.php';
    
}
?>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="index.css"  >
<script src="index.js" ></script>
<title>Survey</title>
</head>
<body>
<form action="" method="post"  >
<div class="container" >
<div class="form" >

<input type="email" class="first"  id="Email" name="Email" placeholder="Email" required="required">
<input type="text" class="second"  id="First" name="First" placeholder="First name" required="required">
<input type="text" class="last"  id="Last" name="Last" placeholder="Last name" required="required">
<input type="password" class="pass" name="Password" id="Password" placeholder="Password" required="required">

<div class="day" >

<p class="bd" >Birthday Date:</p>

<input type="date" class="date" id="Dateofb" name="Dateofb" >

</div>
<div>
<div class="malee" >
<input type="checkbox" class="male" id="Male" name="Male">
<p class="mal" >Male</p>
</div>
<div class="femalee" >
<input type="checkbox" class="female" id="Female" name="Female" >
<p class="fem" >Female</p>
</div>
</div>
<div >
<input class="submit" id="submit" type="submit" name="submit"  >
</div>
<div class="acc" >
already have account <a href="#" >Login</a>
</div></div>
</div>
</form>
</body>
</html>
        

代码imp.php

<?php

    
$sql = "INSERT INTO signup (`Email`, `First`, `Last`, `Password`, `Male`, `Female`, `Dateofb`) VALUES ('$Email', '$First', '$Last', '$Password', '$Male', '$Female', '$Dateofb');";
 
    // use exec() because no results are returned
    $conn->exec($sql);
    echo "New record created successfully";
    $conn->connection = null;
?>

问题是只提交选中的框。 您需要检查 $_POST 中是否设置了框的名称,如果没有: 手动分配一个您希望使用的值

即使用这个 short hand if/else

$Female = (isset ($_POST['Female'])) ? 1 : 0;