我正在尝试将图像插入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
}
}
}
}
?>
1条答案
按热度按时间6tdlim6h1#
远离的
global $bdd;
因为我们已经在全球范围内,我们不需要他们将prepare语句移出循环,因为它们只需要准备一次。
补充
is_uploaded_file
检查是否确实是上传的文件。远离的
$img..=$filename[..]
改变foreach($_FILES['uploadImage']['name'] as $key => $file
至foreach($_FILES['uploadImage'] as $file
为了更好更安全的版本