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
https://forums.phpfreaks.com/topic/292764-isset_files-question/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.