2013-03-19 50 views
1

我試着去插入來自看起來像這樣的表單數據:插入數據

<input type="checkbox" name="single_color_show[]" value="1" id="1" checked> 
<input type="text" name="single_color_val[]" class="input-admin-val" value="0" id="1"> 
<input type="hidden" name="single_color_name[]" value="Vit: (NCS S0502-Y)" id="1"> 
<input type="hidden" name="single_color_head[]" value="colors" id="1"> 
<input type="hidden" name="single_color_standard[]" value="1" id="1"> 
<input type="hidden" name="single_color_order[]" value="1" id="1"> 
<input type="hidden" name="single_color[]" value="1" id="1"></td> 

等等...

我要的是所有值存儲在數據庫中。只要single_color_show被選中,這工作正常。但是我需要插入數據,無論如何,但值爲0而不是1,所以腳本知道它應該隱藏。

這是我的PHP代碼:

$single_color_show = $_POST['single_color_show']; 
$single_color = $_POST['single_color']; 
$single_color_name = $_POST['single_color_name']; 
$single_color_val = $_POST['single_color_val']; 
$single_color_head = $_POST['single_color_head']; 
$single_color_standard = $_POST['single_color_standard']; 
$single_color_order = $_POST['single_color_order']; 

foreach($single_color_order as $key => $n) { 
$s_color = "INSERT INTO product_attributes (products_id, att_name, att_head, att_val, att_standard, att_order, att_show) VALUES ('".$products_id_enkel."', '".$single_color_name[$key]."', '".$single_color_head[$key]."', '".$single_color_val[$key]."', '".$single_color_standard[$key]."', '".$single_color_order[$key]."', '".$single_color_show[$key]."');"; 
$q = mysql_query($s_color) or die ('Error posting data enkeldörr färg'); 
} 

只要我把一切都,選項選中它保存數據美麗的溫馨,但因爲我需要檢索的數據爲未來的編輯,我需要所有列出的選去與選擇的屬性。

如果我檢查可以說,2出3,選項的,我想對數據進行存儲,因爲這

products_id, att_name, att_head, att_val, att_standard, att_order, att_show 
12   Red  Color  80   1   1   1 
12   Blue  Color  50   0   2   0 
12   Green  Color  70   0   3   1 

這就是我現在得到:

products_id, att_name, att_head, att_val, att_standard, att_order, att_show 
12   Red  Color  80   1   1   1 
12   Green  Color  70   0   3   1 
+0

+1好的供應碼等,但:如何與att_val記錄從其他= 50種不同記錄? – michi 2013-03-19 21:19:08

+0

我聽到那種諷刺嗎? – Martin 2013-03-19 21:20:37

+0

沒有。我是認真的。 – michi 2013-03-19 21:21:49

回答

0

你需要的複選框-HTML碼和複選框陣列工作:

(1)HTML
給每個複選框一個獨特value從0開始:

<input type="checkbox" name="box[]" value="0"/> 
<input type="checkbox" name="box[]" value="1"/> 
<input type="checkbox" name="box[]" value="2"/> 
<input type="checkbox" name="box[]" value="3"/> 
<input type="checkbox" name="box[]" value="4"/> 
<input type="checkbox" name="box[]" value="5"/> 

(2 )PHP
處理數組提交後...

if (isset($_POST['box'])) { 

    $max = count($_POST['single_color']); // get max number of elements in "full" array 

    for ($i = 0;$i < $max;$i++) 
     $box[$i] = intval(in_array($i,$_POST['box'])); 
} else $box = array(); 

(3)使用mysq li_ *或PDO,而不是mysql_ * ;-)

--->看現場演示這裏:http://codepad.viper-7.com/jUPseH

+0

謝謝你!我解決了選擇列表替換複選框與顯示/隱藏預置顯示。工作就像一個魅力:) – Martin 2013-03-19 21:46:56

+0

@Martin:當我用你的代碼工作時,你有一個好主意;-) – michi 2013-03-19 21:49:49

+0

是的,它的瘋狂:)我花了最後8個小時struggeling與這個問題,當我終於得到一個很好的答案,我想出了一個好主意,即時通訊對不起人:)雖然真的很感謝你的努力!乾杯! – Martin 2013-03-19 21:54:56

0

你可以使single_color_show屬性一個像其他人一樣的隱藏字段,然後每當複選框更改時使用javascript將其值在0和1之間切換。