【问题标题】:How to store multiple checkbox values in multiple columns of same table in database using PHP?如何使用PHP将多个复选框值存储在数据库中同一表的多个列中?
【发布时间】:2017-10-11 16:00:05
【问题描述】:

在下面的代码中,我选择了 4 个复选框,并且能够显示这 4 个选中的复选框。但是如何将这 4 个选中的复选框值存储在我数据库的同一个表中的演讲 1、演讲 2 等下的不同列中。 4 是讲座复选框的默认值。

$teachername1=$_POST[ 'teachername1' ];
echo"$teachername1";
$_SESSION['teachername1']=$_POST[ 'teachername1' ];
if(!empty($_POST['lecture']))
{
   $checked_count = count($_POST['lecture']);
   echo"You have selected following ".$checked_count."option(s):<br/>";
   foreach($_POST['lecture'] as $selected)
   {
      echo $selected."<br>";
   }
}    

【问题讨论】:

标签: php mysql checkbox


【解决方案1】:

您还没有发布表格结构,但无论如何我会尝试在这里给出我最好的建议。

现有系统:

您的表格中有 4 个选择列,例如:

将所有 4 列设置为布尔值,通过分别插入 1 或 0 来检查它们是否已设置 (1) 或未设置 (0),以跟踪它们是否选择了选项。

id,(other columns),choice1, choice2, choice3, choice4

确保每当您插入复选框选定的值时,同时将数据库表集 1 或 0 插入到相应的表中。

当您获取相应行的记录时,如果列设置为 1,则该复选框被选中。

你的系统有问题。请使用以下方式。

建议方式:

请不要将复选框值存储在不同的列中。今天你可能只有 4 个值。想想将来你可能会想出 8 个复选框。在这种情况下,您需要再添加 4 列。

除此之外,您可以通过另外一个具有相应名称的表来规范您的数据库表。现在你的新规范化表看起来像

checkbox table
--------------

id, checkbox_name, checkbox_value(if any)

teachers table
--------------

id, cols (other columns which I dont know whether its there or not)

teachers_choices //This is the checkbox selected table
----------------

id, teacher_id(this references to teacher table), checkbox_id (this references to checkbox table)

【讨论】:

  • 感谢您的建议:)
猜你喜欢
  • 2011-08-12
  • 2015-02-22
  • 1970-01-01
  • 1970-01-01
  • 2019-08-13
  • 2012-12-11
  • 2019-02-26
  • 2018-03-29
  • 1970-01-01
相关资源
最近更新 更多