2014-01-23 29 views
0

嗨,你好,你有這個代碼,textarea提交到數據庫的一些想法。因爲我有一個connection.php,但它沒有插入數據庫,請幫助我使用選項textarea。你能不能幫我也phpMyAdmin的SQL什麼名稱,類型,等我應該把,謝謝錯誤插入PHP到數據庫使用jquery函數選項textarea

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="submit.php" method="post"> 

    <select multiple="multiple" name="food" class="options" id="textarea"> 
     <option name="foodA"value="foodA">foodA</option> 
      <option name="foodB" value="foodB">foodB</option> 
       <option name="foodC" value="foodC">foodC</option> 
       <option name="foodD" value="foodD">foodD</option> 
        <option name="foodE" 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" name="food"> 


     <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); 
    ?> 

提交.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(food1, food2, food3, food4, food5) VALUES ('".$food1."', '".$food2."', '".$food3."','".$food4."','".$food5."');"; 
      mysql_query($link, $sql); 
     echo "User has been added!"; 
     header('Location: select.html'); 

    } 
    ?> 
+0

在選擇關閉之前,'select's需要名稱和表格正在關閉 –

+0

@ G.Mendes tnx您的回覆,但我已經在名稱上,但仍未插入到db – user3211646

+0

請更新代碼在選擇名稱中,'submit.php'中的'$ _POST'項必須與輸入中給出的名稱具有相同的名稱才能工作 –

回答

2

你缺少

<form action="submit.php" method="POST"> 

從形式開始。

而且更深入的調試SQL問題,ID提出:

if (mysql_errno()) { 
    echo "MySQL error ".mysql_errno().": ".mysql_error()."\n<br>When executing:<br>\n$query\n<br>"; 
exit; 
} 

權前的header()函數的mysql_query()之後。發佈錯誤,如果它發生,我們應該很容易能夠調試出了什麼問題。

+0

生病請嘗試這個我認爲我有一個表單操作? – user3211646

+0

sry代碼已被剪切:)已編輯它,我嘗試在每個食物上放置名稱,但仍未插入到db – user3211646

+0

提交時是否收到任何MySQL錯誤?點擊提交後會顯示什麼? –

0

你試圖用兩個selects,從第一到第二發送內容,所以第二個將被張貼到script.php,所以首先你需要名存實亡第二selectfood[]因爲它是一個多重選擇,也copy功能必須改變,以確保經過正確後,關閉form之前選擇#textarea2被關閉無副作用,表單代碼將改爲:

select.html

<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">Copy</button> 
    <button type="button" id="remove">Remove</button> 

    <!-- note how multiple select name must be set --> 
    <select id="textarea2" multiple class="remove" name="food[]"> 
    </select> 

    <input type="submit" name="submit" /> 
    </form> 
加入選定3210

和腳本部分,因此它會被處理:

<script> 
$(function(){ 
    $("#copy").on("click", function(){ 
    $(".options option:selected").each(function({ 
     $("#textarea2").append('<option selected>'+$(this).text()+'</option>'); 
     $('option:selected', "#textarea").remove(); 
    }); 
    }); 
    $("#remove").on("click", function(){ 
    $(".remove option:selected").each(function(){ 
     $("#textarea").append('<option>'+$(this).text()+'</option>'); 
     $('option:selected', "#textarea2").remove(); 
    }); 
    }); 
}); 
</script> 

正被改變,PHP將把textarea2作爲一組選項,以獲取一種方式他們可能是:

提交.PHP

foreach ($_POST['food'] as $food){ 
    $food == "foodA" ? $foodA = $food : $foodA = ''; 
    $food == "foodB" ? $foodB = $food : $foodB = ''; 
    $food == "foodC" ? $foodC = $food : $foodC = ''; 
    $food == "foodD" ? $foodD = $food : $foodD = ''; 
    $food == "foodE" ? $foodE = $food : $foodE = ''; 
} 

確保發送的數據是一個你數據庫預計,任何懷疑使用熱汗的調試蘇ggestions。

+0

生病嘗試這也tnx :)虐待不久的答覆 – user3211646

+0

所以在submit.php我會刪除所有?並將其更改爲foreach?或者我爲什麼會改變爲foreach? tnx – user3211646

+0

只是用'foreach'塊替換$ foodA - $ foodE塊 –