php mysql插入表textarea行

时间:2012-08-21 16:42:19

标签: php textarea row lines

我使用此表单逐个将密钥导入数据库。 如何使表单从textarea(作为行)导入键并将它们(作为行)保存在表格上。

这是我使用的代码:

HTML CODE:

<form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1">
  <table align="center">
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Product Paket:</td>
      <td><select name="produktid">
        <?php
do {  
?>
        <option value="<?php echo $row_Paketa['paketaid']?>"><?php echo $row_Paketa['emripaketa']?></option>
        <?php
} while ($row_Paketa = mysql_fetch_assoc($Paketa));
  $rows = mysql_num_rows($Paketa);
  if($rows > 0) {
      mysql_data_seek($Paketa, 0);
      $row_Paketa = mysql_fetch_assoc($Paketa);
  }
?>
      </select></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Serial Number:</td>
      <td><textarea cols="45" rows="5" name="seriali" /></textarea></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Voucher Number:</td>
      <td><textarea cols="45" rows="5" name="kodiabonimit" /></textarea></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">&nbsp;</td>
      <td><input type="submit" value="Insert record" /></td>
    </tr>
  </table>
  <input type="hidden" name="MM_insert" value="form1" />
</form>

PHP代码:

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
     $insertSQL = sprintf("INSERT INTO products (pid, produktid, seriali, kodiabonimit, data) VALUES (%s, %s, %s, %s, NOW())",
                       GetSQLValueString('', "int"),
                       GetSQLValueString($_POST['produktid'], "int"),
                       GetSQLValueString($_POST['seriali'], "text"),
                       GetSQLValueString($_POST['kodiabonimit'], "text")
                       );
  mysql_select_db($database, $ksam);
  $Result1 = mysql_query($insertSQL, $ksam) or die(mysql_error());
}

1 个答案:

答案 0 :(得分:2)

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
$codi  = $_POST['kodiabonimit'];    
$serial = $_POST['seriali'];


$text = $codi;
$text1 = $serial;



$itemserial = explode("\n", $serial);
$itemcodi = explode("\n", $codi);

foreach(array_combine($itemcodi, $itemserial) as $text => $text1)
{
//code to insert into database