Jump to content

uploading image to server and link to mysql db


sayr

Recommended Posts

Hello!

 

I have a problem here which i really don't get...

I have a database and a form which uploads typed data and image if wanted.

What I want is a working script that uploads chosen picture to server and makes the link of the picture to mysql database.

It's like an webshop which has products listed and evey product has it's picture there...

 

I have two files; upload.php and uploaded.php

Here is the image upload part from upload.php;

<form method="post" action="uploaded.php" enctype="multipart/form-data">
...some code here...
<strong>2. Image upload</strong><br><br>
<input type="file" name="image[]" alt="image" size=50 maxlength=100><br><br>
...some code here too...
<input type="submit" name="action" value="Save">

 

Ok so next one is uploaded.php;

...some code here...
  foreach ($_FILES["image"]["error"] as $key => $error) {
   if ($error == UPLOAD_ERR_OK) {
       $tmp_name = $_FILES["image"]["tmp_name"][$key];
       $name = $_FILES["image"]["name"][$key];
       move_uploaded_file($tmp_name, "images/$name");
   }
}
$image=$name;
...some code here...

I didn't paste the whole code, because there is a huge line of it.

 

So that code uploads the image to /images file, but it doesn't add the image name(link) to database. The database field "image" is just empty.

 

Could you help me with this please!

Thanks!

Link to comment
Share on other sites

Yes im trying to store the link to database, not the image. The image I want to be uploaded in server (which it does already)...

I am quite new with php so im not sure is that code making a link to db? Earlier it actually did wrote the name of the uploaded picture to database, but then it didn't upload the image itself to server... And now it's the opposite

Link to comment
Share on other sites

I had the same issue to start with,

Then I saw that I needed to look at what is getting sent in the insert script.

 

If I remember correctly...

 

I set a variable

$address = 'http://www.yoursite.com.au/images/"

 

Then I had my File Uploader for the image to go to folder destination = images/$image

 

After that I made

$image_name = $address . $image

under the File Uploader

 

Sending the $image_name to the database.

 

So....

The database receives (http://www.yoursite.com.au/images/image_name.jpg)

Folder receives (image_name.jpg)

 

Is this any help ?

Cheers

Link to comment
Share on other sites

In your mysql insert statement you'll want to take the image name $_POST['image'] and do something like this:

 

$imageinsert = "INSERT INTO imagetable (image, value1, value2) VALUES ('$_POST[image]', 'value1', 'value2')";

 

mysql_query($yourconnectionstring, $imageinsert);

 

I am not on a lamp box right now so I can't verify the above code, but something very similiar to that should work.  I am developing an application with this feature in it and that code works.  If you want a better example I can post my code when I get home.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.