php, mysql and images ... ARGH!

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

nVee
Forum Newbie
Posts: 11
Joined: Mon Mar 31, 2008 9:44 am

php, mysql and images ... ARGH!

Post by nVee »

Good day

I am sooooo glad i found this forum, i just hope i can be helped before my deadline (which is tomorrow afternoon!) Now before I begin, you need to know that i have VERY LIMITED php experience. I know the basic functions, what they do and in a way how they operate, but thats about it. So giving a one sentence answer like "you need to use the php_function_yourscrewed wont help me what so ever. I will however give all of the code i am using, and really its a simple thing i am struggling with.

I have a customer who wants to add their products to a website. ITs a very simple database application, containing:

* Product Name
* Product image
* Description.

I am using php5 and mysql. The database is already created, with the following fields:

* ID
* name
* img (its a blob type field)
* description

I found a tutorial script which basically does what i need. I can add stuff to the database, edit and delete it. I can obviously also recall it. But my problem comes in with the image. All images will be jpg, 200 width and 150height, and will never be more than 150kb.

I found out today that I have one of 2 ways to do this:
* BLOB
* image directory must be saved in database.

Honestly, i want to learn how to do this, but with my limited php skills, and the fact that i berely understand the script i am using, i have gone thru 100's of "quick tutorials" and EACH ONE IS DIFFERENT, and with that its something new every time! I am not limited to web space, nor database size, so honestly i do not care where the image is stored.

Knowing php, there probably isnt a easy way to do it. I am pasting my code with the hopes that someone can help me to
1) store the files in the database.
2) Give me a LOGICALLY method to retrieve it.

PLEASE PLEASE PLEASE, I am in need of help URGENTLY!

Code: Select all

<html>
<body>
<?php
$image = $row['img'];
$db = mysql_connect("localhost", "root", "password");
 
mysql_select_db("products",$db);
 
if ($submit) {
 
  if ($id) {
 
    $sql = "UPDATE bushshirts SET img='$img',name='$name',description='$description' WHERE id=$id";
 
  } else {
 
    $sql = "INSERT INTO bushshirts (img,name,description) VALUES ('$img','$name','$description')";
 
  }
 
  $result = mysql_query($sql);
 
  echo "Record updated/edited!<p>";
 
} elseif ($delete) {
 
    $sql = "DELETE FROM bushshirts WHERE id=$id";   
 
    $result = mysql_query($sql);
 
    echo "$sql Record deleted!<p>";
 
} else {
 
  if (!$id) {
 
    $result = mysql_query("SELECT * FROM bushshirts",$db);
    while ($myrow = mysql_fetch_array($result)) {
      printf("<a href=\"%s?id=%s\">%s %s</a> \n", $PHP_SELF, $myrow["id"], $img["$image"],$myrow["name"], $myrow["description"]);
      printf("<a href=\"%s?id=%s&delete=yes\">(DELETE)</a><br>", $PHP_SELF, $myrow["id"]);
 
    }
  }
  ?>
 
  <P>
  <a href="<?php echo $PHP_SELF?>">ADD A RECORD</a>
  <P>
  <form action="<?php echo $PHP_SELF?>" method="post" enctype="multipart/form-data">
  <?php
 
  if ($id) {
 
    $sql = "SELECT * FROM bushshirts WHERE id=$id";
    $result = mysql_query($sql);
    $myrow = mysql_fetch_array($result);
    $id = $myrow["id"];
    $first = $myrow["name"];
    $last = $myrow["img"];
    $address = $myrow["description"];
    ?>
 
    <input type=hidden name="id" value="<?php echo $id ?>">
    <?php
  }
 
 
 
  ?>
  Name:
  <input type="name" name="name" value="<?php echo $first ?>"><br>
  Image:
  <label>
  <input type="file" name="img" id="img">
  </label>
<br>
  Description
  <label>
  <textarea name="description" value="<?php echo $description ?>" id="description" cols="45" rows="5"></textarea>
  </label>
  <br>
  <input type="Submit" name="submit" value="Enter information">
 
  </form>
 
<?php
}
 
?>
</body>
</html>

scottayy| Please put your code in BBCode [ code ] tags. I've edited your post to show how we would like it posted.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: php, mysql and images ... ARGH!

Post by aceconcepts »

So you need to upload the image to a directory and also store it in the database and then retrieve it also?

Correct?
nVee
Forum Newbie
Posts: 11
Joined: Mon Mar 31, 2008 9:44 am

Re: php, mysql and images ... ARGH!

Post by nVee »

Honoustly, which ever one is easier!

Each product has a image, so it must be uploaded to the server (or to the database) and then just be shown later as the product pic on the website.

The script i showed above does the upload, edit and delete of the fields. The products are shown on the physical website. That i am sure i will be able to sort out, its just the images which is a problem.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: php, mysql and images ... ARGH!

Post by aceconcepts »

From the script you posted, there is no uploading code.

I am very willing to help, I just need to know what's required.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: php, mysql and images ... ARGH!

Post by pickle »

It'll be easier & faster to just store the filename in the DB & store the actual image in the filesystem.

