2011-04-21 69 views
0

我需要做的是将用户选中的任何内容插入到我们在数据库中创建的表中。我能够将数据存储到数组中,但后来我无法将它存储到数据库中。通过PHP发送复选框到MySQL

<form> 
    <input type="checkbox" name="partType[]" value="GenericBreakPads" /> Generic Break Pads 
    <input type="checkbox" name="partType[]" value="Air_Conditioning_Compressor" /> Air Conditioning Compressor 
    <input type="checkbox" name="partType[]" value="Generic_Battery" /> Generic Battery <br /> 
    <input type="checkbox" name="partType[]" value="Carburetor" /> Carburetor  
    <input type="checkbox" name="partType[]" value="Engine_Balance_Shaft" /> Engine Balance Shaft    
    <input type="checkbox" name="partType[]" value="Engine_Cylinder" /> Engine Cylinder     
    </form> 

我已经尝试过序列化,但由于它存储到表中的方式并不是很有帮助。

$theParts=serialize($_POST['partType']); //takes the data from a post operation... 
$query=INSERT INTO parts VALUES('$partType'); 

如果有人可以帮助我,我会非常感激!

+1

发布您的表格结构! – Bil 2011-04-21 00:37:52

+1

你想要如何存储数据?在单列中?在多个栏目中,作为m2m关系? – prodigitalson 2011-04-21 00:39:17

+0

你真的想输入:'$ theParts ='**'mysql_real_escape_string' **'(serialize($ _ POST ['partType'])); //从后期操作中获取数据... $ query = INSERT INTO部分VALUES('$ partType'); “对,对吗? – Johan 2011-04-21 00:40:07

回答

0
$query = 'INSERT INTO parts VALUES("'. mysql_real_escape_string($partType) .'");'; 
0

你真的应该循环复选框的结果。 即

for ($i=0;$i<count($partType);$i++) { 
    //insert record into database using $partType[$i] 
} 

然后,您可以将数据插入离散行。

但是,如果你真的必须存储在一个单一的领域,那么你也可以爆炸数组到一个逗号分隔字符串。那么你可以插入串到你的数据库字段

implode("," $partType); 

这虽然打破良好的数据库设计/使用其不再归为您的存储单个数据库字段

implode();

内超过1个项目

希望有帮助