Jump to content

Image Upload Problem


craigerjs

Recommended Posts

Hi, I am having a problem with an image upload script.  When I enter a user name, choose an image and press 'Add' I get my error message "Please enter all of the information to add your image."  Nothing gets written to the database and the image is not uploaded to the folder I've specified.  I am new to PHP and MySQL so if I have left out any info please let me know.  Here is my code and SQL table:

 

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>SKETCHWEEK.COM - Submit an image!</title>
  <link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
  <h2>SKETCHWEEK.COM - Submit an image!</h2>

<?php
  require_once('appvars.php');
  require_once('connectvars.php');

  if (isset($_POST['submit'])) {
    // Grab the id data from the POST
    $user_name = $_POST['user_name'];
    $image_name = $_FILES['image_name']['user_name'];
    $image_name_type = $_FILES['image_name']['type'];
    $image_name_size = $_FILES['image_name']['size']; 

    if (!empty($user_name) && !empty($image_name)) {
      if ((($image_name_type == 'image/gif') || ($image_name_type == 'image/jpeg') || ($image_name_type == 'image/pjpeg') || ($image_name_type == 'image/png'))
        && ($image_name_size > 0) && ($image_name_size <= GW_MAXFILESIZE)) {
        if ($_FILES['image_name']['error'] == 0) {
          // Move the file to the target upload folder
          $target = GW_UPLOADPATH . $image_name;
          if (move_uploaded_file($_FILES['image_name']['tmp_name'], $target)) {
            // Connect to the database
     $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
      or die('Error connecting to MySQL server.');

            // Write the data to the database
            $query = "INSERT INTO user_images VALUES (0, '$user_name', NOW(), '$image_name')";
            mysqli_query($dbc, $query);

            // Confirm success with the user
            echo '<p>Thanks for submitting an image!  Under review as soon as possible.</p>';
            echo '<p><strong>User Name:</strong> ' . $user_name . '<br />';
            echo '<img src="' . GW_UPLOADPATH . $image_name . '" alt="Submitted Image" /></p>';
            echo '<p><a href="submit.php"><< Back to submit page.</a></p>';

            // Clear the id data to clear the form
            $user_name = "";
            $image_name = "";

            mysqli_close($dbc);
          }
          else {
            echo '<p class="error">Sorry, there was a problem uploading your screen shot image.</p>';
          }
        }
      }
      else {
        echo '<p class="error">The screen shot must be a GIF, JPEG, or PNG image file no greater than ' . (GW_MAXFILESIZE / 1024) . ' KB in size.</p>';
      }

      // Try to delete the temporary screen shot image file
      @unlink($_FILES['image_name']['tmp_name']);
    }
    else {
      echo '<p class="error">Please enter all of the information to add your image.</p>';
    }
  }
?>

  <hr />
  <form enctype="multipart/form-data" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo GW_MAXFILESIZE; ?>" />
    <label for="user_name">Enter User Name:</label>
    <input type="text" id="user_name" name="user_name" value="<?php if (!empty($user_name)) echo $user_name; ?>" /><br />
    <label for="image_name">Image:</label>
    <input type="file" id="image_name" name="image_name" />
    <hr />
    <input type="submit" value="Add" name="submit" />
  </form>
</body> 
</html>

 

table.jpg

 

Thanks for looking, hope you can help!

 

Craig

 

Link to comment
https://forums.phpfreaks.com/topic/207353-image-upload-problem/
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.