2012-01-11 7 views
-5

私は、クライアントがテキストエリア(つまりコピー&ペースト)を介してバウチャーコードをアップロードしてフォームを送信し、各コードを抽出して配置する機能を作成したいと考えています独自のIDを持つデータベース内に存在します。MYSQLデータベースに入れる前にテキストを挿入して区切る

のコード例は、

GBVVVVVVV

HGBBBBBBB

だろうJKKKKKKKK

など

+0

質問です何... – jValdron

+0

あなたは素晴らしいPHPのマニュアルを読んでみましたがありますか? http://php.net/manualがあなたの場所です... $ _POST変数とmysql関数を探します。 – roirodriguez

答えて

1

あなたが行うことができます:配列に変数$テキストフィールドの各ラインを置く

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

は$バウチャーと呼ばれます。

あなたはそのようにのような配列のすべての項目をループできます

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

これは、遅くて問題の多い複数のSQLクエリを引き起こします。私の答えを見て、すべてを1つのステートメントにまとめる方法を見てみましょう。 –

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メソッドを使用していることを前提としています。また、これにバグがある可能性があります。私はまったくそれをテストしていないが、間違いなくあなたを正しい方向に導くだろう。

関連する問題