这个php代码选项(textarea字段)将如何插入到mysql数据库中

时间:2014-01-22 11:49:45

标签: javascript php html mysql forms

大家好,你对这些代码有所了解。 textarea提交到数据库。因为我有一个connection.php但它没有插入数据库请帮助我使用选项textarea。你能帮助我phpmyadmin SQL我应该把什么名字,类型等放到tnx

select.html

     <html lang="en">
     <title>NTF 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="create.php" method="post">

         <select multiple="multiple" class="options" id="textarea">
         <option value="item1">Item 1</option>
         <option value="item2">Item 2</option>
         <option value="item3">Item 3</option>
         <option value="item4">Item 4</option>
          <option value="item5">Item 5</option>
        </select>


    <button id="copy">Copy</button>
    <button id="remove">Remove</button>

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


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

connection.php

     <?php
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "";
$db = "copy";

$conn = mysql_connect($dbhost,$dbuser,$dbpass);
mysql_select_db($db);
      ?>

submit.php
   <?php
include 'connection.php';

$food1 = $_POST['food1'];
$food2 = $_POST['food2'];
$food3 = $_POST['food3'];
$food4 = $_POST['food4'];
$food5 = $_POST['food5'];


if(!$_POST['submit']) {
    echo "please fill out the form";
    header('Location: select.html');
 } else {
         $sql = "INSERT INTO remove(food1, food2, food3, food4, food5) VALUES ('".$food1."', '".$food2."', '".$food3."','".$food4."','".$food5."');";
         mysql_query($link, $sql);
    echo "User has been added!";
    header('Location: select.html');

     }
     ?>

2 个答案:

答案 0 :(得分:-1)

让您的INSERT INTO声明成为以下形式:

$sql = "INSERT INTO remove(food1, food2, food3, food4, food5) VALUES ($food1, $food2, $food3, $food4, $food5)";     

答案 1 :(得分:-1)

您的行为有误,应为submit.php

另外请不要使用mysql_*函数,因为它们已被弃用。

改为使用mysqliPDO。还可以使用prepared statements来阻止SQL注入。

connection.php 的示例:

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

submit.php 的示例(带有预备声明):

<?php
include 'connection.php';

$food1 = $_POST['food1'];
$food2 = $_POST['food2'];
$food3 = $_POST['food3'];
$food4 = $_POST['food4'];
$food5 = $_POST['food5'];


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

     }
?>