Inserting image link into db

What is the best and easiest way to insert an image's link into the db and then call it onto the page?
LVL 1
pingeyegAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Vel EousResearch & Development ManagerCommented:
Into DB:

$url = addslashes($_POST['inputField']);


Out of DB:

$url = stripslashes(("SELECT url FROM table"));

echo $url;
0
pingeyegAuthor Commented:
I thought that seemed too easy.  Doesn't seem to do anything.
0
pingeyegAuthor Commented:
What I'm doing is using a form that has a file select button to add the image link to the database.
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

sebastienboCommented:
hmm, strange

what do you wanna put excly in the DB ? the image itself of the local path that the user has created by pushing the browse button (the latter doesn't make sense to do)
0
pingeyegAuthor Commented:
I want to place the link of where the image is located on the server into the database and then be able to retrieve it by using something like:

<img src="<?= strImage?>">
0
sebastienboCommented:
wll understand correctly, that by doing so, you will have a local path: c:\path\to\image.jpg , in order for this to work with the img tag, only the uploader will be able to see this image, as the image will be localy by the user who has uploaded the link.

On this page :
http://www.tizag.com/phpT/fileupload.php

You have an example of how to upload an image, but what you need to do is not copying the file to somewhere but just copy the path + filename called $_FILES['uploadedfile']['name'] in the example, right to your DB , the DB field will contain only the name and path of the localy located file
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
pingeyegAuthor Commented:
From what I read I was able to come up with this, but it doesn't seem to work correctly unless I am missing something:

$target_path = "mockup/images/";
$target_path = $target_path . basename( $_FILES['strImage']['name']);

addslashes($strOtherservices);
addslashes($strServicesoffered);
addslashes($strProvidertagline);

$SQL = mysql_query("SELECT * FROM tblAdspace where strCompanyname = '" .$strCompanyname. "'")
or die(mysql_error());

if ($row = mysql_fetch_array($SQL)) {

$SQL = mysql_query("UPDATE tblAdspace SET "
       . "strProviderservice = '" . $strProviderservice . "', "
     . "strCompanyname='" . $strCompanyname . "', "
     . "strOwner='" . $strOwner . "', "
       . "strAddress='" . $strAddress . "', "
     . "strTown='" . $strTown . "', "
       . "strZipcode='" . $strZipcode . "', "
       . "strPhone='" . $strPhone . "', "
     . "str2ndphone='" . $str2ndphone . "', "
     . "strMobile='" . $strMobile . "', "
     . "strPager='" . $strPager . "', "
     . "strFax='" . $strFax . "', "
     . "strEmail='" . $strEmail . "', "
     . "strWebsite='" . $strWebsite . "', "
     . "strLicense='" . $strLicense . "', "
     . "strInsured='" . $strInsured . "', "
     . "strBonded='" . $strBonded . "', "
     . "strHours='" . $strHours . "', "
     . "str24houremerg='" . $str24houremerg . "', "
     . "strOtherservices='" . $strOtherservices . "', "
     . "strServicearea='" . $strServicearea . "', "
     . "strInbusiness_since='" . $strInbusiness_since . "', "
     . "strServicesoffered='" . $strServicesoffered . "', "
       . "strFreeestimate='" . $strFreeestimate . "', "
     . "strWorkguaranteed='" . $strWorkguaranteed . "', "
       . "strProvidertagline='" . $strProvidertagline . "', "
     . "strAd_size='" . $strAd_size . "', "
       . move_uploaded_file($_FILES['strImage']['tmp_name'], $target_path);
     . " WHERE strCompanyname = '" . $strCompanyname . "'");
0
pingeyegAuthor Commented:
Any ideas?
0
pingeyegAuthor Commented:
Right now these are the errors that I am getting:

Warning: move_uploaded_file(/Library/WebServer/Documents/images/plumber_premium.jpg) [function.move-uploaded-file]: failed to open stream: Permission denied in /Library/WebServer/Documents/admin/update_provider.php on line 75

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/var/tmp/phpOq5GrV' to '/Library/WebServer/Documents/images/plumber_premium.jpg' in /Library/WebServer/Documents/admin/update_provider.php on line 75
0
Vel EousResearch & Development ManagerCommented:
Have you set the CHMOD value of the folder you are uploading the files into to '777' ?
0
pingeyegAuthor Commented:
I just tried that, but it still gives me the same error
0
Vel EousResearch & Development ManagerCommented:
What is on line 75 of your update_provider.php file and what lines above that does it access ?
0
pingeyegAuthor Commented:
Ok, I finally got the permission issue fixed, but still the image's link isn't being sent into the db.
0
pingeyegAuthor Commented:
I don't know what I have done now, but I can't view my website now that I have tampered with the permissions.  My document folder now has other as no access.
0
pingeyegAuthor Commented:
Nevermind, I'm back in
0
pingeyegAuthor Commented:
Line 75 is: . move_uploaded_file($_FILES['strImage']['tmp_name'], $target_path)
0
Vel EousResearch & Development ManagerCommented:
Dont put your PHP move_uploaded_file function in your SQL statement.

Do your PHP upload_file functions first, then deal with the SQL, dont do to many things at once.

Personaly I dont think you should be concatinating your information into your DB anyway but perhaps that is just me.
0
pingeyegAuthor Commented:
Is this what you mean?

$target_path = "/Library/WebServer/Documents/images/";
$target_path = $target_path . basename( $_FILES['strImage']['name']);
$_FILES['strImage']['tmp_name'];
move_uploaded_file($_FILES['strImage']['tmp_name'], $target_path);

addslashes($strOtherservices);
addslashes($strServicesoffered);
addslashes($strProvidertagline);

$SQL = mysql_query("SELECT * FROM tblAdspace where strCompanyname = '" .$strCompanyname. "'")
or die(mysql_error());

if ($row = mysql_fetch_array($SQL)) {

$SQL = mysql_query("UPDATE tblAdspace SET "
       . "strProviderservice = '" . $strProviderservice . "', "
     . "strCompanyname='" . $strCompanyname . "', "
     . "strOwner='" . $strOwner . "', "
       . "strAddress='" . $strAddress . "', "
     . "strTown='" . $strTown . "', "
       . "strZipcode='" . $strZipcode . "', "
       . "strPhone='" . $strPhone . "', "
     . "str2ndphone='" . $str2ndphone . "', "
     . "strMobile='" . $strMobile . "', "
     . "strPager='" . $strPager . "', "
     . "strFax='" . $strFax . "', "
     . "strEmail='" . $strEmail . "', "
     . "strWebsite='" . $strWebsite . "', "
     . "strLicense='" . $strLicense . "', "
     . "strInsured='" . $strInsured . "', "
     . "strBonded='" . $strBonded . "', "
     . "strHours='" . $strHours . "', "
     . "str24houremerg='" . $str24houremerg . "', "
     . "strOtherservices='" . $strOtherservices . "', "
     . "strServicearea='" . $strServicearea . "', "
     . "strInbusiness_since='" . $strInbusiness_since . "', "
     . "strServicesoffered='" . $strServicesoffered . "', "
       . "strFreeestimate='" . $strFreeestimate . "', "
     . "strWorkguaranteed='" . $strWorkguaranteed . "', "
       . "strProvidertagline='" . $strProvidertagline . "', "
     . "strAd_size='" . $strAd_size . "', "
       . "strImage='" . $strImage . "'"
     . " WHERE strCompanyname = '" . $strCompanyname . "'");
0
pingeyegAuthor Commented:
Ok, I got it to work finally!  Now my only problem is shortening the link.
0
Vel EousResearch & Development ManagerCommented:
$filename = basename( $_FILES['uploadedfile']['name']);
0
pingeyegAuthor Commented:
Not sure what that was supposed to do?
0
Vel EousResearch & Development ManagerCommented:
That gets just the filename rather than the absolute file URL.

So,

$target_path = "/Library/WebServer/Documents/images/";
$target_path = $target_path . basename( $_FILES['strImage']['name']);
$filename = basename($_FILES['strImage']['name']);
$_FILES['strImage']['$filename'];
move_uploaded_file($_FILES['strImage']['$filename'], $target_path);
0
pingeyegAuthor Commented:
That didn't seem to work.  I'm still getting the entire link.  I would like to have inserted only this:

/images/image
0
pingeyegAuthor Commented:
I got it to work.  Thanks!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.