我将一些数据上传到包含标题,新闻日期和图像的数据库,但是当我想要更新它时,我面临的问题。我基本上想做什么,如果我更新除了图像之外的所有行然后更新不发生。什么我想要什么基本上当我必须更新让我们假设标题只有它然后它应该更新它,但所有其他数据应保持相同,但问题是我必须从我的电脑更新再次选择图像。我的情况是我只是在db中保存名称而不是整个路径并硬编码我需要显示图像的路径
这里是html
<div class="row">
<label>Image upload</label>
<div class="right"><input type="file" name="file" value="<?php echo $row['images'];?>" /></div>
</div>
这是php
if(($_GET['mod']=='edit') && (isset($_POST['hidden'])))
{
echo $_FILES["file"]["name"];
$allowedExts = array("jpg", "jpeg", "gif", "png");
$extension = end(explode(".", $images));
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& in_array($extension, $allowedExts))
{
if ($_FILES["file"]["error"] > 0)
{
echo $_FILES["file"]["error"] . "<br>";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"], "upload-images/" . $images);
$update="UPDATE headline SET
headline_title = '$title',
headline_des = '$description',
month = '$month_name',
day = '$day_name',
year = '$year_name',
featured = '$featured',
headline = '$headline',
images = '$images'
where id = ".$_GET['id']."";
$result = mysql_query($update);
}
}
答案 0 :(得分:0)
再次提交表单将导致文件输入的新值为空 所以你必须检查它是否为空,并根据状态
进行操作例如
if($_FILES['file']['name'] != "")
{
//upload the image with what ever you want
//move_uploaded_file($_FILES['file']['tmp_name'],$target.$image );
//the SQL query should contain the updating the image column
if(($_GET['mod']=='edit') && (isset($_POST['hidden'])))
{
echo $_FILES["file"]["name"];
$allowedExts = array("jpg", "jpeg", "gif", "png");
$extension = end(explode(".", $images));
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& in_array($extension, $allowedExts))
{
if ($_FILES["file"]["error"] > 0)
{
echo $_FILES["file"]["error"] . "<br>";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"], "upload-images/" . $images);
$update="UPDATE headline SET
headline_title = '$title',
headline_des = '$description',
month = '$month_name',
day = '$day_name',
year = '$year_name',
featured = '$featured',
headline = '$headline',
images = '$images'
where id = ".$_GET['id']."";
$result = mysql_query($update);
}
}
}
else
{
//SQL update without image
$update="UPDATE headline SET
headline_title = '$title',
headline_des = '$description',
month = '$month_name',
day = '$day_name',
year = '$year_name',
featured = '$featured',
headline = '$headline'
WHERE id = ".$_GET['id']."";
$result = mysql_query($update);
}
如果您不想编辑图像,只需将其从更新和用于该图像的表单中删除,或者将其隐藏在输入中的路径
希望这会有所帮助
根据您的评论,查询应该看起来像这样
$update="UPDATE headline SET
headline_title = '$title',
headline_des = '$description',
month = '$month_name',
day = '$day_name',
year = '$year_name',
featured = '$featured',
headline = '$headline'
WHERE id = ".$_GET['id']."";
$result = mysql_query($update);
我认为您需要使用良好的意图来使您的代码在未来更具可读性和可维护性:)
答案 1 :(得分:0)
解决方案
if(!empty($_FILES['fileToUpload']['name']))
结论
下次更新任何其他字段时,图像不会消失,这意味着文件路径在数据库中不会为空,并且如果已经存在,则除非您手动更改它,否则它将不会更改。