18910140161

JavaScript-更新一个图像而不更新所有图像-堆栈溢出

顺晟科技

2022-10-19 12:25:16

115

我正在创建一个简单的图库,我有数据库,这样我就可以上传,更新和删除图片。上传、更新和删除已经在工作。但是我的更新有问题,因为每次更新一张图片,我都需要更新所有的图片。

我的问题是如何更新一张图片而不更新所有图片?

我想问题出在我的javascript上,但我对它不太熟悉,这就是为什么我不能解决它。我也寻找了可能的解决方案,但仍然找不到,这就是我在这里问的原因。

HTML表单

<form class="form-valide"  method="post" action="edit_query_maingallery.php?photo_id=<?php echo $fetch['photo_id'] ?>" enctype="multipart/form-data">
    <div class="form-group row">
        <label class="col-lg-4 col-form-label" for="val-digits">Photo Description For Gallery: <span class="text-danger">*</span></label>
        <div class="col-lg-6">
            <textarea name="photo_description" placeholder="Description" required="" rows="4" cols="50"><?php echo $fetch['photo_description'] ?></textarea>
        </div>
    </div>

    <div class="form-group row">
        <label class="col-lg-4 col-form-label" for="val-digits">Main Gallery Preview: <span class="text-danger"></span></label>
        <div class="col-lg-6">
            <label>Main Photo </label>
            <div id = "preview" style = "width:150px; height :150px; border:1px solid #000;">
                <img src = "../photo/<?php echo $fetch['photo'] ?>" id = "lbl" width = "100%" height = "100%"/>
            </div>
            <input type = "file" required = "required" id = "photo" name = "photo" />
        </div>
    </div>

    <div class="form-group row">
        <label class="col-lg-4 col-form-label" for="val-digits">Main Gallery Preview 1: <span class="text-danger">*</span></label>
        <div class="col-lg-6">
            <label>Photo 1 </label>
            <div id = "preview1" style = "width:150px; height :150px; border:1px solid #000;">
                <img src = "../photo/<?php echo $fetch['photo1'] ?>" id = "lbl1" width = "100%" height = "100%"/>
            </div>
            <input type = "file" required = "required" id = "photo1" name = "photo1" />
        </div>
    </div>

    <div class="form-group row">
        <label class="col-lg-4 col-form-label" for="val-digits">Main Gallery Preview 2: <span class="text-danger">*</span></label>
        <div class="col-lg-6">
            <label>Photo 2 </label>
            <div id = "preview2" style = "width:150px; height :150px; border:1px solid #000;">
                <img src = "../photo/<?php echo $fetch['photo2'] ?>" id = "lbl2" width = "100%" height = "100%"/>
            </div>
            <input type = "file" required = "required" id = "photo2" name = "photo2" />
        </div>
    </div>

    <div class="form-group row">
        <label class="col-lg-4 col-form-label" for="val-digits">Main Gallery Preview 3: <span class="text-danger">*</span></label>
        <div class="col-lg-6">
            <label>Photo 3 </label>
            <div id = "preview3" style = "width:150px; height :150px; border:1px solid #000;">
                <img src = "../photo/<?php echo $fetch['photo3'] ?>" id = "lbl3" width = "100%" height = "100%"/>
            </div>
            <input type = "file" required = "required" id = "photo3" name = "photo3" />
        </div>
    </div>
    
    <div class="form-group row">
        <label class="col-lg-4 col-form-label" for="val-digits">Main Gallery Preview 4: <span class="text-danger">*</span></label>
        <div class="col-lg-6">
            <label>Photo 4 </label>
            <div id = "preview4" style = "width:150px; height :150px; border:1px solid #000;">
                <img src = "../photo/<?php echo $fetch['photo4'] ?>" id = "lbl4" width = "100%" height = "100%"/>
            </div>
            <input type = "file" required = "required" id = "photo4" name = "photo4" />
        </div>
    </div>

    <div class="form-group row">
        <div class="col-lg-8 ml-auto">
            <button type="submit" class="btn btn-success" name="edit_maingallery">Update</button>
            <button class="btn btn-danger" name="cancel"><a href="view_maingallery.php">Cancel</a></button>
        </div>
    </div>
</form>

JavaScript

