0

I am looking for some help I am having a page where I input images but in that page the number of images that are inputted it's generated as a loop so if the user chooses 2 he will have to enter 2 images and if 3 -> 3 images . The problem here is when I try to upload those images on my database Mysql

the code:

<div id="chambres" class="chambres">
</div>

<script type="text/javascript">
//Here a js code that generat a script as a loop and it gives classes and inputes differents like : 
  <input name='nbr_max_chambre" + (i+1) + "' class='h_input' type='text'>
</script>

here is the code to move the image to my folder

for($i = 1; $i <= $nbr_chambre; $i++) {
    ${"nbr_max_chambre$i"} = $_POST["nbr_max_chambre$i"];
    ${"prix_cham$i"} = $_POST["prix_cham$i"];
    ${"desc_chambre$i"} = $_POST["desc_chambre$i"];
    // image chambre //
    // ${"img_chambre$i"+"_name"} = $_FILES["img_chambre$i"];
    // ${"img_chambre"+ $i +"_name"} = $_FILES["img_chambre$i"]["name"];
    if(isset($_FILES["img_chambre$i"])){
        $imgname = $_FILES["img_chambre$i"]["name"];
        // ${"img_chambre$i"+"size"} = $_FILES["img_chambre$i"]['size'];
        $imgsize = $_FILES["img_chambre$i"]['size'];
        // ${"img_chambre$i"+"_tmp_name"} = $_FILES["img_chambre$i"]['tmp_name'];
        $imgtmp = $_FILES["img_chambre$i"]['tmp_name'];
        // ${"img_chambre$i"+"error"} = $_FILES["img_chambre$i"]['error'];
        $imgerror = $_FILES["img_chambre$i"]['error'];
    }
    

    if ($imgerror === 0) {
        if ($imgsize > 1250000) {
            $em = "Sorry, your file is too large.";
            header("Location: Registration_locataire.php?error=$em");
        }else {
            $img_ex = pathinfo($imgname, PATHINFO_EXTENSION);
            $img_ex_lc = strtolower($img_ex);
    
            $allowed_exs = array("jpg", "jpeg", "png"); 
    
            if (in_array($img_ex_lc, $allowed_exs)) {
                ${"new_img_chambre$i"+"_name"} = uniqid("IMG-", true).'.'.$img_ex_lc;
                $img_upload_path = 'image/'.${"new_img_chambre$i"+"_name"};
                move_uploaded_file($imgtmp, $img_upload_path);
            }else {
                $em = "You can't upload files of this type";
                echo $em;
                // header("Location: add_house.php");
            }
        }
    }else {
        $em = "unknown error occurred!";
        echo $em;
        // header("Location: add_house.php");
    }

    ${"etage_selection$i"} = $_POST["etage_selection$i"];
}

and here is the code when I try to upload it in the database :

for($i = 1; $i <= $nbr_chambre; $i++) {
            ${"ID_CHAM$i"}  = rand(time(), 100000000);
            ${"requete_cham$i"} = $bd->prepare('INSERT INTO chambres values (?,?,?,?,?,?,?,?)');
            ${"requete_cham$i"}->bindValue(1,${"ID_CHAM$i"});
            ${"requete_cham$i"}->bindValue(2,$ID_MAISON);
            ${"requete_cham$i"}->bindValue(3,NULL);
            // ${"requete_cham$i"}->bindValue(4,${"nbr_max_chambre$i"});
            ${"requete_cham$i"}->bindValue(4,2);
            ${"requete_cham$i"}->bindValue(5,NULL);
            ${"requete_cham$i"}->bindValue(6,${"desc_chambre$i"});
            ${"requete_cham$i"}->bindValue(7,${"etage_selection$i"});
            // ${"requete_cham$i"}->bindValue(8,${"new_img_chambre$i"+"_name"});
            ${"requete_cham$i"}->bindValue(8,NULL);
            ${"requete_cham$i"}->execute();
        }

but the problem is the variable: ${"new_img_chambre$i"+"_name"} is not working the image is not uploaded even when I try to do echo to the name of the image echo ${"new_img_chambre$i"+"_name"} it's not working I wish if there is someone to help me and thank you so much.

ADyson
  • 57,178
  • 14
  • 51
  • 63
  • 1
    what does "not working" mean specifically? What exactly do you see when you try to echo that value? Also we can't see the form to see how you're adding the image inputs into your HTML, which makes it harder for us. Also what other debugging have you done? Have you checked the contents of $_FILES in detail? – ADyson Aug 10 '21 at 20:13
  • You can't concatenate strings with `+`. Also note that "variable variables" are almost never a good idea. Use an array instead, it will make your life much easier. – miken32 Aug 10 '21 at 20:58
  • Honestly having some trouble following what you're attempting, but I will point out that the whole idea of prepared statements is that they are prepared once and then executed multiple times. Don't prepare the same statement over and over again. – miken32 Aug 10 '21 at 21:00
  • thank you so much for your answers I did a modification to the question and I added the code that were missing – Ali Elmakhroubi Aug 11 '21 at 11:03
  • I don't see any changes to the question? – ADyson Aug 11 '21 at 11:08
  • Since you're talking about the file inputs, please show an example of a file input in the HTML... – ADyson Aug 11 '21 at 11:29
  • Telecharger Image – Ali Elmakhroubi Aug 11 '21 at 11:50
  • here is the code to input a file with html the "i" is changing inside a loop – Ali Elmakhroubi Aug 11 '21 at 12:03
  • And can you show the form this is all contained within? Also have you done what I asked earlier and checked the content of $_FILES when you run the upload? What is in there? – ADyson Aug 11 '21 at 12:09
  • The form is long it contains many things instead of this part I will upload it in GitHub then I will send the link and thank you so much – Ali Elmakhroubi Aug 11 '21 at 12:11
  • https://github.com/alielm01/formimages – Ali Elmakhroubi Aug 11 '21 at 12:16
  • Actually I just meant literally the `
    – ADyson Aug 11 '21 at 12:25
  • Anyway, once again I'm going to say - you need to do a `var_dump($_FILES);` to see what's actually in $_FILES, to ensure it matches the structure you're expecting, and that there are no errors. – ADyson Aug 11 '21 at 12:26
  • Ah yeah i did and I found a problem with it – Ali Elmakhroubi Aug 11 '21 at 13:03
  • array (size=13) 'img_cuisine' => array (size=5) 'name' => string 'app.jfif' (length=8) 'type' => string 'image/jpeg' (length=10) 'tmp_name' => string 'C:\wamp64\tmp\php5FDE.tmp' (length=25) 'error' => int 0 'size' => int 5633 'img_salle_bain' => array (size=5) 'name' => string '' (length=0) 'type' => string '' (length=0) 'tmp_name' => string '' (length=0) 'error' => int 4 'size' => int 0 – Ali Elmakhroubi Aug 11 '21 at 13:03
  • only the first img that works well – Ali Elmakhroubi Aug 11 '21 at 13:04
  • As an aside, in your add_house.php you've got jQuery included twice, different versions. That's not necessary and could even cause problems. Just stick with the highest version – ADyson Aug 11 '21 at 13:15
  • ah yeah thank you I corrected that one but I am still not getting the other images all that I am getting in $_FILE is the first image – Ali Elmakhroubi Aug 11 '21 at 13:41
  • I've looked all through the file and at the moment I honestly can't think why that would be happening. – ADyson Aug 11 '21 at 13:43
  • 1
    Thank you so much for your help I found the problem it was that all of those inputs of files they were having the same id when I changed it It worked thank you again – Ali Elmakhroubi Aug 11 '21 at 14:42

0 Answers0