Skip to content Skip to sidebar Skip to footer

Cannot Insert Data In The Database Using Option(textarea)

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

Solution 1:

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

name="food[]"

Like this

<selectmultiple="multiple"name="food[]"class="options"id="text area" ><optionvalue="foodA">foodA</option><optionvalue="foodB">foodB</option><optionvalue="foodC">foodC</option><optionvalue="foodD">foodD</option><optionvalue="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;
}

Solution 2:

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.

<selectmultiple="multiple"name="food"class="options"id="textarea" ><optionvalue="foodA">foodA</option><optionvalue="foodB">foodB</option><optionvalue="foodC">foodC</option><optionvalue="foodD">foodD</option><optionvalue="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'

Post a Comment for "Cannot Insert Data In The Database Using Option(textarea)"