我有搜索,但没有看到任何适合我的代码的答案或示例。我是MySQL / PHP的新手,所以我可能不理解我所看到的答案。 我有一个网页发布数据,我想只使用一个插入。这是我到目前为止插入的内容:
$statement1 = $link->prepare("INSERT INTO collection_items(collection_list_id, collection_item_number, username_id, collection_item_title, collection_item_text, created_date)
VALUES(:collection_list_id, :collection_item_number, :username_id, :collection_item_title, :collection_item_text, now())");
$statement1->execute(array(
"collection_list_id" => $last_id,
"collection_item_number" => "1",
"username_id" => $user_number,
"collection_item_title" => $collection_item_title_1,
"collection_item_text" => $collection_item_text_1
),
(
"collection_list_id" => $last_id,
"collection_item_number" => "2",
"username_id" => $user_number,
"collection_item_title" => $collection_item_title_2,
"collection_item_text" => $collection_item_text_2
));
如果我删除最后一组数据,它可以正常运行一行。我在页面帖子中有大约20行数据。 TIA
这就是我所拥有的,但无法让它发挥作用。我展示了更多可以提供帮助的代码。
$form = $_POST;
$collection_item_title_1 = $form[ 'number1' ];
$collection_item_text_1 = $form[ 'comment1' ];
$collection_item_title_2 = $form[ 'number2' ];
$collection_item_text_2 = $form[ 'comment2' ];
$last_id = $link->lastInsertId();
$q= $link->query("SELECT id FROM sitelok WHERE Username='$slusername'");
$user_number = $q->fetchColumn();
$collection_item_titles = array($collection_item_title_1, $collection_item_text_1, $collection_item_title_2, $collection_item_text_2);
$statement1 = $link->prepare("INSERT INTO collection_items(collection_list_id, collection_item_number, username_id, collection_item_title, collection_item_text, created_date)
VALUES(:collection_list_id, :collection_item_number, :username_id, :collection_item_title, :collection_item_text, now())");
for ($i = 0; $i < size($collection_item_titles); $i++)
$statement1->execute(array(
"collection_list_id" => $last_id,
"collection_item_number" => "$i",
"username_id" => $user_number,
"collection_item_title" => $collection_item_title[$i],
"collection_item_text" => $collection_item_text[$i]
));
答案 0 :(得分:0)
你不能将超过1个参数传递给execute
您可以在foreach
循环中插入值,也可以创建包含数百个占位符的查询。
我认为第一种方式更好,例如:
$values = array(); // your inserted values array here
$statement1 = $link->prepare("INSERT INTO collection_items(collection_list_id, collection_item_number, username_id, collection_item_title, collection_item_text, created_date) VALUES(:collection_list_id, :collection_item_number, :username_id, :collection_item_title, :collection_item_text, now())");
foreach ($values as $v) {
$statement1->execute($v);
}
答案 1 :(得分:0)
您可以创建一个数组并循环遍历数据,而不是将数据放在单独的变量($ collection_item_title_1,$ collection_item_title_2)中,您可以执行诸如$ collection_item_titles []之类的操作,然后循环执行:
statement1 = $link->prepare("INSERT INTO collection_items(collection_list_id, collection_item_number, username_id, collection_item_title, collection_item_text, created_date)
VALUES(:collection_list_id, :collection_item_number, :username_id, :collection_item_title, :collection_item_text, now())");
for ($i = 0; $i < size($collection_item_titles); $i++)
$statement1->execute(array(
"collection_list_id" => $last_id,
"collection_item_number" => "$i",
"username_id" => $user_number,
"collection_item_title" => $collection_item_title[$i],
"collection_item_text" => $collection_item_text[$i]
);