php - 复选框到 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;
}
解决方案
您需要在保存之前检查哪些复选框被选中。如果没有检查,你需要给他们一个值。为每个人应用此代码。例如:
if(!isset($_POST["industry"])){
$industry = "notchecked";
}else{
$industry = $_POST["industry"];
}
推荐阅读
- stata - 如何在使用列名而不是列索引加载数据集时定义变量类型?
- arrays - 在 Swift 中为数组中的任何值断言数组
- android - 如何根据 12 小时格式在 x 轴上以 2:00 小时为间隔绘制小时数?(同步融合颤振图表)
- c# - CS1503 参数 1:无法从 'string' 转换为 'string[*,*]'
- amazon-web-services - 具有带宽限制的云文件存储
- node.js - 如何使用节点 cookie-session 创建 cookie
- java - 有没有办法跳过数组中的索引?
- git - 当没有什么有趣的东西可看时,为什么我的 Github 存储库有很多独特的克隆?
- python - 将 SQLite 导入 Python 3.7.x
- r - 转换具有 2 列和行分隔符的 R 数据框