uploading images with PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ninethousandfeet
Forum Contributor
Posts: 130
Joined: Tue Mar 10, 2009 4:56 pm

uploading images with PHP

Post by ninethousandfeet »

hello,

i'm trying to allow my users to upload images when they add a new product. my db has a table called postTable (post_id, post_title, product, post_date, user_id, image, image_type).
i have an insert form as follows:

Code: Select all

<form id="Upload" action="<?php echo $editFormAction ?>" enctype="multipart/form-data" method="post"> 
     
        <h1> 
            Upload form 
      </h1>
            <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $max_file_size ?>">
            <input name="post_id" type="hidden" id="post_id" />
      <input name="user_id" type="hidden" id="user_id" value="<?php echo $row_rsgetUsername['user_id']; ?>" />
      <input name="buy_or_share" type="hidden" id="buy_or_share" value="buy" />
      <input name="post_date" type="hidden" id="post_date" value="NOW()" />
<p> 
    <label for="post_title">Posting Title:</label> 
            <input id="post_title" type="text" name="post_title"> 
      </p> 
      <p> 
    <label for="product_name">Product/Store Name:</label> 
            <input id="product_name" type="text" name="product_name"> 
      </p>
      <p> 
    <label for="category_name">Category Name:</label> 
            <input id="category_name" type="text" name="category_name"> 
      </p>
      <p> 
    <label for="file">File to upload:</label> 
            <input id="image_data" type="file" name="image_data"> 
      </p>
                 
        <p> 
            <label for="submit">Press to...</label> 
            <input id="submit" type="submit" name="submit" value="Upload me!"> 
        </p> 
     
</form>
i'm doing something wrong because this is not working and i'm going in circles. i think it has to do with where i'm sending the uploaded file, but i'm not sure. this is the first time i have tried to allow a user to add a file/image so i don't really know what to and not to do.

any recommendations would help, thank you for taking a look!
User avatar
php_east
Forum Contributor
Posts: 453
Joined: Sun Feb 22, 2009 1:31 pm
Location: Far Far East.

Re: uploading images with PHP

Post by php_east »

your inputs do not have closures.
example ...

Code: Select all

<input id="image_data" type="file" name="image_data">
should be

Code: Select all

<input id="image_data" type="file" name="image_data" />
or

Code: Select all

<input id="image_data" type="file" name="image_data"></input>
assuming your browser is kind enough to forgive you for forgetting the / then what's left to check is your form's action url.
User avatar
Inkyskin
Forum Contributor
Posts: 282
Joined: Mon Nov 19, 2007 10:15 am
Location: UK

Re: uploading images with PHP

Post by Inkyskin »

Quick tip: It's good practice to end your statements properly...

Code: Select all

<?php echo $editFormAction ?>
 
// this is better:
 
<?php echo $editFormAction; ?>
As for your problem - are you getting any error messages etc? 'It's not working', and not showing any of the PHP, leaves things a little vague...
ninethousandfeet
Forum Contributor
Posts: 130
Joined: Tue Mar 10, 2009 4:56 pm

Re: uploading images with PHP

Post by ninethousandfeet »

okay so i cleaned the code up thanks to your recommendations.

the error message i receive is, Unknown column 'Array' in 'field list'...

right now, this is my insert form script:

Code: Select all

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
 
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  $insertSQL = sprintf("INSERT INTO postingTable (post_id, post_title, product_name, user_id, post_date, buy_or_share, category_name, image_data, image_type) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['post_id'], "int"),
                       GetSQLValueString($_POST['post_title'], "text"),
                       GetSQLValueString($_POST['product_name'], "text"),
                       GetSQLValueString($_POST['user_id'], "int"),
                       GetSQLValueString($_POST['post_date'], "defined", 'NOW()'),
                       GetSQLValueString($_POST['buy_or_share'], "text"),
                       GetSQLValueString($_POST['category_name'], "text"),
                       GetSQLValueString($_FILES['image_data'], "file"),
                       GetSQLValueString($_FILES['image_type'], "text"));
 
  mysql_select_db($database_connUser, $connUser);
  $Result1 = mysql_query($insertSQL, $connUser) or die(mysql_error());
 
  $insertGoTo = "index.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));
}
and this is my form script...

