【问题标题】:store all possibles of a checkbox in mysql database using php使用php在mysql数据库中存储所有可能的复选框
【发布时间】:2013-12-10 06:17:47
【问题描述】:

![节假日][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();

  }

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

【问题讨论】:

  • 您正试图在一个表中保存多个假期,对吧?如果是这样,您需要将其设置为用逗号或其他任何内容分隔的字符串。
  • 否则您可以在 mySQL 中使用多对多关系来存储与一个或多个相关的多个数据。看看吧joinfu.com/2005/12/…

标签: php mysql pdo


【解决方案1】:

我认为 PHP serialize 很适合你。

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-11
    • 2013-12-10
    • 2018-04-04
    • 1970-01-01
    • 2015-12-12
    • 2012-08-16
    • 1970-01-01
    • 2013-08-11
    相关资源
    最近更新 更多