循环/插入图像

nc1teljy  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(209)

我正在尝试将图像插入mysql。脚本当前插入一个图像。
我已经把它的形式:name=“uploadimage[]”(制作一个数组)。
我知道我的代码有问题。
感谢您的帮助,并提前向您表示感谢!:)

<?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
        }
    }

  }

}

?>
6tdlim6h

6tdlim6h1#

远离的 global $bdd; 因为我们已经在全球范围内,我们不需要他们
将prepare语句移出循环,因为它们只需要准备一次。
补充 is_uploaded_file 检查是否确实是上传的文件。
远离的 $img..=$filename[..] 改变 foreach($_FILES['uploadImage']['name'] as $key => $fileforeach($_FILES['uploadImage'] as $file 为了更好更安全的版本

<?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
            }
        }

    }
}

?>

相关问题