我想创建一个函数,客户可以通过该函数通过文本区上传凭证代码(即复制和粘贴),一旦表单提交,编码就会提取每个代码并将其与自己的id一起放入数据库中。
代码示例是
gbvvvvvtr
HGBBBBBBB-
JKKKKKKKK
etc
你可以做:
$vouchers = explode("'n", $textfield);
它将变量$text字段的每一行放入一个名为$凭单的数组中。
然后,您可以循环遍历数组中的所有项目,如下所示:
foreach ($vouchers as &$value) {
// put your mysql insert here
}
您可以生成这样的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方法。此外,这可能有错误。我根本没有测试过它,但它肯定会引导你朝着正确的方向前进。