Jump to content

Image Upload Problems - HELP!


melissal

Recommended Posts

I've put the following code into a site...It allows users to add a product, and an image. The problem with it is that if they don't put an image in, nothing happens....It works great if they browse for an image, and click submit, but i need the image field to be optional. Can anyone help?


[code]<?php
    include('require.php');
    include('include/HTML_header.php');
?>

<table id="table_main" class="table_main">
    <tr>
        <td colspan="4">
            <?php include('include/header.php'); ?>
        </td>
    </tr>
    <tr>
        <td id="table_left" width="150px" align="left">
            <?php category_list_by_price(); ?><br />
            <?php category_list(); ?><br />
            <?php searchform(); ?>
        </td>
        <td width="20px">&nbsp;</td>
        <td id="table_content" width="560px" align="left">
        
<!-----------------------------------THE CONTENT GOES BELOW THIS LINE----------------------------------->
<?php
// This page allows users to upload files to the server.

$counter = 1; // Number of files to allow for.

if (isset($_POST['submitted'])) { // Handle the form.

    //require_once ('../mysql_connect.php'); // Connect to the database.
    
    for ($i = 0; $i < $counter; $i++) { // Handle each uploaded file.
    
        // Create index names to refer to the proper upload and description.
        $filename = 'product' . $i;
        //$description = 'description' . $i;
    
        // Check for a file.
        if (isset($_FILES[$filename]) && ($_FILES[$filename]['error'] != 4)) {

            // Check for a description (not required).
            /*if (!empty($_POST[$description])) {
                $d = "'" . escape_data($_POST[$description]) . "'";
            } else {
                $d = 'NULL';
            }*/
            
            // Add the record to the database.
            $query = "INSERT INTO products (product_file_name, product_file_size, product_file_type, product_ipaddress,product_date,product_user_id,product_title,product_description,product_category_id,product_price_id,product_price) VALUES ('{$_FILES[$filename]['name']}', {$_FILES[$filename]['size']}, '{$_FILES[$filename]['type']}', '".$_SERVER['REMOTE_ADDR']."', NOW(), '".$_SESSION['user_id']."','".$_POST['product_title']."','".$_POST['product_description']."','".$_POST['product_category_id']."','".$_POST['product_price_id']."','".$_POST['product_price']."')";
            $result = mysql_query ($query);
        
            if ($result) {
                
                // Return the product_id from the database.
                $product_id = mysql_insert_id();
                
                
                // Move the file over.
                if (move_uploaded_file($_FILES[$filename]['tmp_name'], "../uploads/$product_id-{$_FILES[$filename]['name']}")) {
                
                    echo '<p>File number ' . ($i + 1) . ' has been uploaded!</p>';
                    
                } else { // File could not be moved.
                
                    echo '<p><font color="red">File number ' . ($i + 1) . ' could not be moved.</font></p>';
        
                    // Remove the record from the database.
                    $query = "DELETE FROM products WHERE product_id = $product_id";
                    $result = mysql_query ($query);
                    
                    // Add more detailed error reporting, if desired.
                    
                }
                
            } else { // If the query did not run OK.
                echo '<p><font color="red">Your submission could not be processed due to a system error. We apologize for any inconvenience!</font></p>';
                // Print the query and invoke the mysql_error() function to debug.
            }
            
        } // End of if (isset($the_file)...
        
    } // End of FOR loop.
    
    mysql_close(); // Close the database connection.
        
} // End of the main Submit conditional.
?>

<div class="titletext">Add a Product</div><br />
<form enctype="multipart/form-data" action="product_add-working.php" method="post">
    <input type="hidden" name="MAX_FILE_SIZE" value="524288">
    
    <?php // Create the inputs.
    for ($i = 0; $i < $counter; $i++) {
        echo '
        
            <table>
                <tr>
                    <td width="120"><font color="#FF0033">*</font>Product Title/Name:</td>
                    <td width="200"><input type="text" name="product_title" size="30" maxlength="50" /></td>
                </tr>
                <tr>
                    <td width="120"><font color="#FF0033">*</font>Description:</td>
                    <td width="200"><textarea name="product_description" cols="23" rows="5"></textarea></td>
                </tr>
                <tr>
                    <td width="120"><font color="#FF0033">*</font>Category:</td>
                    <td width="200"><input type="text" name="product_category_id" size="30" maxlength="50" /></td>
                </tr>
                <tr>
                    <td width="120"><font color="#FF0033">*</font>Price:</td>
                    <td width="200"><input type="text" name="product_price_id" size="30" maxlength="50" /></td>
                </tr>
                <tr>
                    <td width="120">Price:</td>
                    <td width="200"><input type="text" name="product_price" size="30" maxlength="50" /></td>
                </tr>
                <tr>
                    <td width="120">Image:</td>
                    <td width="200"><input type="file" name="product' . $i . '" /></td>
                </tr>
    
        </table>
    
    ';
    }
    ?>
    
    <input type="hidden" name="submitted" value="TRUE" />
    <div align="center"><input type="submit" name="submit" value="Submit" /></div>

</form>



<!-----------------------------------THE CONTENT GOES ABOVE THIS LINE----------------------------------->
                </td>
        <td width="20px">&nbsp;</td>
    </tr>
</table>

<?php include('include/loginfooter2.php'); ?> [/code]
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.