<form class="form-valide"  method="post" action="edit_query_maingallery.php?photo_id=<?php echo $fetch['photo_id'] ?>" enctype="multipart/form-data">
    <div class="form-group row">
        <label class="col-lg-4 col-form-label" for="val-digits">Photo Description For Gallery: <span class="text-danger">*</span></label>
        <div class="col-lg-6">
            <textarea name="photo_description" placeholder="Description" required="" rows="4" cols="50"><?php echo $fetch['photo_description'] ?></textarea>
        </div>
    </div>

    <div class="form-group row">
        <label class="col-lg-4 col-form-label" for="val-digits">Main Gallery Preview: <span class="text-danger"></span></label>
        <div class="col-lg-6">
            <label>Main Photo </label>
            <div id = "preview" style = "width:150px; height :150px; border:1px solid #000;">
                <img src = "../photo/<?php echo $fetch['photo'] ?>" id = "lbl" width = "100%" height = "100%"/>
            </div>
            <input type = "file" required = "required" id = "photo" name = "photo" />
        </div>
    </div>

    <div class="form-group row">
        <label class="col-lg-4 col-form-label" for="val-digits">Main Gallery Preview 1: <span class="text-danger">*</span></label>
        <div class="col-lg-6">
            <label>Photo 1 </label>
            <div id = "preview1" style = "width:150px; height :150px; border:1px solid #000;">
                <img src = "../photo/<?php echo $fetch['photo1'] ?>" id = "lbl1" width = "100%" height = "100%"/>
            </div>
            <input type = "file" required = "required" id = "photo1" name = "photo1" />
        </div>
    </div>

    <div class="form-group row">
        <label class="col-lg-4 col-form-label" for="val-digits">Main Gallery Preview 2: <span class="text-danger">*</span></label>
        <div class="col-lg-6">
            <label>Photo 2 </label>
            <div id = "preview2" style = "width:150px; height :150px; border:1px solid #000;">
                <img src = "../photo/<?php echo $fetch['photo2'] ?>" id = "lbl2" width = "100%" height = "100%"/>
            </div>
            <input type = "file" required = "required" id = "photo2" name = "photo2" />
        </div>
    </div>

    <div class="form-group row">
        <label class="col-lg-4 col-form-label" for="val-digits">Main Gallery Preview 3: <span class="text-danger">*</span></label>
        <div class="col-lg-6">
            <label>Photo 3 </label>
            <div id = "preview3" style = "width:150px; height :150px; border:1px solid #000;">
                <img src = "../photo/<?php echo $fetch['photo3'] ?>" id = "lbl3" width = "100%" height = "100%"/>
            </div>
            <input type = "file" required = "required" id = "photo3" name = "photo3" />
        </div>
    </div>
    
    <div class="form-group row">
        <label class="col-lg-4 col-form-label" for="val-digits">Main Gallery Preview 4: <span class="text-danger">*</span></label>
        <div class="col-lg-6">
            <label>Photo 4 </label>
            <div id = "preview4" style = "width:150px; height :150px; border:1px solid #000;">
                <img src = "../photo/<?php echo $fetch['photo4'] ?>" id = "lbl4" width = "100%" height = "100%"/>
            </div>
            <input type = "file" required = "required" id = "photo4" name = "photo4" />
        </div>
    </div>

    <div class="form-group row">
        <div class="col-lg-8 ml-auto">
            <button type="submit" class="btn btn-success" name="edit_maingallery">Update</button>
            <button class="btn btn-danger" name="cancel"><a href="view_maingallery.php">Cancel</a></button>
        </div>
    </div>
</form>

php

