首页 > 解决方案 > 复选框到 php 数据库

问题描述

嗨,我正在执行一项任务,将 3 个检查项目输入到我的数据库中。我不能为此使用数组。

这是我的表格:

 <p>What are your categories of interest? (Select as many as you'd 
 like below)</p>
 <input name="industry" type="checkbox" value="industry" 
 />Industry<br />
 <input name="technical" type="checkbox" value="technical" 
 />Technical<br />
 <input name="career" type="checkbox" value="career" />Career<br />

这是我的php:

$name = $_POST["name"];
$email = $_POST["email"];
$industry = $_POST["industry"];
$technical = $_POST["technical"];
$career = $_POST["career"];
$role = $_POST["role"];

include("includes/dbconfig.php");

$stmt = $pdo->prepare("INSERT INTO `contacts` (`contactid`, `name`, 
`email`, `industry`, `technical`, `career` `role`) VALUES (NULL, 
'$name', '$email', '$industry', '$technical', '$career', 
'$role');");

$stmt->execute();

header("location:contactthankyou.php");



?>

我的数据库表有 3 列(行业、技术、职业)。

没有任何效果,请帮忙

我试过这个:

if(! isset($_POST["industry"]))
{$industry = "notchecked";

}else{
$industry = $_POST["industry"];}

那没起效...

和这个:

$industry = false;
if( isset($_POST["industry"])) 
{
  $industry = true;
}

标签: phpdatabaseformscheckbox

解决方案


您需要在保存之前检查哪些复选框被选中。如果没有检查,你需要给他们一个值。为每个人应用此代码。例如:

if(!isset($_POST["industry"])){

$industry = "notchecked";

}else{
$industry = $_POST["industry"];

}

推荐阅读