Jump to content

Archived

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

melissal

Uploading images

Recommended Posts

I cant seem to get this upload form working properly...It comes up w/errors....Can anyone help?




[code]
<?php
// This page allows users to upload files to the server.


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

    require_once ('../mysql_connect.php'); // Connect to the database.
    
    // Create index names to refer to the proper upload and description.
    $filename = 'product' . $i;
    //$description = 'description' . $i;
    
    /*// Add the record to the database.
    $query = "INSERT INTO products (product_user_id,product_category_id,product_price_id,product_title,product_description,product_date,product_price,product_ipaddress,product_file_name, product_file_size, product_file_type) VALUES ('".$_SESSION['user_id']."','".$_POST['product_category_id']."','".$_POST['product_price_id']."','".$_POST['product_title']."','".$_POST['product_description']."',NOW(),'".$_POST['product_price']."','".$_SERVER['REMOTE_ADDR']."','{$_FILES[$filename]['name']}','{$_FILES[$filename]['size']}','{$_FILES[$filename]['type']}')";
            $result = mysql_query ($query);
            if ($result) {
                // Return the upload_id from the database.
                //$product_id = mysql_insert_id();
                // Move the file over.
                if (move_uploaded_file($_FILES[$filename]['tmp_name'], "../uploads/$product_id")) {
                    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);
                }
                
            } 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.
            }*/
    
    
    
    
    // Add the record to the database.
            $query = "INSERT INTO products (product_file_name, product_file_size, product_file_type) VALUES ('{$_FILES[$filename]['name']}', {$_FILES[$filename]['size']}, '{$_FILES[$filename]['type']}')";
            $result = mysql_query ($query);
        
            if ($result) {
                
                // Return the upload_id from the database.
                $product_id = mysql_insert_id();
                
                // Move the file over.
                if (move_uploaded_file($_FILES[$filename]['tmp_name'], "../uploads/$product_id")) {
                
                    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.
        

?>
<form enctype="multipart/form-data" action="product_add3.php" method="post">
<div class="titletext">Add a Product</div><br />
<input type="hidden" name="MAX_FILE_SIZE" value="524288">
    <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" value="<?php if (isset($_POST['product_title'])) echo $_POST['product_title']; ?>" /></td>
        </tr>
        <tr>
            <td width="120"><font color="#FF0033">*</font>Description:</td>
            <td width="200"><textarea name="product_description" cols="23" rows="5"><?php if (isset($_POST['product_description'])) echo $_POST['product_description']; ?></textarea></td>
        </tr>
        <tr>
            <td width="120"><font color="#FF0033">*</font>Category:</td>
            <td><select name="product_category_id">
            <?php
                $query="select category_id,category_name from category where category_sub_id=0 and category_active='a'    order by category_name";
                $result=mysql_query($query,$connect);
                    for($i=0;$i<mysql_num_rows($result);$i++){
                        list($category_id,$category_name)=mysql_fetch_row($result);
                        echo "<option value='$category_id'>$category_name</option>";
                    }            
            ?></select></td>
        </tr>
        <tr>
            <td width="120"><font color="#FF0033">*</font>Price Category:</td>
            <td><select name="product_price_id">
            <?php
                $query="select price_id,price_name from price where price_active='a' order by price_id";
                $result=mysql_query($query,$connect);
                    for($i=0;$i<mysql_num_rows($result);$i++){
                        list($price_id,$price_name)=mysql_fetch_row($result);
                        echo "<option value='$price_id'>$price_name</option>";
                    }            
            ?></select></td>
        </tr>
        <tr>
            <td width="120">Price:</td>
            <td width="200"><input type="text" name="product_price" size="10" maxlength="10" value="<?php if (isset($_POST['product_price'])) echo $_POST['product_price']; ?>" /></td>
        </tr>                
        <tr>
            <td>Image:</td>
            <td><input type="file" name="product' . $i . '" /></td>
        </tr>
        <tr>
            <td><input type="submit" name="submit" value="Submit" /></td>
            <td>(<font color="#FF0033">*</font> = required field)</td>
        </tr>
    </table>
    <input type="hidden" name="submitted" value="TRUE" />
</form>[/code]

Share this post


Link to post
Share on other sites
What errors does it come up with? On What line(s)?

Share this post


Link to post
Share on other sites

×

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.