You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP循环生成多表单时如何将验证错误绑定到对应表单实例

核心修改逻辑

原有实现的错误数组没有和提交的表单绑定,导致所有表单都会判断全局$error变量是否存在进而输出错误。我们只需要将错误信息与表单唯一标识(hidden字段存储的filename)绑定,输出表单时匹配当前表单的filename是否存在对应错误即可。


一、修改PHP验证逻辑

<?php 
// 先声明全局的错误数组和旧输入存储数组
$error = [];
$old_input = [];

if(isset($_POST['upload-submit'])) {
    $image_title = $_POST['image-title'];
    $image_tags = $_POST['image-tags'];
    $form_filename = $_POST['image-filename']; // hidden表单的唯一标识

    $image_title = filter_var($image_title, FILTER_SANITIZE_STRING);
    $image_tags = filter_var($image_tags, FILTER_SANITIZE_STRING);
    $form_filename = filter_var($form_filename, FILTER_SANITIZE_STRING);
    
    // 表单验证:用filename作为错误数组的键,绑定对应表单的错误
    if(empty(trim($image_title))){
        $error[$form_filename][] = "Image Title cannot be blank";
    }

    if (empty($error[$form_filename])) {
        try {
            $sql = "UPDATE 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
            ]);
            
        } catch (PDOException $e) {
            echo "Error: " . $e->getMessage();
        }
    } else {
        // 验证失败时存储用户输入的内容,用于表单回填
        $old_input[$form_filename]['image_title'] = $image_title;
        $old_input[$form_filename]['image_tags'] = $image_tags;
    }
}
?>

二、修改表单输出代码

<?php
// $user_id 在用户登录时通过$_SESSION赋值
$stmt = $connection->prepare("SELECT * FROM imageposts WHERE user_id = :user_id");
$stmt->execute([
    ':user_id' => $user_id
]); 

while ($row = $stmt->fetch()) {
    $db_image_filename = escape($row['filename']); // 输出到hidden input的value属性
?>
          
<form method="post" enctype="multipart/form-data">
    <?php 
        // 仅当前表单的filename存在对应错误时,才输出错误
        if(isset($error[$db_image_filename])) {
            foreach($error[$db_image_filename] as $msg) {
                echo "<p>* ERROR: {$msg}</p>";
            }
        }
        // 取出当前表单的旧输入内容,没有则为空
        $current_title = isset($old_input[$db_image_filename]['image_title']) ? htmlspecialchars($old_input[$db_image_filename]['image_title']) : '';
        $current_tags = isset($old_input[$db_image_filename]['image_tags']) ? htmlspecialchars($old_input[$db_image_filename]['image_tags']) : '';
    ?>
    <div>
        <div class="form-row">
            <label for="upload-details-title">Image Title</label>
            <!-- 回填用户之前输入的内容 -->
            <input id="upload-details-title" type="text" name="image-title" value="<?php echo $current_title; ?>">
        </div>
        <div class="form-row">
            <label for="upload-details-tags">Comma Separated Image Tags</label>
            <!-- 回填用户之前输入的内容 -->
            <textarea id="upload-details-tags" name="image-tags"><?php echo $current_tags; ?></textarea>
        </div>
        <div class="form-row">
            <button name="upload-submit">COMPLETE UPLOAD</button>
        </div>
        <div class="form-row">
            <!-- 隐藏表单输入项 -->
            <input type="hidden" name="image-filename" value="<?php echo $db_image_filename; ?>"> 
        </div>
    </div>
</form>

<?php } ?>

内容的提问来源于stack exchange,提问作者pjk_ok

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.30 00:54:02