<form class="form-valide"  method="post" action="edit_query_maingallery.php?photo_id=<?php echo $fetch['photo_id'] ?>" enctype="multipart/form-data">
    <div class="form-group row">
        <label class="col-lg-4 col-form-label" for="val-digits">Photo Description For Gallery: <span class="text-danger">*</span></label>
        <div class="col-lg-6">
            <textarea name="photo_description" placeholder="Description" required="" rows="4" cols="50"><?php echo $fetch['photo_description'] ?></textarea>
        </div>
    </div>

    <div class="form-group row">
        <label class="col-lg-4 col-form-label" for="val-digits">Main Gallery Preview: <span class="text-danger"></span></label>
        <div class="col-lg-6">
            <label>Main Photo </label>
            <div id = "preview" style = "width:150px; height :150px; border:1px solid #000;">
                <img src = "../photo/<?php echo $fetch['photo'] ?>" id = "lbl" width = "100%" height = "100%"/>
            </div>
            <input type = "file" required = "required" id = "photo" name = "photo" />
        </div>
    </div>

    <div class="form-group row">
        <label class="col-lg-4 col-form-label" for="val-digits">Main Gallery Preview 1: <span class="text-danger">*</span></label>
        <div class="col-lg-6">
            <label>Photo 1 </label>
            <div id = "preview1" style = "width:150px; height :150px; border:1px solid #000;">
                <img src = "../photo/<?php echo $fetch['photo1'] ?>" id = "lbl1" width = "100%" height = "100%"/>
            </div>
            <input type = "file" required = "required" id = "photo1" name = "photo1" />
        </div>
    </div>

    <div class="form-group row">
        <label class="col-lg-4 col-form-label" for="val-digits">Main Gallery Preview 2: <span class="text-danger">*</span></label>
        <div class="col-lg-6">
            <label>Photo 2 </label>
            <div id = "preview2" style = "width:150px; height :150px; border:1px solid #000;">
                <img src = "../photo/<?php echo $fetch['photo2'] ?>" id = "lbl2" width = "100%" height = "100%"/>
            </div>
            <input type = "file" required = "required" id = "photo2" name = "photo2" />
        </div>
    </div>

    <div class="form-group row">
        <label class="col-lg-4 col-form-label" for="val-digits">Main Gallery Preview 3: <span class="text-danger">*</span></label>
        <div class="col-lg-6">
            <label>Photo 3 </label>
            <div id = "preview3" style = "width:150px; height :150px; border:1px solid #000;">
                <img src = "../photo/<?php echo $fetch['photo3'] ?>" id = "lbl3" width = "100%" height = "100%"/>
            </div>
            <input type = "file" required = "required" id = "photo3" name = "photo3" />
        </div>
    </div>
    
    <div class="form-group row">
        <label class="col-lg-4 col-form-label" for="val-digits">Main Gallery Preview 4: <span class="text-danger">*</span></label>
        <div class="col-lg-6">
            <label>Photo 4 </label>
            <div id = "preview4" style = "width:150px; height :150px; border:1px solid #000;">
                <img src = "../photo/<?php echo $fetch['photo4'] ?>" id = "lbl4" width = "100%" height = "100%"/>
            </div>
            <input type = "file" required = "required" id = "photo4" name = "photo4" />
        </div>
    </div>

    <div class="form-group row">
        <div class="col-lg-8 ml-auto">
            <button type="submit" class="btn btn-success" name="edit_maingallery">Update</button>
            <button class="btn btn-danger" name="cancel"><a href="view_maingallery.php">Cancel</a></button>
        </div>
    </div>
</form>

顺晟科技:

您的SQL当前正在用空白值覆盖现有图像。

在将每张照片添加到查询之前,需要更新查询以检查它们是否为空。


现有查询:

<form class="form-valide"  method="post" action="edit_query_maingallery.php?photo_id=<?php echo $fetch['photo_id'] ?>" enctype="multipart/form-data">
    <div class="form-group row">
        <label class="col-lg-4 col-form-label" for="val-digits">Photo Description For Gallery: <span class="text-danger">*</span></label>
        <div class="col-lg-6">
            <textarea name="photo_description" placeholder="Description" required="" rows="4" cols="50"><?php echo $fetch['photo_description'] ?></textarea>
        </div>
    </div>

    <div class="form-group row">
        <label class="col-lg-4 col-form-label" for="val-digits">Main Gallery Preview: <span class="text-danger"></span></label>
        <div class="col-lg-6">
            <label>Main Photo </label>
            <div id = "preview" style = "width:150px; height :150px; border:1px solid #000;">
                <img src = "../photo/<?php echo $fetch['photo'] ?>" id = "lbl" width = "100%" height = "100%"/>
            </div>
            <input type = "file" required = "required" id = "photo" name = "photo" />
        </div>
    </div>

    <div class="form-group row">
        <label class="col-lg-4 col-form-label" for="val-digits">Main Gallery Preview 1: <span class="text-danger">*</span></label>
        <div class="col-lg-6">
            <label>Photo 1 </label>
            <div id = "preview1" style = "width:150px; height :150px; border:1px solid #000;">
                <img src = "../photo/<?php echo $fetch['photo1'] ?>" id = "lbl1" width = "100%" height = "100%"/>
            </div>
            <input type = "file" required = "required" id = "photo1" name = "photo1" />
        </div>
    </div>

    <div class="form-group row">
        <label class="col-lg-4 col-form-label" for="val-digits">Main Gallery Preview 2: <span class="text-danger">*</span></label>
        <div class="col-lg-6">
            <label>Photo 2 </label>
            <div id = "preview2" style = "width:150px; height :150px; border:1px solid #000;">
                <img src = "../photo/<?php echo $fetch['photo2'] ?>" id = "lbl2" width = "100%" height = "100%"/>
            </div>
            <input type = "file" required = "required" id = "photo2" name = "photo2" />
        </div>
    </div>

    <div class="form-group row">
        <label class="col-lg-4 col-form-label" for="val-digits">Main Gallery Preview 3: <span class="text-danger">*</span></label>
        <div class="col-lg-6">
            <label>Photo 3 </label>
            <div id = "preview3" style = "width:150px; height :150px; border:1px solid #000;">
                <img src = "../photo/<?php echo $fetch['photo3'] ?>" id = "lbl3" width = "100%" height = "100%"/>
            </div>
            <input type = "file" required = "required" id = "photo3" name = "photo3" />
        </div>
    </div>
    
    <div class="form-group row">
        <label class="col-lg-4 col-form-label" for="val-digits">Main Gallery Preview 4: <span class="text-danger">*</span></label>
        <div class="col-lg-6">
            <label>Photo 4 </label>
            <div id = "preview4" style = "width:150px; height :150px; border:1px solid #000;">
                <img src = "../photo/<?php echo $fetch['photo4'] ?>" id = "lbl4" width = "100%" height = "100%"/>
            </div>
            <input type = "file" required = "required" id = "photo4" name = "photo4" />
        </div>
    </div>

    <div class="form-group row">
        <div class="col-lg-8 ml-auto">
            <button type="submit" class="btn btn-success" name="edit_maingallery">Update</button>
            <button class="btn btn-danger" name="cancel"><a href="view_maingallery.php">Cancel</a></button>
        </div>
    </div>
