我正在尝试将图像插入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
}
}
}
}
?>
答案 0 :(得分:0)
global $bdd;
删除,因为我们已经在全球范围内,我们不需要它们is_uploaded_file
检查以检查它是否实际上是上传的文件。$img..=$filename[..]
foreach($_FILES['uploadImage']['name'] as $key => $file
更改为foreach($_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
}
}
}
}
?>