- Look at the PHPDocs page for HTTP file uploads - you're missing a MAX_FILE_SIZE hidden field.
- After the form has been submitted, both the name of the original file and the name of the temporary file it's been uploaded to, will be in the $_FILES array.
- You can use move_uploaded_file() to move the file from the temporary location, to the place you want to store the file.
- Store the filepath in the database like you would any other bit of text. Remember though that the filepath you use for move_uploaded_file() will be the system filepath (such as /var/www/html/images/myImage.jpg), but the filepath you'll want to store for retrieval will likely be the web filepath (such as /images/myImage.jpg).

Retrieving it is just as easy as retrieving any other bit of text. Once you retrieve it, you can throw the stored filepath in an <img> tag to display it.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
nVee
Forum Newbie
Posts: 11
Joined: Mon Mar 31, 2008 9:44 am

Re: php, mysql and images ... ARGH!

Post by nVee »

Thank you Soooooo much aceconcepts!!!!

Well its quite simple. The script you have there adds, edits and deletes the products. There is one database (bustshirts) which contains id, img, name and description. So far the script adds the products, deletes and edits the products, but i cannot upload photos.

All i need is to get the script to actually upload the photo (regardless whether its on a directory or directly in the database.) Thats really it. I just want that script i posted, to be able to upload the photos to the database or directory. Obviously if it uploads to the directory, the link location must be saved in the database, because what happens later is the potential customers must just click on a button "view our products" and all of the products must be shown, with the names, description and images.

REally, thats it.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: php, mysql and images ... ARGH!

Post by aceconcepts »

Ok,

What should be done is the following:

1. Create a hidden field called MAXFILESIZE (just like pickle said)
2. validate the posted values
3. upload the image to the correct location on the server (not the database)
4. save the image name to the database

Once all this has been done you will be able to retrieve the file name and append it to its location.

I will sort out the code for you.
nVee
Forum Newbie
Posts: 11
Joined: Mon Mar 31, 2008 9:44 am

Re: php, mysql and images ... ARGH!

Post by nVee »

Ace, i cannot begin too thank you enough!

I have made it my duty to go thru ALL of my php books ASAP after this project!!!

I have seen this maxfilesize thing on quite a number of other tutorials. And as you listed the steps below it sounds simple, but for a absolute noob like me, it REALLY is all greek. I understand the principles you have mentioned there, but i guarantee you that the 22 hours i have, i will never sort it out myself!!!

Thank you soooo much for your help, you really have saved me!!!! i will then wait for your code! :)
nVee
Forum Newbie
Posts: 11
Joined: Mon Mar 31, 2008 9:44 am

Re: php, mysql and images ... ARGH!

Post by nVee »

if it helps, i have also created the folder "uploads" and set the permissions to chmod 777
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: php, mysql and images ... ARGH!

Post by pickle »

aceconcepts wrote:1. Create a hidden field called MAXFILESIZE (just like pickle said)
Not meaning to be nitpicky, but it's important that the field is called "MAX_FILE_SIZE" and not "MAXFILESIZE".

~nVee: while you're waiting, a big part of your procedure is covered if you read through the PHP docs section on handling HTTP file uploads.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: php, mysql and images ... ARGH!

Post by aceconcepts »

Doh, thanks pickle :D
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: php, mysql and images ... ARGH!

Post by aceconcepts »

This code will be used to upload your image (very crude and simplistic):

Code: Select all

 
$imgUploadPath="/path/to/uploads";
$uploadPath=$imgUploadPath.basename($_FILES['img']['name']);
 
if(move_uploaded_file($_FILES['img']['tmp_name'], $uploadPath))
{
//FILE UPLOADED SUCCESSFULLY
} 
else    //UPLOAD ERROR - FILE DID NOT UPLOAD
{
//ERROR MSG
}
 
Last edited by aceconcepts on Mon Mar 31, 2008 11:15 am, edited 1 time in total.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: php, mysql and images ... ARGH!

Post by pickle »

If there's going to be more code posted in this thread, I'd just like to remind everyone to wrap it in tags to maintain spacing & (more importantly) syntax highlighting.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
nVee
Forum Newbie
Posts: 11
Joined: Mon Mar 31, 2008 9:44 am

Re: php, mysql and images ... ARGH!

Post by nVee »

Thanks alot aceconcepts

I am going to be quite retarded now, but where do i put the code? I am guessing in the code i posted above, will it make a difference where i post it (aslong as i dont do it in a open statement).

Then how will i get that link to be inserted into the database's "img" field?
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: php, mysql and images ... ARGH!

Post by aceconcepts »

You're gonna want to put it within the if($submit){ statements but before all of the database inserts.

However, you will also need to consider putting all the database inserts within the else brackets e.g.:

Code: Select all

 
$imgUploadPath="/path/to/uploads";
$uploadPath=$imgUploadPath.basename($_FILES['img']['name']);
 
if(move_uploaded_file($_FILES['img']['tmp_name'], $uploadPath))
{
//DATABASE INSERTS GO HERE - YOU DONT WANT TO HAVE MULTIPLE RECORDS IN THE DATBASE
} 
else    //UPLOAD ERROR - FILE DID NOT UPLOAD
{
//ERROR MSG
}
 
Post Reply