我正在构建一个用户可以编辑信息的平台。这包括一张图片。它可以在更换图片时起作用,但如果用户没有添加新图片并希望保留旧图片,则图片会消失并在数据库中填入空白,而不是保留已存在的图片。
<?
if(isset($_POST['submit']))
{
# Define all the standard values that were created in the form
$picture = $_FILES['file']['name'];
$name = $_POST['name'];
$description = $_POST['description'];
$tags = $_POST['tags'];
$color = $_POST['color'];
$xs = $_POST['xs'];
$s = $_POST['s'];
$m = $_POST['m'];
$l = $_POST['l'];
$xl = $_POST['xl'];
$xxl = $_POST['xxl'];
$price = $_POST['price'];
$sale = $_POST['sale'];
$gender = $_POST['gender'];
# checking the sizes and adjusting the values accordingly
if ($xs=="on"){$xs="true";}else{$xs="false";};
if ($s=="on"){$s="true";}else{$s="false";};
if ($m=="on"){$m="true";}else{$m="false";};
if ($l=="on"){$l="true";}else{$l="false";};
if ($xl=="on"){$xl="true";}else{$xl="false";};
if ($xxl=="on"){$xxl="true";}else{$xxl="false";};
#defining the $size
$sizes = $xs."&".$s."&".$m."&".$l."&".$xl."&".$xxl;
#handling the picture that was uploaded
if(isset($_FILES['file']))
{
#Putting all the information into the database
move_uploaded_file($_FILES['file']['tmp_name'], '../upload/'.$picture);
$todo = "INSERT INTO PRODUCTS values('','$name','$picture','$description','$tags','$color','$sizes','$price','$sale','$gender')";
if (mysqli_query($con,$todo))
{
$notice = "The product has been added to the product list";
}
else
{
$notice = "The data could not be handled, please try again";
}
}
else
{
$notice = "The picture could not be handled, please try again";
};
}
?>
编辑:添加了整个代码
同样快速的旁注,我是一名学生,我并没有真正得到过于复杂的代码(但是)所以如果你带来一个20行的解决方案,我可能不会得到它,也不会从中学到东西&gt; &LT;
答案 0 :(得分:1)
您可以像这样检查文件上传:
if( !file_exists( $_FILES['file']['name'] ) || !is_uploaded_file( $_FILES['file']['name'] ) ) {
// Nothing uploaded ...
}