在php中更新记录后,图像应保持不变
我正在上传一些包含标题、新闻日期和图片的数据到数据库,但当我想更新它时,我面临着问题。我想做的基本上是,如果我更新除图片以外的所有行,那么更新就不会发生。我想要的基本上是当我必须更新时,让我们假设只有标题,然后它应该更新它,但所有其他数据应该保持不变,但问题是,我必须再次选择图像从我的电脑在更新。我的场景是,我只是将名称保存在db中,而不是整个路径,并在需要显示图像的地方对路径进行硬编码 这里是html在php中更新记录后,图像应保持不变,php,mysql,Php,Mysql,我正在上传一些包含标题、新闻日期和图片的数据到数据库,但当我想更新它时,我面临着问题。我想做的基本上是,如果我更新除图片以外的所有行,那么更新就不会发生。我想要的基本上是当我必须更新时,让我们假设只有标题,然后它应该更新它,但所有其他数据应该保持不变,但问题是,我必须再次选择图像从我的电脑在更新。我的场景是,我只是将名称保存在db中,而不是整个路径,并在需要显示图像的地方对路径进行硬编码 这里是html <div class="row">
<div class="row">
<label>Image upload</label>
<div class="right"><input type="file" name="file" value="<?php echo $row['images'];?>" /></div>
</div>
图像上传
再次提交表单将导致文件输入的新值为空
因此,您必须检查它是否为空,并根据状态采取行动
比如说
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);
}
我认为您需要使用善意,以使您的代码在将来更具可读性和可维护性:)解决方案
检查文件是否不为空:if(!empty($\u FILES['fileToUpload']['name'])
在IF语句中使用表中的图像列进行更新查询,
如果未上载图像,则将上载图像
在ELSE语句中,在不提及表中图像列的情况下进行更新查询,若文件为空,它将上载
结论
下次更新任何其他字段时,图像不会消失,这意味着数据库中的文件路径不会为空,如果已经存在,它将不会更改,除非您手动更改它。介意分享一些片段,解释您正在做什么和如何做吗?是的,在更新imge字段中给我空值,但我希望它从数据库中获取插入时插入的图像,因为这一次我不想更改图像,而只想更改标题或内容,如果您需要的话如果不想更改图像,请从查询中删除图像字段立即检查。。这就是你想要的吗?事实上,我也想更改imgae,但问题是,例如,我在db中正确插入数据,但突然发现我输入了错误的标题,我想更新它。这次为什么我要再次上传图像?我只需更正标题并单击“更新”按钮,但这并没有发生,它迫使我上传图像以及。当我为udation选择特定的新闻时,所有从db获取并正确显示在其各自字段中的数据,但它无法从db获取图像名称,一旦它获取,它将立即解决我的所有问题,参考上面您编辑的代码,如果我也必须更新图像,我该怎么办
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);