2013-07-17 62 views
-6
一行

我正在裝箱形式,如何添加複選框intems到數據庫中

<td> 
       <input type="checkbox" name="mode[]" id="mode" value="sweet" /> 
        Sweet<br /> 
        <input type="checkbox" name="mode[]" id="mode" value=" sour" /> 
         Sour<br /> 
        <input type="checkbox" name="mode[]" id="mode" value=" creamy" /> 
         Creamy<br /> 
         <input type="checkbox" name="mode[]" id="mode" value="bland" /> 
         Bland<br />     
       </td> 

和PHP代碼,

for ($i=0; $i<sizeof($mode); $i++) 
{ 
    $emode = mysql_real_escape_string($mode[$i]); 

    $query =<<<EOF 
INSERT INTO frutesdetails(fruitname,fruitcolor,seasonfrom,seasonto,fruitetaste,fruitbenefit) 
VALUES ('{$fruitname}', '{$category}', 
     '{$startdate}','{$enddate}','{$emode}','{$fruitbenefit}') 
EOF; 
$insertresults = mysql_query($query) or die(mysql_error()); 

} 

我得到了所有的記錄,但不是在一個recod,如果我檢查2複選框我得到兩個不同行中的兩個記錄不在單行

+3

什麼? 「如果我檢查2複選框,我得到兩個,像三個」沒有任何意義。這個問題質量很低 – pattyd

回答

2

如果你想要它全部存儲在一個記錄中,首先刪除for()循環。然後你有幾個選項可以選擇:

$emode = mysql_real_escape_string(json_encode($mode)); 
$emode = mysql_real_escape_string(serialize($mode)); 
$emode = mysql_real_escape_string(implode(',',$mode)); 

然後,你只是解碼的出路。

+0

等待你是否想把它們全部存儲在一個記錄中或者是否想要檢查每個盒子的一個記錄? – Pitchinnate

+0

如何在代碼中使用此代碼? –

+0

擺脫for循環。然後用我列出的選項之一替換'$ emode = mysql_real_escape_string($ mode [$ i]);''。 – Pitchinnate

1

如果我正確理解你的問題,你的問題是,

  • 你的PHP代碼中插入一個一行到數據庫中爲所選的每個複選框,但
  • 你想讓它插入一行包含所有複選框選項。

但是,這是你做你的代碼到底是什麼 - 你遍歷$mode$mode執行你的mysql_query呼籲每個元素。如果你想要在同一行中進行所有選擇,那麼讓$emode作爲所有選擇的代表而不是其中的一個選項,然後擺脫那個for循環。

+0

你能告訴我什麼解決方案 –

相關問題