</form>

新查询:

<form class="form-valide"  method="post" action="edit_query_maingallery.php?photo_id=<?php echo $fetch['photo_id'] ?>" enctype="multipart/form-data">
    <div class="form-group row">
        <label class="col-lg-4 col-form-label" for="val-digits">Photo Description For Gallery: <span class="text-danger">*</span></label>
        <div class="col-lg-6">
            <textarea name="photo_description" placeholder="Description" required="" rows="4" cols="50"><?php echo $fetch['photo_description'] ?></textarea>
        </div>
    </div>

    <div class="form-group row">
        <label class="col-lg-4 col-form-label" for="val-digits">Main Gallery Preview: <span class="text-danger"></span></label>
        <div class="col-lg-6">
            <label>Main Photo </label>
            <div id = "preview" style = "width:150px; height :150px; border:1px solid #000;">
                <img src = "../photo/<?php echo $fetch['photo'] ?>" id = "lbl" width = "100%" height = "100%"/>
            </div>
            <input type = "file" required = "required" id = "photo" name = "photo" />
        </div>
    </div>

    <div class="form-group row">
        <label class="col-lg-4 col-form-label" for="val-digits">Main Gallery Preview 1: <span class="text-danger">*</span></label>
        <div class="col-lg-6">
            <label>Photo 1 </label>
            <div id = "preview1" style = "width:150px; height :150px; border:1px solid #000;">
                <img src = "../photo/<?php echo $fetch['photo1'] ?>" id = "lbl1" width = "100%" height = "100%"/>
            </div>
            <input type = "file" required = "required" id = "photo1" name = "photo1" />
        </div>
    </div>

    <div class="form-group row">
        <label class="col-lg-4 col-form-label" for="val-digits">Main Gallery Preview 2: <span class="text-danger">*</span></label>
        <div class="col-lg-6">
            <label>Photo 2 </label>
            <div id = "preview2" style = "width:150px; height :150px; border:1px solid #000;">
                <img src = "../photo/<?php echo $fetch['photo2'] ?>" id = "lbl2" width = "100%" height = "100%"/>
            </div>
            <input type = "file" required = "required" id = "photo2" name = "photo2" />
        </div>
    </div>

    <div class="form-group row">
        <label class="col-lg-4 col-form-label" for="val-digits">Main Gallery Preview 3: <span class="text-danger">*</span></label>
        <div class="col-lg-6">
            <label>Photo 3 </label>
            <div id = "preview3" style = "width:150px; height :150px; border:1px solid #000;">
                <img src = "../photo/<?php echo $fetch['photo3'] ?>" id = "lbl3" width = "100%" height = "100%"/>
            </div>
            <input type = "file" required = "required" id = "photo3" name = "photo3" />
        </div>
    </div>
    
    <div class="form-group row">
        <label class="col-lg-4 col-form-label" for="val-digits">Main Gallery Preview 4: <span class="text-danger">*</span></label>
        <div class="col-lg-6">
            <label>Photo 4 </label>
            <div id = "preview4" style = "width:150px; height :150px; border:1px solid #000;">
                <img src = "../photo/<?php echo $fetch['photo4'] ?>" id = "lbl4" width = "100%" height = "100%"/>
            </div>
            <input type = "file" required = "required" id = "photo4" name = "photo4" />
        </div>
    </div>

    <div class="form-group row">
        <div class="col-lg-8 ml-auto">
            <button type="submit" class="btn btn-success" name="edit_maingallery">Update</button>
            <button class="btn btn-danger" name="cancel"><a href="view_maingallery.php">Cancel</a></button>
        </div>
    </div>
</form>

