2013-07-08 70 views
0

所以即時嘗試從複選框插入選中的值,但不工作。我相信我的問題是他們沒有正確的開始。我嘗試用一​​個循環,但,但我相信即時通訊做錯了:P檢查值沒有插入到DB - PHP

我 「isset」:

if(!empty($_POST)) 
{ 

    if(empty($_POST['name'])) {die("Please enter a Your Name.");} 
    if(empty($_POST['ckboxes[]'])) {die("Please check at least one box.");} 
    if(empty($_POST['ddown'])) {die("Please select a value.");} 
    if(empty($_POST['txtDate'])) {die("Please type in a date.");} 
    if(empty($_POST['agdis'])) {die("Please agree or disagree.");} 
    if(empty($_POST['yn'])) {die("Please select Yes or No.");} 

和刀片proccess:

//Insert for Checkboxes// 
    ///////////////////////// 
    $query2 = " 
     INSERT INTO coffee_ckbx (
      white, 
      green, 
      red, 
      blue 
     ) VALUES (
      :white, 
      :green, 
      :red, 
      :blue 
     ) 
    "; 

    foreach($_POST['ckboxes'] as $check){array($check);} 

    $query_params2 = array(
     ':white' => $check['1'], 
     ':green' => $check['2'], 
     ':red' => $check['4'], 
     ':blue' => $check['5'] 
    ); 

    try 
    { 
     // Execute the query to create the user 
     $stmt = $db->prepare($query); 
     $result = $stmt->execute($query_params); 

     $stmt2 = $db->prepare($query2); 
     $result2 = $stmt2->execute($query_params2); 
    } 
    //kept the catch out and other code due to lengthiness. 

,這裏是我的html:

  <li> 
      <label for='ck-boxes'>Select from the check boxes: </label> 
      <input type="hidden" name="ckboxes" value="0" /> 
      <input type='checkbox' id='' name='ckboxes' value='white'/> 
      <input type='checkbox' id='' name='ckboxes' value='green'/> 
      <input type='checkbox' id='' name='ckboxes' value='red'/> 
      <input type='checkbox' id='' name='ckboxes' value='blue'/> 
     </li> 

任何想法? 預先感謝您:)

回答

0

嘗試將[]添加到名稱的末尾,所以這會告訴PHP這是一個數組。

<input type="hidden" name="ckboxes[]" value="0" /> 
<input type='checkbox' id='' name='ckboxes[]' value='white'/> 
<input type='checkbox' id='' name='ckboxes[]' value='green'/> 
<input type='checkbox' id='' name='ckboxes[]' value='red'/> 
<input type='checkbox' id='' name='ckboxes[]' value='blue'/> 

也不要引用數字,引號裏的東西都被視爲string

所以$check['1']$check[1]爲例。

+0

它沒有;?... T工作:( – husin

+0

在實際輸入'form' – Karo

+0

是的,他們是在形式在這裏你可以看到我的代碼,也許,這將有助於http://codepad.org/2sL3slm6 – husin