gpt4 book ai didi

php mysql 用 1 个查询插入 X 行多行

转载 作者:行者123 更新时间:2023-11-29 13:59:04 25 4
gpt4 key购买 nike

我想将 $add_rows = $_POST['add-rows'] 中的行数插入到表中。对于我现在的工作,我需要 32 行。我该如何让 SQL VALUES 成为一个循环,以便它输入 32 行?

这是我的表格

<form>
<input name="add-name" id="add-name" type="text" value="">
<input name="add-start" id="add-start" type="text" value="">
<input name="add-end" id="add-end" type="text" value="">
<input name="add-rows" id="add-rows" type="text" value="">
<input name="add-submit" id="add-submit" type="submit" value="Add" />
</form>

下面是我将从表单发布的值,其余值可以设置为 NULL

$add_name = $_POST['add-name'];
$add_start = $_POST['add-start'];
$add_end = $_POST['add-end'];
$add_rows = $_POST['add-rows']; //in this case, the value is 32

这是查询...我需要输入 32 个值。

mysql_query("INSERT INTO table position, name, start, end, code, one, two, three, four, five, six) 
VALUES (NULL, '$add_name', '$add_start', '$add_end', NULL, NULL, NULL, NULL, NULL, NULL, NULL)")
or die(mysql_error());

最佳答案

这将为您提供 32 行。

$sql = "INSERT INTO table (position, name, start, end, code, one, two, three, four, five, six) 
VALUES (NULL, '$add_name', '$add_start', '$add_end', NULL, NULL, NULL, NULL, NULL, NULL, NULL)"
. str_repeat(", (NULL, '$add_name', '$add_start', '$add_end', NULL, NULL, NULL, NULL, NULL, NULL, NULL)", $add_rows - 1)
mysql_query($sql);

关于php mysql 用 1 个查询插入 X 行多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15367341/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com