Link to home
Create AccountLog in
Avatar of pingeyeg
pingeyeg

asked on

Code is not working properly when adding images to the server

The code that I currently have is spotty.  Sometimes it does right and sometimes it does not.  I need something that will work all the time.  My problem is that when you upload an image to the server and have the image name inserted into the database, somtimes the image name gets loaded like ../../listings/something.jpg and sometimes it gets loaded like something.jpg.  I want it to always upload the image file to the server and the image name to the database.  Can anyone help me out with this?  My code is below.
Add Page:
 
 
$target_path = '../../listings/';
	$target_path = $target_path . basename( $_FILES['strImage']['name']); 
	$_FILES['strImage']['tmp_name'];
	copy($_FILES['strImage']['tmp_name'], $target_path);
	
 
$conn = mysql_connect("localhost", "username", "password")
or die(mysql_error());
mysql_select_db("347529_listings");
 
$theQuery = "INSERT INTO featured_listings (strPrice, strAddress, strCity, strState, strZip, strCounty, strYearBuilt, strSubdiv, strStories, strBeds, strBaths, strElementary, strMiddle, strHigh, strDirections, strRemarks, strFeatureBed, strFeatureMaster, strFeatureKitchen, strFeatureDining, strFeatureConst, strFeatureTen, strFeaturePool, strFeatureParking, strFeatureRoad, strFeatureRooms, strFeatureBasement, strFeatureLotSize, strListDate, strImage) VALUES ('$price', '$address', '$city', '$state', '$zip', '$county', '$yearbuilt', '$subdiv', '$stories', '$beds', '$baths', '$elementary', '$middle', '$high', '" . mysql_real_escape_string($directions) . "', '" . mysql_real_escape_string($remarks) . "', '$featurebed', '$featuremaster', '$featurekitchen', '$featuredining', '$featureconst', '$featureten', '$featurepool', '$featureparking', '$featureroad', '$featurerooms', '$featurebasement', 'featurelotsize', '$listdate', '$target_path')";
 
$add_info = mysql_query($theQuery) or die(mysql_error());
 
 
Edit Page:
 
$target_path = '../../listings/';
	$target_path = $target_path . basename($_FILES['strImage']['name']); 
	$_FILES['strImage']['tmp_name'];
	copy($_FILES['strImage']['tmp_name'], $target_path);
		
	
$conn = mysql_connect("localhost", "username", "password")
or die(mysql_error());
mysql_select_db("347529_listings");
 
if($target_path != "../../listings/") {
	
$update_image = "UPDATE featured_listings SET strImage = '" . addslashes(basename($target_path)) . "' WHERE ID = $id";
 
$image_updated = mysql_query($update_image) or die(mysql_error());
 
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of favoretti
favoretti
Flag of Netherlands image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
SOLUTION
Avatar of Steve Bink
Steve Bink
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of pingeyeg
pingeyeg

ASKER

I'm not sure what you mean by 6-23 as they are two separate pages.  What I am trying to have done is on the Add Page I want the user to add an image as part of the listing, but if they don't have one quite yet then nothing gets inserted into the database as far as a name.  If they finally have an image for that listing I want them to go to the Edit Page and upload the image that way.  Does that make sense?
Actually the other problem I am having now is that the image file is not being uploaded to the server.
Ok, I think I figured out the adding the image name to the db without having the ../../listings added to it, but the image file is not being uploaded to the server.
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
To be honest, I don't know about lines 6 and 23.  That was the code given to me to be able to upload the file to the server.
I got it working.  Thanks anyway.
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
I readded the function baseline to the $target variable to make sure only the name still got inserted.