简体   繁体   English

选择新图像后如何保存到数据库。 如果没有,将旧图片保留在数据库中?

[英]How can I save to database when new image is selected. If not, leave old pictures in the database?

If I try with my code to save new pictures, or to leave old pictures in the database, it will not work. 如果我尝试使用代码保存新图片或将旧图片保留在数据库中,则将无法使用。

I have rewritten the code several times but can not get it. 我已经重写了几次代码,但无法获取它。

This is my Code. 这是我的代码。 I hope someone can help me. 我希望有一个人可以帮助我。

    $files = "";


if(isset($_POST['submit']) && !empty($_FILES['files']['tmp_name'])){


    $files = $_FILES['files'];


    $ergebnis = "SELECT FILE_NAME FROM auktion_images WHERE ANGEBOT_ID = '{$change}'";
    $resultat = $pdo->query($ergebnis);
foreach ($resultat as $reihe): ?>
            <?php unlink('image_uploads/'.$reihe["FILE_NAME"]); 
            $statement = $pdo->prepare("delete from auktion_images where ANGEBOT_ID = '{$change}'");
            $statement->execute(array());
            endforeach;

    $query = "INSERT into auktion_images(`ANGEBOT_ID`, `FILE_NAME`, `FILE_SIZE`,`FILE_TYPE`)
             VALUES(:ANGEBOT_ID, :FILE_NAME, :FILE_SIZE, :FILE_TYPE)";
    $stmt  = $pdo->prepare($query);

    foreach($_FILES['files']['tmp_name'] as $key => $error ){
        if ($error != UPLOAD_ERR_OK) {
            $errors[] = $_FILES['files']['name'][$key] . ' wurde nicht hochgeladen.';
            continue;
        }

        $file_name = $key.$_FILES['files']['name'][$key];
        $file_size = $_FILES['files']['size'][$key];
        $file_tmp  = $_FILES['files']['tmp_name'][$key];
        $file_type = $_FILES['files']['type'][$key];  
        if($file_size > 2097152){
            $errors[] = 'Bilder müssen kleiner als 2 MB sein.';
            continue;
        }
        try{
            $stmt->bindParam( ':ANGEBOT_ID', $change, PDO::PARAM_STR );
            $stmt->bindParam( ':FILE_NAME', $file_name , PDO::PARAM_STR );
            $stmt->bindParam( ':FILE_SIZE', $file_size, PDO::PARAM_STR );
            $stmt->bindParam( ':FILE_TYPE', $file_type, PDO::PARAM_STR );
            $stmt->execute();

            $desired_dir="image_uploads";

            if(is_dir($desired_dir)==false){
                mkdir($desired_dir, 0700);// Create directory if it does not exist
            }
            if(is_file($desired_dir.'/'.$file_name)==false){
                move_uploaded_file($file_tmp,$desired_dir.'/'.$file_name);
            }else{    //rename the file if another one exist
                $new_file=$desired_dir.'/'.$file_name.time();
                move_uploaded_file($file_tmp,$new_file) ;               
            }
        }catch(PDOException $e){
            $errors[] = $file_name . 'nicht in Datenbank gespeichert.';
            echo $e->getMessage();
        }   
    }}
    elseif(isset($_POST['submit']) && !isset($files)){

The best thing that has happened so far is that I can save new pictures. 到目前为止最好的事情是我可以保存新照片。

if(isset($_POST['submit']) && !empty($_FILES['files']['tmp_name'][0])){ ... }
if(isset($_POST['submit']) && empty($_FILES['files']['tmp_name'][0])){ ...}

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

相关问题 如何根据选择的内容在我的数据库中添加信息。? - How I add the information in my database depending on what is selected.? 未选择MySQL数据库。 我在哪里想念它? - MySQL database not selected. where did I miss it? 没有选择数据库。 课程预订输入失败:未选择数据库 - No database selected. lesson bookings' unsuccessful entry : No database selected 当我上载docx文件时,内容存储在数据库中如何保存图像并在数据库中插入图像名称? - when i have upload docx file, content are storing in database How can i save image and insert image name in database? 如何用旧映像而不选择新映像来更新数据库? - How to update database with old image and without choosing new image? 如何将图像和文本从tinymce编辑器保存到数据库中? - How can I save image and text from tinymce editor into database? 如何将动态生成的图像保存到数据库 - How can I save a dynamically generated image to database 如何将我的图像作为base 64保存到数据库中 - how can i save my image as base 64 into database 如何将动态生成的图像保存到数据库? - How can I save a dynamically generated image to my database? 我无法使用php从sql数据库显示图像。 如何显示它,如何一次显示更多图片? - I can' display an image from sql database with php. How is it possible to display it and how can I display more pictures at a time?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM