简体   繁体   English

使用pdo更新映像时,文件夹名称成为数据库中的值

[英]Folder name becomes value in database when update image using pdo

I tried to update picture in database, but the value in database become like this : user_images/photo.jpg It should be photo.jpg only but why was it followed by folder name? 我试图更新数据库中的图片,但是数据库中的值变成了这样: user_images / photo.jpg仅应为photo.jpg ,但为什么后面跟文件夹名呢? It also made the photo can't be saved in folder. 这也使照片无法保存在文件夹中。

Here is the editform 这是editform

<?php 
    $id = $_GET['id'];
    $edit = $db_con->query("SELECT foto FROM mahasiswa WHERE nim='$id'");
    $row = $edit->fetch(PDO::FETCH_ASSOC);
?>
<div class="panel panel-default">
    <!-- Default panel contents -->
    <div class="panel-body">
        <form action="" method="POST" class="form-horizontal" enctype="multipart/form-data" role="form">
                <div class="form-group">
                    <center>
                      <legend>Upload Foto Profil  Mahasiswa </legend>
                    </center>
                </div>

                <div class="form-group">
                    <label for="input" class="col-sm-4 control-label">Pas Foto Mahasiswa :</label>
                  <div class="col-sm-6">
                      <p>
                        <input type="hidden" name="nim" value="<?php echo $id; ?>">
                        <input class="input-group" required="required" type="file" name="foto" accept="image/*" />
                    </p>
            </div>
                </div>


                <div class="form-group">
                    <div class="col-sm-6 col-sm-offset-4">
                        <button type="submit" name="edit_foto" class="btn btn-primary">Update</button>
                        <a href="?apps=ubahmhs" class="btn btn-warning">Batal</a>
                    </div>
                </div>
        </form>
    </div>
</div>

And this is the process 这是过程

<?php 

if (isset($_POST['edit_foto'])) {
    try{
        $path="user_images/" . basename($_FILES['foto']['name']);
        move_uploaded_file($_FILES['foto']['tmp_name'], $path);
        $edit = $db_con->prepare("UPDATE mahasiswa SET foto=:foto WHERE nim=:nim");
        $edit->bindParam(":nim", $_POST['nim']);
        $edit->bindParam(":foto", $path);
        $edit->execute();
        echo "<script>location.href='?apps=ubahmhs';</script>"; 
        exit();
    } 
        catch (PDOException $e) {
        echo $e->getMessage();
            }
    }
        else {'location=apps/app_ubahmhs/view.php'; }

?>

I need your helping so much. 我需要你的帮助 Thanks before for every responses 之前感谢您的每一个回应

You are using $path to save image name in database, which contains user_images along with the file name. 您正在使用$path将映像名称保存在数据库中,该数据库包含user_images以及文件名。 You should keep file name in separate variable like below: 您应该将文件名保留在单独的变量中,如下所示:

$imageName = basename($_FILES['foto']['name']);
$path="user_images/" . $imageName;

And then while binding parameters use: 然后在绑定参数时使用:

$edit->bindParam(":foto", $imageName);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM