gpt4 book ai didi

php - 循环/插入图像

转载 作者:行者123 更新时间:2023-11-29 17:11:40 25 4
gpt4 key购买 nike

我正在尝试将图像插入到 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
}
}

}

}

?>

最佳答案

  • 已删除 global $bdd;因为我们已经在全局范围内,所以我们不需要它们
  • 将准备语句移出循环,因为它们只需要准备一次。
  • 已添加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
}
}

}
}

?>

关于php - 循环/插入图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51862877/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com