开发者

Posting check boxes into MySQL through PHP

What I need to do is insert whatever is checked by the user into the table that we created in our database. I am able to get the data stored into an array, but then I cant get it stored into the database.

<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>

I have tried serialize but that wasn't very helpful due to the way it stored it into the table.

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

If anyone could help me I would really appreciate it!


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


you really should loop through the checkbox results. ie

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

you can then insert the data on discrete rows.

However if you really must store in a single field then you could also just implode the array into a comma seperated string. you can then insert that string into your database field

implode("," $partType);

This is breaking good database design/usage though as its no longer normalised as your storing more than 1 item inside a single database field

implode();

Hope that helps

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