• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 289
  • Last Modified:

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?
0
pingeyeg
Asked:
pingeyeg
  • 16
  • 6
  • 2
4 Solutions
 
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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
 
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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 16
  • 6
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now