我有一个页面,在初始上传后从数据库中获取图像,允许用户向图像添加标题和标签。
我设置了while
循环来输出一个表单中的各个图像。每个图像都有两个相关的输入字段。我想做的是让它在提交表格时一次性更新数据库中的所有信息(即使相关输入字段中的图像详细信息保持空白/未填写)。
为了实现这一点,我想我可以在while
循环之外设置表单的提交按钮。不过,这并没有像预期的那样奏效。它基本上每次更新一个图像,每次我按下submit(它更新循环中的最后一个图像)。
我如何获得它,以便它在提交时一次性处理while循环中所有图像的所有信息?如果我把提交按钮放在循环中,我会为每个图像得到一个按钮,这是我不想要的。
注意:我刚刚在下面对<img/>
源路径进行了硬编码,以避免添加实现这一点的不同变量,从而希望代码更简单。
从数据库获取数据并输出HMTL表单
<?php isset($_REQUEST['username']) ? $username = $_REQUEST['username'] : header("Location: login.php"); ?>
<form method="post" enctype="multipart/form-data">
<?php
if (isset($_SESSION['logged_in'])) {
$user_id = $_SESSION['logged_in'];
}
$stmt = $connection->prepare("SELECT * FROM lj_imageposts WHERE user_id = :user_id");
$stmt->execute([
':user_id' => $user_id
]);
while ($row = $stmt->fetch()) {
$db_image_filename = htmlspecialchars($row['filename']);
?>
<div class="upload-details-component">
<div class="form-row">
<img src="/project/images/image.jpg">
</div>
<div class="edit-zone">
<div class="form-row">
<label for="upload-details-title">Image Title</label>
<input id="upload-details-title" type="text" name="image-title">
</div>
<div class="form-row">
<label for="upload-details-tags">Comma Separated Image Tags</label>
<textarea id="upload-details-tags" type="text" name="image-tags"></textarea>
</div>
<div class="form-row">
<input type="hidden" name="username" value="<?php echo $username; ?>">
<input type="hidden" name="image-filename" value="<?php echo $db_image_filename; ?>">
</div>
</div>
</div>
<?php } ?>
<button type="submit" name="upload-submit">COMPLETE UPLOAD</button>
</form>
表单提交时更新数据库
<?php
if(isset($_POST['upload-submit'])) {
$image_title = $_POST['image-title'];
$image_tags = $_POST['image-tags'];
$form_filename = $_POST['image-filename']; // value attribute from hidden form element
try {
$sql = "UPDATE lj_imageposts SET
image_title = :image_title,
image_tags = :image_tags
WHERE filename = :filename";
$stmt = $connection->prepare($sql);
$stmt->execute([
':image_title' => $image_title,
':image_tags' => $image_tags,
':filename' => $form_filename
]);
// This is the URL to this actual page (basically refreshes the page)
header("Location: upload-details.php?username={$username}");
} catch (PDOException $e) {
echo "Error: " . $e->getMessage();
}
// give values an empty string to avoid an error being thrown before form submission if empty
$image_title = $image_tags = "";
}
?>
当前方法的问题是bcs每组输入使用相同的name
,它们会相互覆盖。假设你有3张记录,3组表单输入在你的前端。如果为image-title
输入值one
、two
和three
,则在后端只能看到three
。
您可以自己验证:print_r($_POST);
。
解决方案是将数据作为数组发送,只需在输入名称中使用[]
即可:
<?php while ($row = $stmt->fetch()) { ?>
<input type="text" name="image-title[]">
<textarea type="text" name="image-tags[]"></textarea>
<input type="hidden" name="image-filename[]" value="<?php echo $db_image_filename; ?>">
<?php } ?>
<button type="submit" name="upload-submit">COMPLETE UPLOAD</button>
现在,您将在后端获得输入数组,例如(再次使用print_r($_POST);
自己查看):
[image-title] => Array
(
[0] => First Title
[1] => 2nd Title
)
[image-tags] => Array
(
[0] => foo
[1] => bar
)
[image-filename] => Array
(
[0] => filename
[1] => another-filename
)
所以你可以循环浏览它们,一次处理一个:
$sql = "UPDATE lj_imageposts SET
image_title = :image_title,
image_tags = :image_tags
WHERE filename = :filename";
$stmt = $connection->prepare($sql);
foreach ($_POST['image-title'] as $key => $value) {
$stmt->execute([
':image_title' => $_POST['image-title'][$key],
':image_tags' => $_POST['image-tags'][$key],
':filename' => $_POST['image-filename'][$key]
]);
}
请注意,使用文件名作为唯一标识符并不理想——这就是ID的作用。文件名可能存在非唯一性的风险,但对于像UPDATE ... WHERE id=:id
这样的查询(假设您的表具有主键id
),DB查询将更加高效。您可以通过将隐藏的文件输入替换为隐藏的ID输入来做到这一点:
<input type="hidden" name="id[]" value="<?php echo $row->id; ?>">
然后当然在后端更新您的查询:
WHERE id = :id
// ...
':id' => $_POST['id'][$key]
更好的是,您可以在输入name
中使用id
来明确标识值集:
<input type="text" name="image-title[<?php echo $row->id; ?>]">
<textarea type="text" name="image-tags[<?php echo $row->id; ?>]"></textarea>
在这种情况下,您甚至不需要将id
(或image-filename
)作为一个单独的隐藏输入传递——您已经在输入名称中拥有了所有的ID。在后端,你可以做:
$sql = "UPDATE lj_imageposts SET
image_title = :image_title,
image_tags = :image_tags
WHERE id = :id";
$stmt = $connection->prepare($sql);
foreach ($_POST['image-title'] as $id => $value) {
$stmt->execute([
':image_title' => $_POST['image-title'][$id],
':image_tags' => $_POST['image-tags'][$id],
':id' => $id
]);
}
正如你所能想象的,这是一个常见的问题,这里有许多其他解决方案可供参考:
- 通过php中的POST进行多个同名输入
- 如何将表单输入数组转换为PHP数组
- HTML/PHP-表单-以数组形式输入
PHP文档参考:
- https://www.php.net/manual/en/faq.html.php#faq.html.arrays
如果我正确理解你的问题,你希望每个图像的所有多个输入项都保存到你的数据库中。
要做到这一点,首先需要使每个输入名称成为一个数组。例如,在第一次输入时;图像标题";你需要把它做成";图像标题[]";。您还需要";图像标签";成为";图像标签[]";。
完成后,您将需要它提交给的代码中的逻辑,以循环通过每个输入并更新数据库。
所以现在提交脚本上的$image_title将是一个值数组。您需要循环浏览这些内容并相应地更新数据库。
首先,在问题/代码中,您使用filename
字段作为表lj_imageposts
中记录的唯一标识符。我假设您确信该字段中的值对于您拥有的每个用户都是唯一的。我强烈建议您在表中创建一个唯一的自动递增字段id
(如果您没有)并使用它。在我的回答中,我将使用id
字段来识别图像。
while ($row = $stmt->fetch()) {
$db_image_id = $row['id']; // the unique id for this image
$db_image_filename = htmlspecialchars($row['filename']);
...
...
其次,当您创建/构建表单时,请使用数组名称作为输入,并确保每个图像都有一个唯一的输入名称。要执行此操作,请替换此名称:
<input id="upload-details-title" type="text" name="image-title">
到此:
<input id="upload-details-title_<?=$db_image_id;?>" type="text" name="image-title[<?=$db_image_id;?>]">
重要您需要为循环中的每个input
和textarea
执行此操作。
在这一点上,当你提交你的表格时,你的$_POST
应该看起来像这个
array(
'image-title' => array(
1 => 'First Image Title',
2 => 'Second Image Title',
3 => 'Third Image Title',
...
...
),
'image-tags' => array(
1 => 'First Image Tags',
2 => 'Second Image Tags',
3 => 'Third Image Tags',
...
...
),
...
...
)
其中1,2,3。。。你的图像的ID是吗
第三,在你的更新代码中,你需要循环寻找你有数据的图像,并逐一更新图像:
if(isset($_POST['upload-submit'])) {
foreach( $_POST['image-title'] as $image_id => $dummy) { // at this point I only need the ID
$image_title = $_POST['image-title'][$image_id];
$image_tags = $_POST['image-tags'][$image_id];
// I don't need this value to identify the record. in this example I'm using `id` field
//$form_filename = $_POST['image-filename'][$imageId]; // value attribute from hidden form element
try {
$sql = "UPDATE lj_imageposts SET
image_title = :image_title,
image_tags = :image_tags
WHERE id = :id";
$stmt = $connection->prepare($sql);
$stmt->execute([
':image_title' => $image_title,
':image_tags' => $image_tags,
':id' => $image_id // using id instead of filename as identifier
]);
} catch (PDOException $e) {
echo "Error: " . $e->getMessage();
}
}
// move the "redirect" header to outside my update loop
// This is the URL to this actual page (basically refreshes the page)
header("Location: upload-details.php?username={$username}");
// also, exit to only send the redirect header not anything else
exit;
}