我正在尝试使用单个ID上传PHP中的多个图像。但是我在所有值中只有一个上传了id的图像。
具体来说,在下面的代码中,在数据库中上传了多个图像,但是我只获得了一个为整个图像更新的值,并且只存储了一个图像。在echo
语句中,它正确地显示了三个不同的图像,并且MySQL语句显示了3个单独的111.
$id = $_GET['c_id'];
if (isset($_GET['c_id'])){
$id = $_GET['c_id'];
if (isset($_POST['submit'])) {
extract($_POST);
$error = array();
$extension = array("jpeg", "jpg", "png", "gif");
foreach ($_FILES["files"]["tmp_name"] as $key => $tmp_name) {
$file_name = $_FILES["files"]["name"][$key];
$file_tmp = $_FILES["files"]["tmp_name"][$key];
// $ext=pathinfo($file_name,PATHINFO_EXTENSION);
$filename = basename($file_name);
$newFileName = $filename . '.' . $ext;
move_uploaded_file($file_tmp = $_FILES["files"]["tmp_name"][$key], "uploads/" . $newFileName);
echo $sql = "UPDATE
categories c
LEFT JOIN category_icons e ON e.category_id = c.id SET e.image='$newFileName' where c.id=$id";
$sql = mysql_query("UPDATE
categories c
LEFT JOIN category_icons e ON e.category_id = c.id SET e.image='$newFileName' where c.id=$id");
/* if($sql)
{
echo "<script>alert(' Category icons updated successfully'); window.location.href='category.php';</script>";
}*/
}
}
$qry = mysql_query("SELECT c.*, e.image,e.id
FROM `categories` c
LEFT JOIN category_icons e ON e.category_id = c.id WHERE c.id='$id'");
$cd_row = mysql_fetch_array($qry);
while ($cr_row = mysql_fetch_array($qry)){
?>
<div class="panel-body">
<form method="post" action="" enctype="multipart/form-data">
<div class="col-md-8">
<input id="logo" name="files[]" placeholder="logo" type="file">
<input type="hidden" name="imagehidden" class="form-control" value="<?php echo $cr_row['image']; ?>">
<img src="<?php echo $cr_row['image']; ?>" style="float:right;width:120px;height:100px;margin-top: 0px;" class="logo-default">
</div>
<a href="javascript:sureToApprove(<?php echo $cr_row['id']; ?>)" style="float:right;" class="btn btn-danger"><em class="fa fa-trash">del</em></a>
<?php }
} ?>
<div class="col-md-8">
<input type="submit" name="submit" class="btn btn-info">
<a href="add_category_icon.php?ci_id=<?php echo $id; ?>" class="btn btn-primary">add</a>
</div>
</div>
</form>
</div>
有什么问题?如果你能找到它或任何提示。谢谢你提前。
答案 0 :(得分:0)
您已将更新查询放在错误的位置。
在您的代码中,实际发生的是,在每个循环中,它将上传文件并更新相同类别ID的图像名称。因此,在每个循环中,它将被覆盖以前的值。
您需要做的是,您需要将每个文件的imageNames存储在变量中,并且在循环外部,您需要使用逗号分隔的字符串执行更新查询。
请参考以下代码:
if(isset($_POST['submit'])){
extract($_POST);
$error=array();
$extension=array("jpeg","jpg","png","gif");
$fileNames = array();
foreach($_FILES["files"]["tmp_name"] as $key=>$tmp_name)
{
$file_name=$_FILES["files"]["name"][$key];
$file_tmp=$_FILES["files"]["tmp_name"][$key];
// $ext=pathinfo($file_name,PATHINFO_EXTENSION);
$filename=basename($file_name);
$newFileName=$filename.'.'.$ext;
move_uploaded_file($file_tmp=$_FILES["files"]["tmp_name"][$key],"uploads/".$newFileName);
$fileNames[] = $filename;
}
$imageFileNames = implode(',',$fileNames);
$sql= mysql_query("UPDATE
categories c
LEFT JOIN category_icons e ON e.category_id = c.id SET e.image='$imageFileNames' where c.id=$id");
}
希望这会对你有所帮助。
答案 1 :(得分:0)
代码获取错误未定义变量:行$newFileName=$filename.'.'.$ext;
中的ext,因为$ext
定义了内部注释。
这是完整的代码
if(isset($_POST['submit'])){
extract($_POST);
$error='';
$extension=array("jpeg","jpg","png","gif");
$fileNames = array();
foreach($_FILES["files"]["tmp_name"] as $key=>$tmp_name)
{
$file_name=$_FILES["files"]["name"][$key];
$file_tmp=$_FILES["files"]["tmp_name"][$key];
$ext=pathinfo($file_name,PATHINFO_EXTENSION);
if(in_array($ext, $extension)){
$filename=basename($file_name);
$newFileName=$filename.'.'.$ext;
move_uploaded_file($file_tmp=$_FILES["files"]["tmp_name"][$key],"uploads/".$newFileName);
$fileNames[] = $filename;
}else{
$error ="File extension not support";
}
}
if($error==''){
$imageFileNames = implode(',',$fileNames);
$sql= mysql_query("UPDATE categories c LEFT JOIN category_icons e ON e.category_id = c.id SET e.image='$imageFileNames' where c.id=$id");
}
else{
echo $error;
}
}