/    Sign up×
Community /Pin to ProfileBookmark

image or file path to database

i know this topic has been covered before, but all of the solutions seem to deal with uploading the file after the image or file has been submitted in a form. my situation is somewhat different.

what i have is a mixed form where users submit info, and this is of course passed over to the next page as usual. here is where things differ from the norm: the next page validates the user input for errors and either redirects them to back to the first page to fix errors or proceed. (in the future the user will be asked for more information on the next page, but that is irrelevant for the moment.) if all of the user info checks out, the very last step is to write the image to a temporary folder.

on the next page (or wherever it will be later) the user info is entered into the database, a folder is created using the username, and the image name and path are changed so that the file now appears in the user folder. i did it this way so that only users who have completed registration will get a folder and image, and therefore not clutter my database with useless stuff.

all is good so far, but here is my problem: i cannot get the image path to write to the database. as i have mentioned, the image at this point is no longer associated with the original form that posted it, and the new path is now stored in a session variable containing the new file name and parent folder. i know the path works because the image will display if called up in a regular html img statement.

also, and this might be the giveaway for anyone who knows more about this than me, when i try to insert the path variable into the database with my other variables (name, email, etc) the user info is not entered into the database at all. however if i remove the path variable from my insert code, the other info is entered into a new user account as per normal – just no user image path.

i have tried several solutions including change the field type, but i get nothing at all. in the end i need the file in the user folder so that users can easily get to it, but i need the path to appear correctly in the database – since the file is already on the server i cannot see any difference between the file exsisting in the database or not, as long as i can get to wherever it is. later i will need to put multiple user images into an array or something and keep them organized, so i need to crack this.

(sorry for rambling on, but i’m just trying to give enough info up front.)

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@NogDogAug 23.2010 — My guess without seeing any code is that either the SQL syntax you used with the image page was wrong (often just a simple typo like a missing comma or quote, or you failed to escape the value (e.g. via mysql_real_escape_string()) and some character in the path's value was screwing up the SQL (e.g. a back-slash?). I'd suggest doing some basic debugging of the query, for instance, if using the basic MySQL interface:
[code=php]
$query = "<your query sting goes here>";
$result = mysql_query($query);
if($result == false) {
user_error(mysql_error()."<br />n$query"); // or use error_log()
exit; // or do something more "graceful"
}
[/code]

(Don't forget to make sure display_errors is on if you want to see the results of user_error() in the browser instead of the error log.)
Copy linkTweet thisAlerts:
@mimewearauthorAug 23.2010 — thanks nogdog, you've been very helpful lately.

here is my code from page 1:

[CODE]
$tempimg01 = "temp/";
$tempimg01 = $tempimg01 . basename($_FILES['uploaded']['name']);
$tempimg02 = basename($_FILES['uploaded']['name']);
[/CODE]


you can see there that the file is first told how to get to the temp folder, and then the file name only is stored in the tempimg02 variable which is passed to the next page, where:

[CODE]$userimg = "$username-$tempimg02";
rename("temp/$tempimg02", "$username/$userimg");[/CODE]


and later on the same page:

[CODE]$sql = "INSERT INTO $userdata SET username='$username', email='$email',
password='$password, userimg='$userimg'";
$result = mysql_query($sql);[/CODE]


the file is renamed with the username in front and placed/moved to the new folder also bearing the username. the user info, in this case the username and userimg variables, are placed in a session, and from this point if this username/userimg combo is called up in an image statement, the file displays properly. the database, however, never gets the file name (or username, email, etc.)

so, for example, if josh registers and uploads '12345.jpg', the image is renamed 'josh-12345.jpg' and placed in the new folder called 'josh'. anytime username/userimg is called up you see the image. however, if i check the database, there is no record of josh.
Copy linkTweet thisAlerts:
@NogDogAug 23.2010 — So have you tried adding the error-checking as I suggested above? E.g.:
[code=php]
$sql = "INSERT INTO $userdata SET username='$username', email='$email',
password='$password, userimg='$userimg'";
$result = mysql_query($sql);
if($result == false) {
die(mysql_error()."<br />n$sql");
}
[/code]

Also, you should be applying mysql_real_escape_string() to each of the input values being used in your query, in order to prevent SQL injection errors/attacks. (Unless you have magic_quotes_gpc turned on, in which case you should negate its damage first).
Copy linkTweet thisAlerts:
@mimewearauthorAug 23.2010 — it all works now. i did put the "mysql_real_escape_string" just before my insert code, but i also found a missing single quote after the "pasword" insert just before "userimg" - it always seems to be the little things.

i am not familiar with "mysql_real_escape_string". i'm assuming it lets certain characters through that would otherwise be chopped out or barred entry?

thanks again - i'll probably be bugging you again for more advise in a week or two.
Copy linkTweet thisAlerts:
@NogDogAug 23.2010 — mysql_real_escape_string() "escapes" certain characters by inserting a back-slash in front of them. This makes the MySQL SQL parser treat them as the literal character and ignoring any special meaning they might otherwise have in an SQL statement, e.g.: http://xkcd.com/327/ .
Copy linkTweet thisAlerts:
@mimewearauthorAug 23.2010 — i see. so it's the same as when i edit by hand, say, echoing a line of html or whatever - putting backslashes in front of quotes and that sort of thing. that's good to know.

thanks again.
×

Success!

Help @mimewear spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.1,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...