【问题标题】:Insert multiple rows with same common ID插入具有相同公共 ID 的多行
【发布时间】:2013-03-16 08:04:56
【问题描述】:

我目前正在一次插入多行。我在“协议”表中有一个“id”列,它设置为自动递增。这显然是为插入的每一行创建一个新的 id。

有没有办法一次插入具有相同 ID 的所有行?

if ($stmt = $mysqli->prepare("INSERT agreement (start, end, customer, manufacturer, item_number, item_description) VALUES (?, ?, ?, ?, ?, ?)")) {
    $stmt->bind_param("ssssss", $start, $end, $customer, $manufacturer, $number, $description);
    for ($i = 0; $i < count($_POST['item']); $i++) {
        $number = $_POST['item'][$i];
        $description = $_POST['description'][$i];
        $theid2 = $theid[$i];
        $stmt->execute();
    }
    $stmt->close();
}
// show an error if the query has an error
else {
    echo "ERROR: Could not prepare SQL statement 1.";
}


// redirect the user
}
$mysqli->error;
$mysqli->close();

【问题讨论】:

    标签: php mysql


    【解决方案1】:

    如果这是 oracle,答案是肯定的,但 MySQL 没有序列。为此,您需要一个唯一序列的来源以用于您的记录集。最简单的方法是插入另一个表并从中获取自动递增的键用作您的 id。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-20
      • 2023-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-03
      • 1970-01-01
      相关资源
      最近更新 更多