Lucas Lucas - 2 years ago 80
PHP Question

PHP variable not being replaced with what's been collected

I am building a platform where a user can edit information. This includes a picture. It works when the picture is replaced but if the user does not add a new picture and wants to keep the old one, the picture disappears and it fills in blank in the database instead of keeping what was already there.

# 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
#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";
$notice = "The data could not be handled, please try again";
$notice = "The picture could not be handled, please try again";

EDIT: Added the whole code

Also quick sidenote, I am a student I don't really get overly complicated code (yet) so if you come with a 20 line solution I probably won't get it and don't learn a thing from it ><

Answer Source

You may check file uploads like this:

if( !file_exists( $_FILES['file']['name'] ) || !is_uploaded_file( $_FILES['file']['name'] ) ) {

    // Nothing uploaded ...

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download