Jump to content

Image info wont go into database


melissal

Recommended Posts

Can someone help me with this? Everything else goes into the database fine, except for:

product_image (the filename)
product_size (the size of the image)
product_type (the type of image)


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

// Check to see if the user is signed in
if (isset($_SESSION['artist_id'])) {

  $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+1);
        //$description = 'description' . $i;
     
        // Check for a file.
        if (isset($_FILES[$filename]) && ($_FILES[$filename]['error'] != 4)) {
           
            // Add the record to the database.
            $query = "INSERT INTO products (product_image, product_imagesize, product_imagetype, product_ipaddress, product_date, product_artist_id, product_title, product_shortdescription, product_description, product_price, product_size, product_approved, product_featured) VALUES ('{$_FILES[$filename]['name']}', {$_FILES[$filename]['size']}, '{$_FILES[$filename]['type']}', '".$_SERVER['REMOTE_ADDR']."', NOW(), '".$_SESSION['artist_id']."','".$_POST['product_title']."','".$_POST['product_shortdescription']."','".$_POST['product_description']."','".$_POST['product_price']."','".$_POST['product_size']."','n','n')";
            $result = mysql_query ($query) or die('failed to add product to database' . mysql_error());
       
            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.
            }
           
        } else {
            // no file was uploaded
            $query = "INSERT INTO products (product_image, product_imagesize, product_imagetype, product_ipaddress, product_date, product_artist_id, product_title, product_shortdescription, product_description, product_price, product_size, product_approved, product_featured) VALUES ('', 0, '', '".$_SERVER['REMOTE_ADDR']."', NOW(), '".$_SESSION['artist_id']."','".$_POST['product_title']."','".$_POST['product_shortdescription']."','".$_POST['product_description']."','".$_POST['product_price']."','".$_POST['product_size']."','n','n')";
           
            $result = mysql_query ($query);
       
            if (!$result) {
              echo '<p><font color="red">Your submission could not be processed due to a system error. We apologize for any inconvenience!</font></p>';
            } else {
              echo '<p>Product has been successfully uploaded!</p>';
            }
        }// 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.php" method="post">
      <input type="hidden" name="MAX_FILE_SIZE" value="524288">
     
      <?php // Create the inputs.
      for ($i = 0; $i < $counter; $i++) {
        ?>
       
            <table>
              <tr>
                  <td width="120"><font color="#FF0033">*</font>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>Short Description:</td>
                  <td width="200"><textarea name="product_shortdescription" cols="23" rows="2"></textarea></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>Price:</td>
                  <td width="200"><input type="text" name="product_price" size="30" maxlength="10" /></td>
              </tr>
              <tr>
                  <td width="120"><font color="#FF0033">*</font>Size:</td>
                  <td width="200"><input type="text" name="product_size" size="30" maxlength="20" /></td>
              </tr>
              <tr>
                  <td width="120">Image:</td>
                  <td width="200"><input type="file" name="product1" /></td>
              </tr>
     
        </table>
      <?php
     
      }
      ?>
     
      <input type="hidden" name="submitted" value="TRUE" />
      <div align="center"><input type="submit" name="submit" value="Submit" /></div>
 
  </form>
<?php
}else{
  echo "<a href='login.php'>Please Login</a>";
}
?>[/code]
Link to comment
https://forums.phpfreaks.com/topic/32653-image-info-wont-go-into-database/
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.