Cannot insert data in the database using option(textarea)

前端 未结 2 385
温柔的废话
温柔的废话 2021-01-29 15:13

I got this code but it is not inserting the content of the option (textarea) into the database.

connection.php



        
相关标签:
2条回答
  • 2021-01-29 15:31

    You need to name the <select> so you can use the data.

    name="food[]"
    

    Like this

    <select multiple="multiple" name="food[]" class="options" id="text area" >
       <option value="foodA">foodA</option>
       <option value="foodB">foodB</option>
       <option value="foodC">foodC</option>
       <option value="foodD">foodD</option>
       <option value="foodE">foodE</option>
    </select>
    

    Then if you want the value to be 0 or 1, depending on selected or not, you can use the following to replace this:

    $foodA = $_POST['foodA'];
    $foodB = $_POST['foodB'];
    $foodC = $_POST['foodC'];
    $foodD = $_POST['foodD'];
    $foodE = $_POST['foodE'];
    

    to

    $foodA = 0;
    $foodB = 0;
    $foodC = 0;
    $foodD = 0;
    $foodE = 0;
    
    foreach ($_POST['food'] as $value) {
        if($value == 'foodA')
            $foodA = 1;
        if($value == 'foodB')
            $foodB = 1;
        if($value == 'foodC')
            $foodC = 1;
        if($value == 'foodD')
            $foodD = 1;
        if($value == 'foodE')
            $foodE = 1;
    }
    
    0 讨论(0)
  • 2021-01-29 15:31

    You need to name your select with name=food or something so it will be in $_POST['food']. Each option in the select does not show up in $_POST, only what is selected will be in the name of the select. Each option is not it's own thing.

    <select multiple="multiple" name="food" class="options" id="textarea" >
       <option value="foodA">foodA</option>
       <option value="foodB">foodB</option>
       <option value="foodC">foodC</option>
       <option value="foodD">foodD</option>
       <option value="foodE">foodE</option>
    </select>
    

    When you save the data it will have:

    $_POST['food'] with the value of 'foodA' if multiples, it will be 'foodA, foodB'
    
    0 讨论(0)
提交回复
热议问题