Code: Select all

<form action="<?php echo $editFormAction; ?>" method="post" enctype="multipart/form-data" id="form1">
  <table align="center">
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Post title:</td>
      <td><input type="text" name="post_title" value="" size="50" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right" valign="top">Product/Store Names:</td>
      <td><textarea name="product_name" cols="50" rows="5"></textarea></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Category:</td>
      <td><select name="category_name">
        <option value="other" <?php if (!(strcmp("other", ""))) {echo "SELECTED";} ?>>Other</option>
      </select></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Add Picture:</td>
      <td><input type="file" name="image_data" value="" size="40" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">&nbsp;</td>
      <td><input type="submit" value="Post" /></td>
    </tr>
  </table>
  <input type="hidden" name="post_id" value="" />
  <input type="hidden" name="user_id" value="<?php echo $row_getUser['user_id']; ?>" />
  <input type="hidden" name="post_date" value="NOW()" />
  <input type="hidden" name="buy_or_share" value="buy" />
  <input type="hidden" name="image_type" value="" />
  <input type="hidden" name="MM_insert" value="form1" />
</form>
User avatar
Inkyskin
Forum Contributor
Posts: 282
Joined: Mon Nov 19, 2007 10:15 am
Location: UK

Re: uploading images with PHP

Post by Inkyskin »

The quickest way to diagnose an SQL error is to echo the SQL statement. Try putting this before you execute the query, and see what it outputs:

Code: Select all

 
echo $insertSQL;
 
ninethousandfeet
Forum Contributor
Posts: 130
Joined: Tue Mar 10, 2009 4:56 pm

Re: uploading images with PHP

Post by ninethousandfeet »

this is the result of the echo $insertSQL: INSERT INTO postingTable (post_id, post_title, product_name, user_id, post_date, buy_or_share, category_name, image_data) VALUES (NULL, 'asfdsafsdfsdf', 'sdfasdfsdafd', 174, NOW(), 'buy', 'other', Array)Unknown column 'Array' in 'field list'

so it looks like the two issues would be, why is the post_id NULL and what is with the Array where the image_data should be submitted?

any ideas?
User avatar
Inkyskin
Forum Contributor
Posts: 282
Joined: Mon Nov 19, 2007 10:15 am
Location: UK

Re: uploading images with PHP

Post by Inkyskin »

You don't actually need to insert the Post id - that should automatically be created by the table in the database, so long as everything has been set up correctly. Personally, I'd remove that column and value.

As for the array issue, I'm not too sure, I never use images in databases, I just upload the file to a folder and keep the name in the database so I can call it at a later time.
ninethousandfeet
Forum Contributor
Posts: 130
Joined: Tue Mar 10, 2009 4:56 pm

Re: uploading images with PHP

Post by ninethousandfeet »

okay... so i'm pretty sure i figured it out. i had this in my insert: GetSQLValueString($_FILES['image_data'], "file")); and the "file" had to be changed to "text" for some reason?
now you might laugh at this, but i have no idea how to display the image in a browser... you mentioned you upload the file to a folder, which i think that is what i'll do to keep the space open. let's say i have a user who searches for another user's postings, finds something they like, and clicks to view the details. how do i display the details and the image?
ninethousandfeet
Forum Contributor
Posts: 130
Joined: Tue Mar 10, 2009 4:56 pm

Re: uploading images with PHP

Post by ninethousandfeet »

i was wrong, switching it to text didn't help, it only worked b/c it accepted the word "Array" as input. any ideas as to how to insert the blob?
Post Reply