該賞金到期in 4天。回答這個問題有資格獲得 50聲望獎勵。 paulo_cam正在從信譽良好的來源尋找答案。
我有一個頁面,它在初始上傳后從資料庫中獲取影像,允許用戶向影像添加標題和標簽。
我已將while回圈設定為在一種形式中輸出單個影像。每個影像都有兩個相關的輸入欄位。我想要做的是這樣當提交表單時,資料庫中的所有資訊都會一次性更新(即使相關輸入欄位中的影像詳細資訊保持空白/未填寫)。
為了實作這一點,我認為我可以在while回圈之外設定表單的提交按鈕。但這并沒有像預期的那樣作業。它基本上一次更新一個影像,每次我按下提交(它更新回圈中的最后一個影像)。
我如何獲得它以便它在提交時一次性處理 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 = "";
}
?>
uj5u.com熱心網友回復:
您當前方法的問題在于 bcs 每組輸入使用相同的name,它們會相互覆寫。假設您的前端有 3 條記錄、3 組表單輸入。如果您在后端輸入、 和 的image-title值one,您將只會看到。twothreethree
您可以自己驗證: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 輸入來實作(假設您的主鍵是id):
<input type="hidden" name="id[]" value="<?php echo $row->id; ?>">
然后當然在后端更新您的查詢:
WHERE id = :id
// ...
':id' => $_POST['id'][$key]
可以想象,這是一個常見問題,這里還有許多其他解決方案可供參考:
- php中通過POST多個同名輸入
- 如何將表單輸入陣列轉換為 PHP 陣列
- HTML/PHP - 表單 - 作為陣列輸入
PHP 檔案參考:
- https://www.php.net/manual/en/faq.html.php#faq.html.arrays
uj5u.com熱心網友回復:
如果我正確理解您的問題,您希望將每個影像的所有多個輸入條目都保存到您的資料庫中。
為此,您首先需要將每個輸入名稱設為一個陣列。例如,在您的第一次輸入中,“image-title”您需要將其設為“image-title[]”。您還需要將“影像標簽”設為“影像標簽[]”。
完成后,您將需要它提交到的代碼中的邏輯,以遍歷每個輸入并更新資料庫。
所以現在提交腳本上的 $image_title 將是一個值陣列。您需要遍歷這些并相應地更新資料庫。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/394356.html
