无法使用选项(textarea)在数据库中插入数据

时间:2022-09-26 08:43:05

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

我得到了这段代码,但它没有将选项(textarea)的内容插入到数据库中。

connection.php

<?php
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "";
$db = "copy";
$conn = mysqli_connect($dbhost,$dbuser,$dbpass,$db);
?>

submit.php

<?php
include 'connection.php';

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


if(!$_POST['submit']) {
    echo "please fill out the form";
    header('Location: select.html');
}
else {
    $sql = "INSERT INTO remove(foodA, foodB, foodC, foodD, foodE) VALUES (?,?,?,?,?);";
    $stmt = mysqli_prepare($conn, $sql);
    mysqli_stmt_bind_param($stmt,"sssss",$foodA,$foodB,$foodC,$foodD,$foodE);
    mysqli_stmt_execute($stmt);
    echo "User has been added!";
    header('Location: select.html');
}

select.html

<html lang="en">
<title>Catering Service</title>
<meta charset="utf-8">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> 
<script src="js/js.1.js" type="text/javascript"></script>
</head>
<body>
<form action="submit.php" method="post">

<select multiple="multiple" 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>


  <button type="button" id="copy" onclick="yourFunction()">Copy</button>
  <button type="button" id="remove" onclick="yourFunction()">Remove</button>

  <select id="textarea2" multiple class="remove" >


<input type="submit" name="submit" />
</form>
        </select>
        </html>

2 个解决方案

#1


1  

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:

然后,如果您希望值为0或1,具体取决于是否选中,您可以使用以下内容替换它:

$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;
}

#2


0  

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.

您需要使用name = food或其他名称命名您的选择,以便它在$ _POST ['food']中。 select中的每个选项都不会显示在$ _POST中,只有所选内容才会出现在select的名称中。每个选项都不是它自己的东西。

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

#1


1  

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:

然后,如果您希望值为0或1,具体取决于是否选中,您可以使用以下内容替换它:

$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;
}

#2


0  

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.

您需要使用name = food或其他名称命名您的选择,以便它在$ _POST ['food']中。 select中的每个选项都不会显示在$ _POST中,只有所选内容才会出现在select的名称中。每个选项都不是它自己的东西。

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