0

i want insert this form value to datanase :

<input type="checkbox" name="brand1" id="brand1" value="1"> <label for="brand1">Brand 1</label>
<input type="checkbox" name="brand2" id="brand2" value="1"> <label for="brand2">Brand 2</label>
<input type="checkbox" name="brand3" id="brand3" value="1"> <label for="brand3">Brand 3</label>
<input type="checkbox" name="brand4" id="brand4" value="1"> <label for="brand4">Brand 4</label>
<input type="checkbox" name="brand5" id="brand5" value="1"> <label for="brand5">Brand 5</label>

these text box are get by php from a table in database and may be Variable

i want insert to database by this format if brand 1 are checked $brand="1,";

and Finally like this :

insert($name,$brands); and $brands = "1,2,3,4,5,";

if write this by if and while but it doesn't work because if insert run in while {} Five times insert Done and if insert run out of while {} , $brand = "5,"

thanks for your help or idea for this problem

it's mean :

<form method="post" action="#">
<?php
$result = $db->getall(brands);
        if(!empty($result)) {
    while ( list($key,$val)=each($result) ) {
        $brand_id = stripslashes($val["id"]);
        $brand_name = stripslashes($val["name"]);
    ?>
    <input type="checkbox" name="brand<?php print"$brand_id"; ?>" value="1" style="cursor:pointer;"><label for="brand<?php print"$brand_id"; ?>" style="cursor:pointer;"> <?php print"$brand_name"; ?></label>
    <?php }} ?>

Source Output:

<input type="checkbox" name="brand1" value="1"> <label for="brand1">Brand Name 1</label>
<input type="checkbox" name="brand2" value="1"> <label for="brand2">Brand Name 2</label>
<input type="checkbox" name="brand3" value="1"> <label for="brand3">Brand Name 3</label>
<input type="checkbox" name="brand4" value="1"> <label for="brand4">Brand Name 4</label>
<input type="checkbox" name="brand5" value="1"> <label for="brand5">Brand Name 5</label>
<input type="submit" value="Submit" />
</form>

when submit form , insert source is :

    <?php
$result = $db->getall(brands);
        if(!empty($result)) {
    while ( list($key,$val)=each($result) ) {
        $brand_id = brand.stripslashes($val["id"]);
        $brand_name = stripslashes($val["name"]);
    $brand_ids = "brand.$brand_id";
    if($$brand_ids==1) {$brands="$brandid,"}

}} ?>
$db->add_submenu("$brands"); 
2
  • I think you should try to edit your question and explain everything once again, because it's difficult to understand how do you want to insert and what is your problem. Commented Jun 18, 2011 at 8:00
  • $db->add_submenu("$brands"); is outside of PHP block. Commented Jun 18, 2011 at 9:38

1 Answer 1

2

You should change the name of your checkboxes to brand[]. It will give you an array once submitted at $_POST['brand']

Ex. 

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

on the other side you can either do something like the following:

// this will return '1, 2, 3, 4, 5' when all are selected. 
$index = implode(", ", $_POST['brand']); 

and at that point you will have the brands in comma delimited form.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.