您还调用变量“sq1”而不是“sql”,一个使用数字一,另一个使用小写L,为了便于使用,最好使用字母L。因此,除了复制上面的内容之外,您还需要在这里更改变量名:

<form class="form-valide"  method="post" action="edit_query_maingallery.php?photo_id=<?php echo $fetch['photo_id'] ?>" enctype="multipart/form-data">
    <div class="form-group row">
        <label class="col-lg-4 col-form-label" for="val-digits">Photo Description For Gallery: <span class="text-danger">*</span></label>
        <div class="col-lg-6">
            <textarea name="photo_description" placeholder="Description" required="" rows="4" cols="50"><?php echo $fetch['photo_description'] ?></textarea>
        </div>
    </div>

    <div class="form-group row">
        <label class="col-lg-4 col-form-label" for="val-digits">Main Gallery Preview: <span class="text-danger"></span></label>
        <div class="col-lg-6">
            <label>Main Photo </label>
            <div id = "preview" style = "width:150px; height :150px; border:1px solid #000;">
                <img src = "../photo/<?php echo $fetch['photo'] ?>" id = "lbl" width = "100%" height = "100%"/>
            </div>
            <input type = "file" required = "required" id = "photo" name = "photo" />
        </div>
    </div>

    <div class="form-group row">
        <label class="col-lg-4 col-form-label" for="val-digits">Main Gallery Preview 1: <span class="text-danger">*</span></label>
        <div class="col-lg-6">
            <label>Photo 1 </label>
            <div id = "preview1" style = "width:150px; height :150px; border:1px solid #000;">
                <img src = "../photo/<?php echo $fetch['photo1'] ?>" id = "lbl1" width = "100%" height = "100%"/>
            </div>
            <input type = "file" required = "required" id = "photo1" name = "photo1" />
        </div>
    </div>

    <div class="form-group row">
        <label class="col-lg-4 col-form-label" for="val-digits">Main Gallery Preview 2: <span class="text-danger">*</span></label>
        <div class="col-lg-6">
            <label>Photo 2 </label>
            <div id = "preview2" style = "width:150px; height :150px; border:1px solid #000;">
                <img src = "../photo/<?php echo $fetch['photo2'] ?>" id = "lbl2" width = "100%" height = "100%"/>
            </div>
            <input type = "file" required = "required" id = "photo2" name = "photo2" />
        </div>
    </div>

    <div class="form-group row">
        <label class="col-lg-4 col-form-label" for="val-digits">Main Gallery Preview 3: <span class="text-danger">*</span></label>
        <div class="col-lg-6">
            <label>Photo 3 </label>
            <div id = "preview3" style = "width:150px; height :150px; border:1px solid #000;">
                <img src = "../photo/<?php echo $fetch['photo3'] ?>" id = "lbl3" width = "100%" height = "100%"/>
            </div>
            <input type = "file" required = "required" id = "photo3" name = "photo3" />
        </div>
    </div>
    
    <div class="form-group row">
        <label class="col-lg-4 col-form-label" for="val-digits">Main Gallery Preview 4: <span class="text-danger">*</span></label>
        <div class="col-lg-6">
            <label>Photo 4 </label>
            <div id = "preview4" style = "width:150px; height :150px; border:1px solid #000;">
                <img src = "../photo/<?php echo $fetch['photo4'] ?>" id = "lbl4" width = "100%" height = "100%"/>
            </div>
            <input type = "file" required = "required" id = "photo4" name = "photo4" />
        </div>
    </div>

    <div class="form-group row">
        <div class="col-lg-8 ml-auto">
            <button type="submit" class="btn btn-success" name="edit_maingallery">Update</button>
            <button class="btn btn-danger" name="cancel"><a href="view_maingallery.php">Cancel</a></button>
        </div>
    </div>
</form>

到:

