Solved

Inserting image link into db

Posted on 2007-03-23
24
278 Views
Last Modified: 2008-01-09
What is the best and easiest way to insert an image's link into the db and then call it onto the page?
0
Comment
Question by:pingeyeg
  • 16
  • 6
  • 2
24 Comments
 
LVL 14

Expert Comment

by:Tchuki
ID: 18781731
Into DB:

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


Out of DB:

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

echo $url;
0
 
LVL 1

Author Comment

by:pingeyeg
ID: 18782352
I thought that seemed too easy.  Doesn't seem to do anything.
0
 
LVL 1

Author Comment

by:pingeyeg
ID: 18782360
What I'm doing is using a form that has a file select button to add the image link to the database.
0
 
LVL 5

Expert Comment

by:sebastienbo
ID: 18782529
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
 
LVL 1

Author Comment

by:pingeyeg
ID: 18782560
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
 
LVL 5

Accepted Solution

by:
sebastienbo earned 200 total points
ID: 18782643
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
 
LVL 1

Author Comment

by:pingeyeg
ID: 18783159
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
 
LVL 1

Author Comment

by:pingeyeg
ID: 18783275
Any ideas?
0
 
LVL 1

Author Comment

by:pingeyeg
ID: 18783309
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
 
LVL 14

Assisted Solution

by:Tchuki
Tchuki earned 300 total points
ID: 18783338
Have you set the CHMOD value of the folder you are uploading the files into to '777' ?
0
 
LVL 1

Author Comment

by:pingeyeg
ID: 18783409
I just tried that, but it still gives me the same error
0
 
LVL 14

Expert Comment

by:Tchuki
ID: 18783422
What is on line 75 of your update_provider.php file and what lines above that does it access ?
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 1

Author Comment

by:pingeyeg
ID: 18783431
Ok, I finally got the permission issue fixed, but still the image's link isn't being sent into the db.
0
 
LVL 1

Author Comment

by:pingeyeg
ID: 18783461
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
 
LVL 1

Author Comment

by:pingeyeg
ID: 18783470
Nevermind, I'm back in
0
 
LVL 1

Author Comment

by:pingeyeg
ID: 18783472
Line 75 is: . move_uploaded_file($_FILES['strImage']['tmp_name'], $target_path)
0
 
LVL 14

Assisted Solution

by:Tchuki
Tchuki earned 300 total points
ID: 18783497
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
 
LVL 1

Author Comment

by:pingeyeg
ID: 18783506
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
 
LVL 1

Author Comment

by:pingeyeg
ID: 18783516
Ok, I got it to work finally!  Now my only problem is shortening the link.
0
 
LVL 14

Expert Comment

by:Tchuki
ID: 18783541
$filename = basename( $_FILES['uploadedfile']['name']);
0
 
LVL 1

Author Comment

by:pingeyeg
ID: 18783559
Not sure what that was supposed to do?
0
 
LVL 14

Assisted Solution

by:Tchuki
Tchuki earned 300 total points
ID: 18783581
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
 
LVL 1

Author Comment

by:pingeyeg
ID: 18783813
That didn't seem to work.  I'm still getting the entire link.  I would like to have inserted only this:

/images/image
0
 
LVL 1

Author Comment

by:pingeyeg
ID: 18785238
I got it to work.  Thanks!
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now