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

时间:2014-01-22 23:52:01

标签: php mysql sql forms textarea

我得到了这段代码,但没有将选项(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 个答案:

答案 0 :(得分:1)

您需要name <select>,以便您可以使用这些数据。

name="food[]"

喜欢这个

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

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

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

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

答案 1 :(得分:0)

您需要使用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>

保存数据时,它将具有:

$_POST['food'] with the value of 'foodA' if multiples, it will be 'foodA, foodB'