<form class="form-valide"  method="post" action="edit_query_maingallery.php?photo_id=<?php echo $fetch['photo_id'] ?>" enctype="multipart/form-data">
    <div class="form-group row">
        <label class="col-lg-4 col-form-label" for="val-digits">Photo Description For Gallery: <span class="text-danger">*</span></label>
        <div class="col-lg-6">
            <textarea name="photo_description" placeholder="Description" required="" rows="4" cols="50"><?php echo $fetch['photo_description'] ?></textarea>
        </div>
    </div>

    <div class="form-group row">
        <label class="col-lg-4 col-form-label" for="val-digits">Main Gallery Preview: <span class="text-danger"></span></label>
        <div class="col-lg-6">
            <label>Main Photo </label>
            <div id = "preview" style = "width:150px; height :150px; border:1px solid #000;">
                <img src = "../photo/<?php echo $fetch['photo'] ?>" id = "lbl" width = "100%" height = "100%"/>
            </div>
            <input type = "file" required = "required" id = "photo" name = "photo" />
        </div>
    </div>

    <div class="form-group row">
        <label class="col-lg-4 col-form-label" for="val-digits">Main Gallery Preview 1: <span class="text-danger">*</span></label>
        <div class="col-lg-6">
            <label>Photo 1 </label>
            <div id = "preview1" style = "width:150px; height :150px; border:1px solid #000;">
                <img src = "../photo/<?php echo $fetch['photo1'] ?>" id = "lbl1" width = "100%" height = "100%"/>
            </div>
            <input type = "file" required = "required" id = "photo1" name = "photo1" />
        </div>
    </div>

    <div class="form-group row">
        <label class="col-lg-4 col-form-label" for="val-digits">Main Gallery Preview 2: <span class="text-danger">*</span></label>
        <div class="col-lg-6">
            <label>Photo 2 </label>
            <div id = "preview2" style = "width:150px; height :150px; border:1px solid #000;">
                <img src = "../photo/<?php echo $fetch['photo2'] ?>" id = "lbl2" width = "100%" height = "100%"/>
            </div>
            <input type = "file" required = "required" id = "photo2" name = "photo2" />
        </div>
    </div>

    <div class="form-group row">
        <label class="col-lg-4 col-form-label" for="val-digits">Main Gallery Preview 3: <span class="text-danger">*</span></label>
        <div class="col-lg-6">
            <label>Photo 3 </label>
            <div id = "preview3" style = "width:150px; height :150px; border:1px solid #000;">
                <img src = "../photo/<?php echo $fetch['photo3'] ?>" id = "lbl3" width = "100%" height = "100%"/>
            </div>
            <input type = "file" required = "required" id = "photo3" name = "photo3" />
        </div>
    </div>
    
    <div class="form-group row">
        <label class="col-lg-4 col-form-label" for="val-digits">Main Gallery Preview 4: <span class="text-danger">*</span></label>
        <div class="col-lg-6">
            <label>Photo 4 </label>
            <div id = "preview4" style = "width:150px; height :150px; border:1px solid #000;">
                <img src = "../photo/<?php echo $fetch['photo4'] ?>" id = "lbl4" width = "100%" height = "100%"/>
            </div>
            <input type = "file" required = "required" id = "photo4" name = "photo4" />
        </div>
    </div>

    <div class="form-group row">
        <div class="col-lg-8 ml-auto">
            <button type="submit" class="btn btn-success" name="edit_maingallery">Update</button>
            <button class="btn btn-danger" name="cancel"><a href="view_maingallery.php">Cancel</a></button>
        </div>
    </div>
</form>

您应该重构代码。您的代码中有很多乱七八糟的地方。

首先,您应该删除HTML、JS、PHP文件中的重复代码。

用于HTML文件。请为带有图片的行创建模板:

<form class="form-valide"  method="post" action="edit_query_maingallery.php?photo_id=<?php echo $fetch['photo_id'] ?>" enctype="multipart/form-data">
    <div class="form-group row">
        <label class="col-lg-4 col-form-label" for="val-digits">Photo Description For Gallery: <span class="text-danger">*</span></label>
        <div class="col-lg-6">
            <textarea name="photo_description" placeholder="Description" required="" rows="4" cols="50"><?php echo $fetch['photo_description'] ?></textarea>
        </div>
    </div>

    <div class="form-group row">
        <label class="col-lg-4 col-form-label" for="val-digits">Main Gallery Preview: <span class="text-danger"></span></label>
        <div class="col-lg-6">
            <label>Main Photo </label>
            <div id = "preview" style = "width:150px; height :150px; border:1px solid #000;">
                <img src = "../photo/<?php echo $fetch['photo'] ?>" id = "lbl" width = "100%" height = "100%"/>
            </div>
            <input type = "file" required = "required" id = "photo" name = "photo" />
        </div>
    </div>

    <div class="form-group row">
        <label class="col-lg-4 col-form-label" for="val-digits">Main Gallery Preview 1: <span class="text-danger">*</span></label>
        <div class="col-lg-6">
            <label>Photo 1 </label>
            <div id = "preview1" style = "width:150px; height :150px; border:1px solid #000;">
                <img src = "../photo/<?php echo $fetch['photo1'] ?>" id = "lbl1" width = "100%" height = "100%"/>
            </div>
            <input type = "file" required = "required" id = "photo1" name = "photo1" />
        </div>
    </div>

    <div class="form-group row">
        <label class="col-lg-4 col-form-label" for="val-digits">Main Gallery Preview 2: <span class="text-danger">*</span></label>
        <div class="col-lg-6">
            <label>Photo 2 </label>
            <div id = "preview2" style = "width:150px; height :150px; border:1px solid #000;">
                <img src = "../photo/<?php echo $fetch['photo2'] ?>" id = "lbl2" width = "100%" height = "100%"/>
            </div>
            <input type = "file" required = "required" id = "photo2" name = "photo2" />
        </div>
    </div>

    <div class="form-group row">
        <label class="col-lg-4 col-form-label" for="val-digits">Main Gallery Preview 3: <span class="text-danger">*</span></label>
        <div class="col-lg-6">
            <label>Photo 3 </label>
            <div id = "preview3" style = "width:150px; height :150px; border:1px solid #000;">
                <img src = "../photo/<?php echo $fetch['photo3'] ?>" id = "lbl3" width = "100%" height = "100%"/>
            </div>
            <input type = "file" required = "required" id = "photo3" name = "photo3" />
        </div>
    </div>
    
    <div class="form-group row">
        <label class="col-lg-4 col-form-label" for="val-digits">Main Gallery Preview 4: <span class="text-danger">*</span></label>
        <div class="col-lg-6">
            <label>Photo 4 </label>
            <div id = "preview4" style = "width:150px; height :150px; border:1px solid #000;">
                <img src = "../photo/<?php echo $fetch['photo4'] ?>" id = "lbl4" width = "100%" height = "100%"/>
            </div>
            <input type = "file" required = "required" id = "photo4" name = "photo4" />
        </div>
    </div>

    <div class="form-group row">
        <div class="col-lg-8 ml-auto">
            <button type="submit" class="btn btn-success" name="edit_maingallery">Update</button>
            <button class="btn btn-danger" name="cancel"><a href="view_maingallery.php">Cancel</a></button>
        </div>
    </div>
