Jump to content

isset($_FILES) question


user38271

Recommended Posts

I need to check whether the user has uploaded a file when submitting a form using isset. If they have, the file path is recorded to the database. If they haven't nothing needs to occur, the file path field will just be NULL.
Here is my issue:
When I test it with uploading no file I'm still getting "images/" from my $ filelocation variable recorded to the database.
How can I fix this?

<?php 

    
if(isset($_FILES['userfile'])) 
    { 
    
$fileupload $_FILES['userfile']['name']; 
    
$filetype $_FILES['userfile']['type']; 
    
$filesize $_FILES['userfile']['size']; 
    
$tempname $_FILES['userfile']['tmp_name']; 
    
$filelocation "images/$fileupload"
    } 
    else 
    { 
    
$filelocation NULL
    } 


        if (!
move_uploaded_file($tempname,$filelocation)) { 
                switch (
$_FILES['userfile']['error']) 
                { 
                 
                    case 
UPLOAD_ERR_INI_SIZE
                        echo 
"<p>Error: File exceeds the maximum size limit set by the server</p>" 
                    break; 

                    case 
UPLOAD_ERR_FORM_SIZE
                        echo 
"<p>Error: File exceeds the maximum size limit set by the browser</p>" 
                    break; 

                    default: 
                        echo 
"<p>File could not be uploaded </p>" 
                } 
        } 
         
         
            if (
$_POST["productName"] == "") { 
                
header("Location:getProductDetails.php"); 
                exit(); 
            } 
                elseif (
$_POST["productDescription"] == "") { 
                
header("Location:getProductDetails.php");     
                exit(); 
                } 
                    else { 
                     
                    
$conn = @mysqli_connect("localhost""root"""); 
     
                    if (!
$conn)  
                    { 
                        echo 
"The connection has failed: " mysqli_error($conn); 
                    } 
                    else  
                    { 
                        
//echo "Successfully connected to mySQL!"; 
         
                    
$query "CREATE DATABASE IF NOT EXISTS bazaar"
                    
$dbName =""

                    if (!
mysqli_query($conn,$query))  
                    { 
                        echo 
"<p>Could not open the database: " mysqli_error($conn)."</p>"
                    } 
                    else 
                    { 
                        
//echo "<p>Database successfully created</p>"; 
     
                    
if (!mysqli_select_db($conn,"bazaar"))  
                    { 
                        echo 
"<p>Could not open the database: " mysqli_error($conn)."</p>"
                    } 
                    else  
                    { 
                        
//echo "<p>Database selection successful</p>"; 

                    
$query "CREATE TABLE IF NOT EXISTS products(productid int primary key not null auto_increment,productname varchar(50), productdesc text, colour varchar(25), price decimal(5,2) not null,imagepath varchar(250));"
                 
                    if (!
mysqli_query($conn,$query))  
                    { 
                        echo 
"table query failed1: " mysqli_error($conn); 
                    } 
                    else  
                    { 
                        
//echo "<p>table query successful</p>"; 

                    
$insert "INSERT INTO products (productname, productdesc, colour, price, imagepath) VALUES ('$_POST[productName]','$_POST[productDescription]','$_POST[colour]','$_POST[price]','$filelocation');"
                    if (
mysqli_query($conn,$insert))  
                    { 
                        
$customerId mysqli_insert_id($conn); 
                    } 
                    else  
                    { 
                        echo 
"table query failed: " mysqli_error($conn); 
                    } 
                } 
            } 
        } 
    } 


mysqli_close($conn); 

                    } 
     

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.