1

我正在尝试使用 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";
}
4

3 回答 3

3

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);
于 2011-12-29T13:34:44.627 回答
0

请尝试此代码。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";
 }
}
于 2011-12-29T11:56:40.940 回答
0

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

代码:

// 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>";
}

要拒绝插入一行,请使用continue我的代码片段中注释的条件——当然,写你的逻辑 where trueis (在循环内执行调用之前的任何地方)。

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

如果您想享受更大的数据类型特异性,您可以根据需要将s(string)替换为i(integer) 或d(double/float)。

于 2020-01-24T06:39:48.083 回答