</form>

用于JS文件。请创建一个函数与行进行交互。

<form class="form-valide"  method="post" action="edit_query_maingallery.php?photo_id=<?php echo $fetch['photo_id'] ?>" enctype="multipart/form-data">
    <div class="form-group row">
        <label class="col-lg-4 col-form-label" for="val-digits">Photo Description For Gallery: <span class="text-danger">*</span></label>
        <div class="col-lg-6">
            <textarea name="photo_description" placeholder="Description" required="" rows="4" cols="50"><?php echo $fetch['photo_description'] ?></textarea>
        </div>
    </div>

    <div class="form-group row">
        <label class="col-lg-4 col-form-label" for="val-digits">Main Gallery Preview: <span class="text-danger"></span></label>
        <div class="col-lg-6">
            <label>Main Photo </label>
            <div id = "preview" style = "width:150px; height :150px; border:1px solid #000;">
                <img src = "../photo/<?php echo $fetch['photo'] ?>" id = "lbl" width = "100%" height = "100%"/>
            </div>
            <input type = "file" required = "required" id = "photo" name = "photo" />
        </div>
    </div>

    <div class="form-group row">
        <label class="col-lg-4 col-form-label" for="val-digits">Main Gallery Preview 1: <span class="text-danger">*</span></label>
        <div class="col-lg-6">
            <label>Photo 1 </label>
            <div id = "preview1" style = "width:150px; height :150px; border:1px solid #000;">
                <img src = "../photo/<?php echo $fetch['photo1'] ?>" id = "lbl1" width = "100%" height = "100%"/>
            </div>
            <input type = "file" required = "required" id = "photo1" name = "photo1" />
        </div>
    </div>

    <div class="form-group row">
        <label class="col-lg-4 col-form-label" for="val-digits">Main Gallery Preview 2: <span class="text-danger">*</span></label>
        <div class="col-lg-6">
            <label>Photo 2 </label>
            <div id = "preview2" style = "width:150px; height :150px; border:1px solid #000;">
                <img src = "../photo/<?php echo $fetch['photo2'] ?>" id = "lbl2" width = "100%" height = "100%"/>
            </div>
            <input type = "file" required = "required" id = "photo2" name = "photo2" />
        </div>
    </div>

    <div class="form-group row">
        <label class="col-lg-4 col-form-label" for="val-digits">Main Gallery Preview 3: <span class="text-danger">*</span></label>
        <div class="col-lg-6">
            <label>Photo 3 </label>
            <div id = "preview3" style = "width:150px; height :150px; border:1px solid #000;">
                <img src = "../photo/<?php echo $fetch['photo3'] ?>" id = "lbl3" width = "100%" height = "100%"/>
            </div>
            <input type = "file" required = "required" id = "photo3" name = "photo3" />
        </div>
    </div>
    
    <div class="form-group row">
        <label class="col-lg-4 col-form-label" for="val-digits">Main Gallery Preview 4: <span class="text-danger">*</span></label>
        <div class="col-lg-6">
            <label>Photo 4 </label>
            <div id = "preview4" style = "width:150px; height :150px; border:1px solid #000;">
                <img src = "../photo/<?php echo $fetch['photo4'] ?>" id = "lbl4" width = "100%" height = "100%"/>
            </div>
            <input type = "file" required = "required" id = "photo4" name = "photo4" />
        </div>
    </div>

    <div class="form-group row">
        <div class="col-lg-8 ml-auto">
            <button type="submit" class="btn btn-success" name="edit_maingallery">Update</button>
            <button class="btn btn-danger" name="cancel"><a href="view_maingallery.php">Cancel</a></button>
        </div>
    </div>
