Solved

Inserting image link into db

Posted on 2007-03-23
24
282 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:Vel Eous
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
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 
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:Vel Eous
Vel Eous 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:Vel Eous
ID: 18783422
What is on line 75 of your update_provider.php file and what lines above that does it access ?
0
 
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:Vel Eous
Vel Eous 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:Vel Eous
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:Vel Eous
Vel Eous 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

These days socially coordinated efforts have turned into a critical requirement for enterprises.
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
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 …

777 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