我需要上传5张图片,我们已经制作了这些图片之前上传了许多图片,但是我需要为每张图片制作一个标签,以便如何做到这一点
看看我的代码。
这是HTML代码
<form action="<?php $_SERVER['PHP_SELF'] ?>" method="post" enctype="multipart/form-data">
<?php if(isset($_GET['msg'])){$msg=$_GET['msg']; echo $msg;} ?>
<span style="font-weight: bold; color: #F00;">*ملحوظة </span>الصورة الأولي التي سترفع ستكون صورة المشروع الأساسية نرجوا أختيارها بعناية
<p><label for="img1">صورة 1</label>
<input type="file" name="img[]" id="fileField1" />
<input type="text" name="Limage[]" id="Limage1" placeholder="Add label">
</p>
<p><label for="img2">صورة 2</label>
<input type="file" name="img[]" id="fileField2" />
<input type="text" name="Limage[]" id="Limage2" placeholder="Add label">
</p>
<p><label for="img3">صورة 3</label>
<input type="file" name="img[]" id="fileField3" />
<input type="text" name="Limage[]" id="Limage3" placeholder="Add label">
</p>
<p><label for="img4">صورة 4</label>
<input type="file" name="img[]" id="fileField4" />
<input type="text" name="Limage[]" id="Limage4" placeholder="Add label">
</p>
<p><label for="img5">صورة 5</label>
<input type="file" name="img[]" id="fileField5" />
<input type="text" name="Limage[]" id="Limage5" placeholder="Add label">
</p>
<p><input type="submit" name="submitCon" id="submitCon" value="Upload More Images">
<input type="submit" name="submit" id="submit" value="I am Finished"></p>
</form>
这是我上传图片的php
if(isset($_POST['submitCon'])){
$target = '../images/Projects/';
$num=1;
$projectID=$p;
foreach ($_FILES["img"]["error"] as $key => $error)
{
if ($error==UPLOAD_ERR_OK)
{
$tmp_name = $_FILES["img"]["tmp_name"][$key];
$name = $_FILES["img"]["name"][$key];
$label=$_POST['Limage'];
move_uploaded_file($tmp_name, "$target/$name");
$putData = "INSERT INTO projects_images (id, label, image, image_id)VALUE('', '$name', '$label', '$projectID')";
$result = $db->query($putData)or die($db->error);
if($result){
header('Location:index.php?cid=9&proid=8&p='.$p.'&msg=Images has been uploaded successfully, Need to upload more, after you finish uploading plans save and edit the project');
}else{
echo"Error";
}
}
}
}
?>
我编辑代码,现在我得到图像的名称而不是标签? 任何帮助,请
答案 0 :(得分:1)
您可以根据您在foreach中使用的密钥来确定标签。您已经在为剩余的文件信息执行此操作。
$label=$_POST['Limage'][$key];
此外,您的插入是向后的:
$putData = "INSERT INTO projects_images (id, label, image, image_id)VALUE('', '$name', '$label', '$projectID')";
应该是:
$putData = "INSERT INTO projects_images (id, label, image, image_id)VALUE('', '$label', '$name', '$projectID')";
只需确保在将数据放入数据库之前正确转义数据。