</form>

只应传递一个参数。

用于PHP代码。你应该在你的画廊创建上传图像功能。并像。

那样调用该方法
<form class="form-valide"  method="post" action="edit_query_maingallery.php?photo_id=<?php echo $fetch['photo_id'] ?>" enctype="multipart/form-data">
    <div class="form-group row">
        <label class="col-lg-4 col-form-label" for="val-digits">Photo Description For Gallery: <span class="text-danger">*</span></label>
        <div class="col-lg-6">
            <textarea name="photo_description" placeholder="Description" required="" rows="4" cols="50"><?php echo $fetch['photo_description'] ?></textarea>
        </div>
    </div>

    <div class="form-group row">
        <label class="col-lg-4 col-form-label" for="val-digits">Main Gallery Preview: <span class="text-danger"></span></label>
        <div class="col-lg-6">
            <label>Main Photo </label>
            <div id = "preview" style = "width:150px; height :150px; border:1px solid #000;">
                <img src = "../photo/<?php echo $fetch['photo'] ?>" id = "lbl" width = "100%" height = "100%"/>
            </div>
            <input type = "file" required = "required" id = "photo" name = "photo" />
        </div>
    </div>

    <div class="form-group row">
        <label class="col-lg-4 col-form-label" for="val-digits">Main Gallery Preview 1: <span class="text-danger">*</span></label>
        <div class="col-lg-6">
            <label>Photo 1 </label>
            <div id = "preview1" style = "width:150px; height :150px; border:1px solid #000;">
                <img src = "../photo/<?php echo $fetch['photo1'] ?>" id = "lbl1" width = "100%" height = "100%"/>
            </div>
            <input type = "file" required = "required" id = "photo1" name = "photo1" />
        </div>
    </div>

    <div class="form-group row">
        <label class="col-lg-4 col-form-label" for="val-digits">Main Gallery Preview 2: <span class="text-danger">*</span></label>
        <div class="col-lg-6">
            <label>Photo 2 </label>
            <div id = "preview2" style = "width:150px; height :150px; border:1px solid #000;">
                <img src = "../photo/<?php echo $fetch['photo2'] ?>" id = "lbl2" width = "100%" height = "100%"/>
            </div>
            <input type = "file" required = "required" id = "photo2" name = "photo2" />
        </div>
    </div>

    <div class="form-group row">
        <label class="col-lg-4 col-form-label" for="val-digits">Main Gallery Preview 3: <span class="text-danger">*</span></label>
        <div class="col-lg-6">
            <label>Photo 3 </label>
            <div id = "preview3" style = "width:150px; height :150px; border:1px solid #000;">
                <img src = "../photo/<?php echo $fetch['photo3'] ?>" id = "lbl3" width = "100%" height = "100%"/>
            </div>
            <input type = "file" required = "required" id = "photo3" name = "photo3" />
        </div>
    </div>
    
    <div class="form-group row">
        <label class="col-lg-4 col-form-label" for="val-digits">Main Gallery Preview 4: <span class="text-danger">*</span></label>
        <div class="col-lg-6">
            <label>Photo 4 </label>
            <div id = "preview4" style = "width:150px; height :150px; border:1px solid #000;">
                <img src = "../photo/<?php echo $fetch['photo4'] ?>" id = "lbl4" width = "100%" height = "100%"/>
            </div>
            <input type = "file" required = "required" id = "photo4" name = "photo4" />
        </div>
    </div>

    <div class="form-group row">
        <div class="col-lg-8 ml-auto">
            <button type="submit" class="btn btn-success" name="edit_maingallery">Update</button>
            <button class="btn btn-danger" name="cancel"><a href="view_maingallery.php">Cancel</a></button>
        </div>
    </div>
</form>

这是个例子。如何重构代码!

ps。您应该检查array$_files中的哪个元素存在,并对array$_files中的那个元素应用该方法。

  • TAG:
相关文章
我们已经准备好了,你呢?
2024我们与您携手共赢,为您的企业形象保驾护航