【问题标题】:Inserting multiple rows in a table using PHP使用 PHP 在表中插入多行
【发布时间】:2012-01-29 20:14:26
【问题描述】:

我正在尝试使用 PHP 和 HTML 将多行插入 MySQL DB。我知道基本的 PHP,并在不同的论坛上搜索了许多示例并创建了一个脚本,但它似乎不起作用。任何人都可以帮助解决这个问题。这是我的脚本:

include_once 'include.php';

foreach($_POST['vsr'] as $row=>$vsr) {
   $vsr=mysql_real_escape_string($vsr);
   $ofice=mysql_real_escape_string($_POST['ofice'][$row]);
   $date=mysql_real_escape_string($_POST['date'][$row]);
   $type=mysql_real_escape_string($_POST['type'][$row]);
   $qty=mysql_real_escape_string($_POST['qty'][$row]);
   $uprice=mysql_real_escape_string($_POST['uprice'][$row]);
   $tprice=mysql_real_escape_string($_POST['tprice'][$row]);
}

$sql .= "INSERT INTO maint_track (`vsr`, `ofice`, `date`, `type`, `qty`, `uprice`,
`tprice`) VALUES ('$vsr','$ofice','$date','$type','$qty','$uprice','$tprice')";

$result = mysql_query($sql, $con);

if (!$result) {
   die('Error: ' . mysql_error());
} else {
   echo "$row record added";
}

【问题讨论】:

  • 请澄清“它似乎不起作用”。
  • 您正在保存数组中的最后一个值(例如 $_POST['office']),因为查询在您的 foreach 块之后
  • _POST['vsr']来自哪里?

标签: php mysql multiple-records


【解决方案1】:

MySQL 可以在单个查询中插入多行。我让您的代码尽可能接近原始代码。请记住,如果您有大量数据,这可能会创建一个比 MySQL 接受的更大的查询。

include_once 'include.php';

$parts = array();    
foreach($_POST['vsr'] as $row=>$vsr) {
   $vsr=mysql_real_escape_string($vsr);
   $ofice=mysql_real_escape_string($_POST['ofice'][$row]);
   $date=mysql_real_escape_string($_POST['date'][$row]);
   $type=mysql_real_escape_string($_POST['type'][$row]);
   $qty=mysql_real_escape_string($_POST['qty'][$row]);
   $uprice=mysql_real_escape_string($_POST['uprice'][$row]);
   $tprice=mysql_real_escape_string($_POST['tprice'][$row]);

   $parts[] = "('$vsr','$ofice','$date','$type','$qty','$uprice','$tprice')";
}

$sql = "INSERT INTO maint_track (`vsr`, `ofice`, `date`, `type`, `qty`, `uprice`,
`tprice`) VALUES " . implode(', ', $parts);

$result = mysql_query($sql, $con);

【讨论】:

  • 谢谢路易斯。上面的脚本工作正常。我有两个问题。如果表单中的少数/任何字段为空白,如何忽略第二个有没有办法编辑通过 imlode 插入的数据?提前致谢。
  • 为了忽略数据,空值并没有错。至于编辑数据,我不明白你的意思。数据只是简单地添加到循环中的数组中。 Implode 只是将字符串连接在一起以构建完整的查询。
【解决方案2】:

请试试这个代码。 Mysql 查询将不接受使用 php 的多次插入。由于它是一个 for 循环并且值是动态变化的,因此您可以在 for each 循环中包含 sql insert 查询。它将使用动态值插入每一行。请检查以下代码,如果您有任何疑问,请告诉我

include_once 'include.php';

foreach($_POST['vsr'] as $row=>$vsr) {
   $vsr=mysql_real_escape_string($vsr);
   $ofice=mysql_real_escape_string($_POST['ofice'][$row]);
   $date=mysql_real_escape_string($_POST['date'][$row]);
   $type=mysql_real_escape_string($_POST['type'][$row]);
   $qty=mysql_real_escape_string($_POST['qty'][$row]);
   $uprice=mysql_real_escape_string($_POST['uprice'][$row]);
   $tprice=mysql_real_escape_string($_POST['tprice'][$row]);

   $sql = "INSERT INTO maint_track (`vsr`, `ofice`, `date`, `type`, `qty`, `uprice`,
`tprice`) VALUES ('$vsr','$ofice','$date','$type','$qty','$uprice','$tprice')";

 $result = mysql_query($sql, $con);

 if (!$result)
 {
    die('Error: ' . mysql_error());
 } 
 else 
 {
    echo "$row record added";
 }
}

【讨论】:

    【解决方案3】:

    我更喜欢一种更现代的方法,它创建一个准备好的语句并绑定参数,然后在循环中执行。这提供了稳定/安全的插入查询,并避免进行如此多的转义调用。

    代码:

    // switch procedural connection to object-oriented syntax
    $stmt = $con->prepare('INSERT INTO maint_track (`vsr`,`ofice`,`date`,`type`,`qty`,`uprice`,`tprice`)
                           VALUES (?,?,?,?,?,?,?)');  // use ?s as placeholders to declare where the values will be inserted into the query
    $stmt->bind_param("sssssss", $vsr, $ofice, $date, $type, $qty, $uprice, $tprice);  // assign the value types and variable names to be used when looping
    
    foreach ($_POST['vsr'] as $rowIndex => $vsr) {
        /*
          If you want to conditionally abort/disqualify a row...
          if (true) {
              continue;
          }
        */
        $ofice  = $_POST['ofice'][$rowIndex];
        $date   = $_POST['date'][$rowIndex];
        $type   = $_POST['type'][$rowIndex];
        $qty    = $_POST['qty'][$rowIndex];
        $uprice = $_POST['uprice'][$rowIndex];
        $tprice = $_POST['tprice'][$rowIndex];
        echo "<div>Row# {$rowIndex} " . ($stmt->execute() ? 'added' : 'failed') . "</div>";
    }
    

    要拒绝插入一行,请使用我的 sn-p 中注释的条件 continue——当然,在 true 所在的位置写下你的逻辑(循环内执行调用之前的任何地方都可以) .

    要调整提交的值,请在执行调用之前覆盖迭代变量(例如$vsr$ofice 等)。

    如果您想享受更大的数据类型特异性,可以根据需要将s(字符串)替换为i(整数)或d(双精度/浮点)。

    【讨论】:

      猜你喜欢
      • 2022-11-18
      • 2015-02-01
      • 1970-01-01
      • 2014-02-25
      • 2012-01-04
      • 2021-10-23
      • 2011-11-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多