我如何不从文件和数据库更新图像? [英] How would i not update a image from my files and database?

查看:61
本文介绍了我如何不从文件和数据库更新图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时我会使用包含要上传到数据库的文件的更新表单来遇到此问题。现在解释
:我有一个带有两个输入字段的提交表单。第一个输入是图像的名称,第二个输入是图像本身。表单已提交到upload.php,您可以看到代码

I run into this problem sometimes with the update form which contains files to upload to a database. now to explain :I have a submit form with two input Fields. The first input is the name of the image , and the second one is the image itself. The form is submitted to upload.php as you can see the code

upload.php

upload.php

    if(isset($_POST['submit'])){

    include 'connection.php';

    $imageName=$_POST['imageName'];
    $image=$_FILES['image'];

    $imageTmpName=$image['tmp_name'];
    $imageName=$image['name'];

    $imageExt=explode('.',$imageName);
    $imageExt=strtolower(end($imageExt));

    $newImageName=uniqid('',true).'.'.$imageExt;
    $path='C:/xampp/htdocs/test/img/'.$newImageName;

    if(move_uploaded_file($imageTmpName,$path)){
        echo 'file moved';

    }

    $query="INSERT INTO imagedata(imageName,image) VALUES ('$imageName','$newImageName')";
    if(mysqli_query($conn,$query)){
        echo 'data posted to database';
    }
}

您可以从代码中看到我正在移动图像临时名称到我的路径,同时我将新的图像名称存储到数据库中。
我使用相同的原理以一种新的形式更新图像名称和图像本身,并提交给update.php,如下所示:

You can see from the code that I am moving the image temporary name to my path and in the same time I'm and storing the new image name into the database. I am using the same principle update the image name and the image itself in a new form that is submit to update.php as show below:

    if(isset($_POST['update'])){

    include 'connection.php';

    $uImageName=$_POST['uimageName'];
    $uImage=$_FILES['uImage'];

    $uImageTmpName=$Image['tmp_name'];
    $uImageName=$uImage['name'];

    $uImageExt=explode('.',$uImageName);
    $uImageExt=strtolower(end($uImageExt));

    $uNewImageName=uniqid('',true).'.'.$uImageExt;
    $uPath='C:/xampp/htdocs/test/img/'.$uNewImageName;

    if(move_uploaded_file($uImageTmpName,$uPath)){
        echo 'file updatet';

    }

    $query="UPDATE imagedata SET imageName='$uImageName',image='$uNewImageName'";
    if(mysqli_query($conn,$query)){
        echo 'data updatet to database';
    }
}

请记住,我正在查询数据库并回显其中的值输入字段本身是值。现在问题出在update.php文件中,每当我更新图像名称而不上传任何新图像时,它将使用空白图像更新数据库并将其返回到我的更新表单。

Remember that I am querying database and echo the values inside the input field values itself.Now the problem occurs in the update.php file, whenever i update the image name without uploading any new image, it will update the database with a blank image and return that to my update form.

所以我想做的就是每当我要更新数据库而没有来自更新表单的图像时,我希望以前上传的图像保持不变。或者,如果我想将新图像上载到数据库,那么我想用新图像更新数据库,并删除我先前已上载的旧图像。
我要感谢您对此提供的帮助

So what i want to do is whenever i want to update the database without an image from my update form, i want the previous uploaded image to remain the same. Or if i want a new image to be uploaded to the database then i want the database to be updated with the new image and also to delete the old image that i have previous uploaded. I want to thank you for your help with this

推荐答案

如果未选择新图像,保留旧图像的简单方法!

Simple way to keep old image if new image not selected!

这是简单的方法,创建隐藏的输入并在值中回显旧的图像名称,并将新的图像值保留为空,如下所示:

Here is the easy way, create a hidden input and echo your old image name in value, and keep new image value empty as follows:

<input type="hidden" name="YourOldFile" value="YourOldFileName">
<input type="file" name="YourNewFile" value="">

然后在PHP部分执行if语句:

Then do if statement in PHP part:

if(!empty($POST['YourNewFile'])) {
   $newFile = $_POST['YourNewFile'];
   // Then do your update query with uploading new image here and delete old image from the server.
} else {
   $YourOldFileName = $_POST['YourOldFile'];
   //Then do your query here with oldname and do not delete image from server.
}

注意:您可以对插入和更新执行相同的操作,并且需要设置

Note: you can do same for insert and update, and you need to set where clause for update query.

UPDATE:

显示带或不带图像扩展是一个不同的问题,因为它是您的上传代码的一部分,以及如何保存和显示。
因此,您在页面中的代码如下所示:

Displaying with or without image extention is a different question because its a part of your upload code, and how you save and display. So, your codes in page looks like this :

include 'connection.php';

if(!empty($_FILES['uImageName'])) {
   // Then do your update query with uploading new image here and delete old image from the server.
    if(isset($_POST['update'])){

        $uImageName=$_FILES['uimageName'];
//Your upload code doesnt look right coded to me, I would use an upload class, You can search for how to upload image
// I use verot upload class its very simple.

        $uImageTmpName=$Image['tmp_name'];
        $uImageName=$uImage['name'];

        $uImageExt=explode('.',$uImageName);
        $uImageExt=strtolower(end($uImageExt));

        $uNewImageName=uniqid('',true).'.'.$uImageExt;
        $uPath='C:/xampp/htdocs/test/img/'.$uNewImageName;

        if(move_uploaded_file($uImageTmpName,$uPath)){
            echo 'file updated';

        }

        $query="UPDATE imagedata SET imageName='$uImageName'";
        if(mysqli_query($conn,$query)){
            echo 'data updatet to database';
        }
    }
} else {
   $YourOldFileName = $_POST['YourOldFile'];

   //Then do your query here with oldname and do not delete image from server.

    //Solution 1. You can update image column with your old image name.

    $query="UPDATE imagedata SET image='$YourOldFileName'";

    //Solution 2. You can update all other columns without image column dont update image column and dont include image column in your query.

    $query="UPDATE imagedata SET yourColumnName='$DataYouWantToInsert'";
    if(mysqli_query($conn,$query)){
        echo 'data updatet to database';
    }
}

<form>
/*Your inputs here*/
<input type="hidden" name="YourOldFile" value="YourOldFileName">
<input type="file" name="YourNewFile" value="">
/*Your buttons*/
</form>

阅读代码中的注释。

这篇关于我如何不从文件和数据库更新图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