循环/插入图像

I'm trying to insert images into mysql. The script currently insert one image.
I have put that in the form: name="uploadImage[]" (to make an array).
I understand something is wrong in my code.
I appreciate any help and thank you in advance! :)

<?php
if (isset($_POST['btnSubmit']))
{

$uploaded_images = array();
foreach($_FILES['uploadImage']['name'] as $key=>$val)
{
    $upload_dir = "uploads/";
    $upload_file = $upload_dir . $_FILES['uploadImage']['name'][$key];
    $filename = $_FILES['uploadImage']['name'][$key];


    if (move_uploaded_file($_FILES['uploadImage']['tmp_name'][$key], $upload_file))
    {
        $uploaded_images[] = $upload_file;
        $img0 = $filename[0];
        $img1 = $filename[1];
        $img2 = $filename[2];
        $img3 = $filename[3];
        $img4 = $filename[4];

        echo $filename."<br />";


        $created = date("Y:m:d h:i:s");

        global $bdd;
        $stmt= $bdd->prepare("INSERT INTO annonces_pro(id,ref_member,titre,intro,texte,activite,country,favorite,valid,is_ribbon,date_inserted) 
        VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
        $inserted = $stmt->execute(array('',$ref_member,$titre,$intro,$texte,$activite,$country,'','',$is_ribbon,$created));
        $lastId = $bdd->lastInsertId();

        global $bdd;
        $stmt2 = $bdd->prepare("INSERT INTO annonces_pro_images(id,ref_member,image,is_cover,weight_image,date_published)
         VALUES(?,?,?,?,?,?)");
        $inserted2 =$stmt2->execute(array($lastId,$ref_member,$filename,'','',$created));

        if ($inserted)
        {
            ?>
            <div class="alert alert-success" role="alert">
              ok<br />
                <a href="insert-annonce.php">Insert another ad</a><br />
                <a href="dashboard.php">Back to homepage</a><br />
            </div>
            <?php

        } else
        {
            ?>
            <div class="alert alert-danger" role="alert">Database error</div>
            <?php
        }
    }

  }

}

?>
  • removed global $bdd; as we are already in global scope, we don't need them
  • moved prepare statements out of the loop, as they only need to be prepared once.
  • added is_uploaded_file check to check if it's actually an uploaded file.
  • removed $img..=$filename[..]
  • changed foreach($_FILES['uploadImage']['name'] as $key => $file to foreach($_FILES['uploadImage'] as $file

for a better and secure version

<?php
if (isset($_POST['btnSubmit']))
{
    $stmt= $bdd->prepare("INSERT INTO annonces_pro(id,ref_member,titre,intro,texte,activite,country,favorite,valid,is_ribbon,date_inserted) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
    $stmt2 = $bdd->prepare("INSERT INTO annonces_pro_images(id,ref_member,image,is_cover,weight_image,date_published)
     VALUES(?,?,?,?,?,?)");
    $uploaded_images = array();

    foreach($_FILES['uploadImage'] as $image)
    {
        $filename = $image['name'];
        if (!is_uploaded_file($filename)) continue;
        $upload_dir = "uploads/";
        $upload_file = $upload_dir . $image['name'];



        if (move_uploaded_file($image['tmp_name'], $upload_file))
        {
            $uploaded_images[] = $upload_file;

            echo $filename."<br />";

            $created = date("Y:m:d h:i:s");

            $inserted = $stmt->execute(array('',$ref_member,$titre,$intro,$texte,$activite,$country,'','',$is_ribbon,$created));
            $lastId = $bdd->lastInsertId();

            $inserted2 =$stmt2->execute(array($lastId,$ref_member,$filename,'','',$created));

            if ($inserted)
            {
        ?>
        <div class="alert alert-success" role="alert">
          ok<br />
            <a href="insert-annonce.php">Insert another ad</a><br />
            <a href="dashboard.php">Back to homepage</a><br />
            </div>
            <?php

            } else
            {
?>
        <div class="alert alert-danger" role="alert">Database error</div>
<?php
            }
        }

    }
}

?>