multiple values insertion of checkbox only last value is taking in database

后端 未结 3 1136
自闭症患者
自闭症患者 2021-01-15 19:33

I need to insert more than one values in checkbox but now it is seemed to be only the last value am entering is inserting to database.

this is my HTML code

相关标签:
3条回答
  • 2021-01-15 20:11

    Your problem is likely:

    You have multiple checkboxes.

    And you want to insert all of them into database.

    You need to loop your posted checkboxes.

    Your problem is likely that you are not looping over the $_POST['product'], so, only, the last product is inserting into database.

    So, your checkboxes:

    <input id="option5" type="checkbox" name="product[]"  value="<?php echo $result["software"];?>">
    <label for="option5"><span><span></span></span><?php echo $result["software"];?></label>
    

    The code for posted file:

    if (! empty($_POST['product'])) {
     foreach ($_POST['product'] as $product) {
      // Here, you insert $product into database.
     }
    }
    
    0 讨论(0)
  • 2021-01-15 20:23

    If i understand your problem you are looking for something like this:-

    $array= array(
    '0' => 123,
    '1' => 456,
    '2' => 789, 
    ); // this is your post product array oryou want to get last value of that arrray
        -----Or----
    $array = $_POST['product']; // your post data after submission
    
    end($array);         // move the internal pointer to the end of the array
    $key = key($array);  // fetches the key of the element pointed to by the internal pointer
    echo $array[$key]; // 789
    

    Hope it helps!

    0 讨论(0)
  • 2021-01-15 20:32

    If you want to save multiple values in database then you need to create multiple checkboxes in html and then save the values with implode() method as below:-

    <input id="option1" type="checkbox" name="product[]"  value="value1">
    <label for="option1">Value1</label>
    <input id="option2" type="checkbox" name="product[]"  value="value2">
    <label for="option2">Value2</label>
    <input id="option3" type="checkbox" name="product[]"  value="value3"> 
    <label for="option3">Value3</label>
    

    now to store in database you can use implode() method.

    $values=implode(",",$_POST['product']);
    

    now store the $values in the database.

    0 讨论(0)
提交回复
热议问题