php - 使用php将复选框的所有可能存储在mysql数据库中

标签 php mysql pdo

![假期][1] ![1]: http://i.stack.imgur.com/bdRnV.png 我的表单是这样的,它是使用php和pdo mysql开发的。 如果我选择了三个复选框,这些名称(独立,劳动,...),开始日期这些值应存储在数据库中 我尝试使用以下代码将数据插入数据库。

    $holidays = array(

            "Independence Day" => observed_day($_GET['y'],4,7),
            "Labor Day " => get_holiday($_GET['y'], 9,1, 1),
            "Columbus Day " => get_holiday($_GET['y'],10, 1, 2),
            "Halloween " => format_date($_GET["y"],10,31),
            "DST Ends " => date('I'),
            "Veteran's Day " => format_date($_GET["y"],11,11),
            "Thanksgiving " => get_holiday($_GET['y'],11, 4, 4),
            "Christmas Event " => format_date($_GET['y'],12,24),
            "Christmas Day " => format_date($_GET['y'],12,25),
            "New Year Event " => format_date($_GET['y'],12,31),
            "New Year's Day " => format_date($_GET['n'],1, 1),
            "Martin Luther King Day " => get_holiday($_GET['n'],1,1,3),
            "Valentine's Day " => format_date( $_GET['n'],2,14),
            "President's Day " => get_holiday($_GET['n'],2,1,3),
            "DST Begins " => date('I'),
            "St. Patrick's Day " => format_date($_GET['n'],3, 17),
            "Easter " => calculate_easter($_GET['n']),
            "Mother's Day " => get_holiday($_GET['n'],5, 0, 2),
            "Memorial Day " => get_holiday($_GET['n'],5, 1, '' ),
            "Father's Day " => get_holiday($_GET['n'],6, 0, 3)

            );

 //loop for holidays list
 foreach($holidays as $name => $dates)
 {
 echo "<tr>";
echo "<td width='30%'><input type='checkbox' name='holiday[]' id='".$name."'  value='".$dates."'></td>";
echo "<td width='75%'>".$name."</td>";
  echo "<td>" .$dates."</td>";
echo "</tr>";
 }

为了检索,我使用了以下代码

 $schoolyear = $_REQUEST['schoolyear'];
 $holiday = $_REQUEST['holiday'];
 $var = nl2br(implode(', ', $holiday));
 $periods = $_REQUEST['periods'];
 $start = $_REQUEST['start'];
 $end = $_REQUEST['end'];

 $start = date2mysql($start);
 $end = date2mysql($end);

$status = 1;
$visible = 1;

$test = 0;
if ($test == 1)
{
echo $schoolyear;
echo '<br />';
echo $start;
echo '<br />';
echo $end;
echo '<br />';
echo $status;
echo '<br />';
echo $_SESSION['user_account'];
  } 

 try {


  $sqladdyr = "INSERT INTO school_years (account_id,year_name,start,end,status,added,updated) VALUES (:a_id,:name,:start,:end,:status,NOW(),NOW())";

  $q = $conn->prepare($sqladdyr);

 $q->bindParam(":start", $start, PDO::PARAM_STR);
 $q->bindParam(":end", $end, PDO::PARAM_STR);
  $q->bindParam(":a_id", $_SESSION['user_account'], PDO::PARAM_STR);
  $q->bindParam(":name", $schoolyear, PDO::PARAM_STR);
  $q->bindParam(":status", $status, PDO::PARAM_STR);
  $q->execute();

  $id = $conn->prepare("SELECT MAX(year_id) FROM school_years");  
                  //Set needed id.
  $id->execute();

   $id->setFetchMode(PDO::FETCH_OBJ);

   $id_val = $id->fetch(PDO::FETCH_NUM);

  echo $idval = $id_val[0];


  foreach($holiday as $key => $value){ 
echo $holiday[$key].'<br>';
echo $value.'<br>';
   $sql="INSERT INTO holidays (holiday_id,year_id,account_id,start,end,status,visible,added,updated) VALUES ('',:year_id,:aid,:date,:date,:status,:visible,NOW(),NOW())";
$sql = $conn->prepare($sql);
$sql->execute(array(
     ':year_id'=>$idval,
     ':aid'=>$_SESSION['user_account'],
    ':date'=>$holiday[$key],
     ':status'=>$status,
     ':visible'=>$visible,

   ));
  }

 } 
 catch(PDOException $e) {
  echo 'Error: ' . $e->getMessage();

  }

说到这里,我震惊了。我不知道如何将所选多个复选框的假期名称和日期一次存储到数据库中,请任何人帮助我。 提前致谢

最佳答案

我认为 PHP serialize为你工作得很好。

$holiday = serialize($_REQUEST['holiday']);    

然后从数据库中获取内容,然后使用 unserialize

关于php - 使用php将复选框的所有可能存储在mysql数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20487496/

相关文章:

php - 具有 MySQL 空间数据类型的 PropelORM

php - 将 php 数据绘制到 Highmap : HighCharts

php - 自动换行不让数字也换行

php - Symfony Doctrine ORM CLI 字体/颜色不起作用?

javascript - 检测 Web 应用程序中同一 session 的多个选项卡或窗口

php - 如何获取趋势线的端点?

php - Codeigniter 表单验证...表单重新填充

Mysql 使用匹配的外键加入上一行

mysql - 获取父 ID 为空的记录,并用具有 'parent id' =id 的记录替换数据

php - 将数组值存储在单独的行中