更新图像
我有代码来创建/更新/从数据库中删除我的数据。
我有数据和横幅图像。如果仅设置数据,我不想更新图像。
所以,我尝试制作一个代码验证器:如果仅更新数据 - 不将图像设置为null。
但是我的验证无法检测到我是否填写了输入文件...
这是我的代码:
<?php
include ("config.php");
$id= $_POST['id'];
$konten3 = $_POST['konten3'];
$footer=$_POST['footer'];
$lokasi_file = $_FILES['banner']['tmp_name'];
$nama_file = $_FILES['banner']['name'];
if(isset($_FILES['banner'])) {
$hapus = mysql_query("SELECT * FROM newsletter WHERE id='$id'");
$r=mysql_fetch_array($hapus);
$d = 'upload/'.$r['banner'];
unlink ($d);
move_uploaded_file($lokasi_file,"upload/".$nama_file);
}if (empty($lokasi_file)) {
if ($edit = mysql_query("UPDATE newsletter SET konten3='$konten3', footer='$footer' WHERE id='$id'")){
header("Location: newsletter.php");
exit();
}
die ("Terdapat kesalahan : ". mysqli_error($konek));
}
if ($edit = mysql_query("UPDATE newsletter SET banner='$nama_file', konten3='$konten3',footer='$footer' WHERE id='$id'")){
header("Location: newsletter.php");
exit();
}
die ("Terdapat kesalahan : ". mysql_error($konek));
?>
如果有人能帮助我,我很高兴。我尝试使用名称横幅
问题在您的条件和最后查询中。我认为在上次更新中,您不需要更新图像。更改您的代码如下
<?php
include ("config.php");
$id= $_POST['id'];
$konten3 = $_POST['konten3'];
$footer=$_POST['footer'];
$lokasi_file = $_FILES['banner']['tmp_name'];
$nama_file = $_FILES['banner']['name'];
if(isset($_FILES['banner']) && $_FILES['banner']['name'] != "") {
$hapus = mysql_query("SELECT * FROM newsletter WHERE id='$id'");
$r=mysql_fetch_array($hapus);
$d = 'upload/'.$r['banner'];
unlink ($d);
move_uploaded_file($lokasi_file,"upload/".$nama_file);
}
if (!empty($lokasi_file)) { //change here need to check "not empty" instead of "empty"
if ($edit = mysql_query("UPDATE newsletter SET banner='$nama_file',konten3='$konten3', footer='$footer' WHERE id='$id'")){
header("Location: newsletter.php");
exit();
}
die ("Terdapat kesalahan : ". mysqli_error($konek));
}
//Remove banner from below query
if ($edit = mysql_query("UPDATE newsletter SET , konten3='$konten3',footer='$footer' WHERE id='$id'")){
header("Location: newsletter.php");
exit();
}
die ("Terdapat kesalahan : ". mysql_error($konek));
?>
编辑
如果上传横幅,请更改逻辑,然后移动并更新横幅,否则仅更新其他数据
<?php
include ("config.php");
$id= $_POST['id'];
$konten3 = $_POST['konten3'];
$footer=$_POST['footer'];
$lokasi_file = $_FILES['banner']['tmp_name'];
$nama_file = $_FILES['banner']['name'];
if(isset($_FILES['banner']) && $_FILES['banner']['name'] != "") {
$hapus = mysql_query("SELECT * FROM newsletter WHERE id='$id'");
$r=mysql_fetch_array($hapus);
$d = 'upload/'.$r['banner'];
unlink ($d);
move_uploaded_file($lokasi_file,"upload/".$nama_file);
if ($edit = mysql_query("UPDATE newsletter SET banner='$nama_file',konten3='$konten3', footer='$footer' WHERE id='$id'")){
header("Location: newsletter.php");
exit();
}
die ("Terdapat kesalahan : ". mysqli_error($konek));
}
//Remove banner from below query
if ($edit = mysql_query("UPDATE newsletter SET , konten3='$konten3',footer='$footer' WHERE id='$id'")){
header("Location: newsletter.php");
exit();
}
die ("Terdapat kesalahan : ". mysql_error($konek));
?>