2013-12-10 26 views
0

![假期] [1] ![1]:http://i.stack.imgur.com/bdRnV.png 我的表單就是這樣,它是使用php和pdo mysql開發的。 如果我選擇了三個複選框這些名稱(獨立性,勞動力,...),開始日期這些值應該存儲在數據庫中 我試着用下面的代碼插入數據到數據庫。在mysql數據庫中存儲所有可能的複選框,使用php

$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(); 

    } 

在這裏,我strucked。我不知道如何將假日名稱和日期一次存儲到所選多個複選框的數據庫中,請任何人幫助我。 高級謝謝

+0

您正在嘗試多個假日保存在一個表吧? 如果是這樣,你需要將它作爲一個由逗號或其他任何東西分隔的字符串。 – Anto

+0

或者你也可以在mySQL中使用多對多關係來存儲多個與一個或多個數據相關的數據。 看看吧http://www.joinfu.com/2005/12/managing-many-to-many-relationships-in-mysql-part-1/ – Anto

回答

0

我認爲PHP serialize適合你。

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

然後取出由DB內容然後使用unserialize

相關問題