Cannot insert data in the database using option(textarea)

故事扮演 提交于 2019-12-02 23:40:55

问题


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

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>

回答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:

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


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'


来源:https://stackoverflow.com/questions/21296590/cannot-insert-data-in-the-database-using-optiontextarea

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!