2012-01-11 27 views
-5

我想創建一個函數,通過一個客戶端可以通過textarea(即複製和粘貼)上傳代碼代碼,並且一旦表單提交,代碼將提取每個代碼並將其放置在數據庫中擁有自己的id。插入文本並將其放入MYSQL數據庫之前分離

代碼示例將

GBVVVVVVV

HGBBBBBBB

JKKKKKKKK

+0

有什麼問題... – jValdron 2012-01-11 16:59:38

+0

您是否嘗試過閱讀夢幻般的PHP手冊? http://php.net/manual是你的地方...尋找$ _POST變量和mysql函數。 – roirodriguez 2012-01-11 16:59:42

回答

1

你可以這樣做:

$vouchers = explode("\n", $textfield); 

這使變量$文本框的每行到一個數組名爲$券。

然後你可以通過數組中的所有物品,像這樣的循環:

foreach ($vouchers as &$value) { 
    // put your mysql insert here 
} 
+0

這會導致多個SQL查詢緩慢且有問題。看看我的答案,看看如何將所有內容合併成一個聲明。 – 2012-01-11 17:17:22

0

你會讓一個SQL語句,像這樣:

// Replace "<table name>" with the name of your table. 
// This sets up the first part of the SQL statement. This should look very 
// familiar if you have used any flavor of SQL. 
$query = 'INSERT INTO <table name> VALUES '; 
$is_first = true; 

// Loop through each line. 
// We assume '\n' is the line separator. 
// You will need to replace "<textarea name>" with the proper value. 
foreach (explode('\n', $_POST['<textarea name>']) as &$voucher) { 
    // This determines whether or not we need a comma to separate multiple 
    // records. See note below. 
    if ($is_first) $is_first = false; else $query .= ', '; 
    // This can be unfamiliar to some who are somewhat familiar with SQL. You can 
    // insert more than one record with just one query. 
    // Note that we escape the $voucher variable to prevent SQL injections. 
    $query .= '(' . mysql_real_escape($voucher) . ')'; 
} 

然後,通過PHP將它像正常的SQL語句一樣發送出去。請注意,這假定您正在使用post方法。此外,這可能會有錯誤。我還沒有測試過它,但它肯定會引導你朝着正確的方向發展。

相關問題