2013-03-19 59 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之间切换。