0

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.

  • 3
    Can you show a more complete example of the problem? Where does `$files` come from? What is the runtime value of the variable? – David Feb 11 '19 at 18:28
  • Possible duplicate of [How to 'insert if not exists' in MySQL?](https://stackoverflow.com/questions/1361340/how-to-insert-if-not-exists-in-mysql) – Dougie Feb 11 '19 at 18:31
  • $files = ""; if(isset($_POST['submit']) && isset($files)){ $files = $_FILES['files']; }else if(isset($_POST['submit']) && empty($files)){ $files = $_FILES['files']; – Christian Radtke Feb 11 '19 at 18:31
  • `if(isset($_POST['submit']) && !empty($_FILES['files']['tmp_name'])){ $files = $_FILES['files']; }elseif(isset($_POST['submit'])){ //form submit without select a picture ...} ` – Amin.MasterkinG Feb 11 '19 at 18:36
  • It does not work – Christian Radtke Feb 11 '19 at 18:47
  • "it does not work" is a unless statement. Is there a php error? Is there a MySQL error? Is there some behaviour that it didn't do? You can edit the question rather than putting comments, its easier to read. – danblack Feb 11 '19 at 20:21
  • If you do not select images, the old images will still be deleted. – Christian Radtke Feb 11 '19 at 21:04
  • How "select images" relates to the code here isn't clear. There is no code for "delete images" either. Your lack of verbose description will ultimately result in no answer. The first comment here said exactly the same thing. Last chance. – danblack Feb 11 '19 at 21:47
  • $files = ""; if(isset($_POST['submit']) && !empty($_FILES['files']['tmp_name'])){ $files = $_FILES['files']; else if(isset($_POST['submit']) && empty($_FILES['files'])){ insert only none image data – Christian Radtke Feb 12 '19 at 09:49
  • $ergebnis = "SELECT FILE_NAME FROM auktion_images WHERE ANGEBOT_ID = '{$change}'"; $resultat = $pdo->query($ergebnis); foreach ($resultat as $reihe): ?> prepare("delete from auktion_images where ANGEBOT_ID = '{$change}'"); $statement->execute(array()); endforeach; – Christian Radtke Feb 12 '19 at 09:50
  • I tryed this: $statement = $pdo->prepare("INSERT INTO `auktion_images` (ANGEBOT_ID, FILE_NAME, FILE_SIZE, FILE_TYPE) SELECT `ANGEBOT_ID`, `FILE_NAME`, `FILE_SIZE`,`FILE_TYPE` FROM `auktion_images` WHERE NOT EXISTS (SELECT * FROM `auktion_images` WHERE ANGEBOT_ID='$change')"); $result = $statement->execute(); But it doesn't work. What is wrong? – Christian Radtke Feb 12 '19 at 12:08

1 Answers1

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