简体   繁体   中英

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])){ ...}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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