...

View Full Version : Uploading Image names to MySql



inebula88
04-15-2009, 07:21 PM
I have created a script (with help) to upload user data and images from an HTML form to a MySQL db. It stores the images in a folder on my server. Everything is working great... except I am getting the tmp path/name written to the db instead of the actual path/name.
My code is below. I appreciate any suggestions!!:confused:


$sql="INSERT INTO test (FirstName, email, Age, state, nude, acting, comments, photo, photo2, photo3, photo4, photo5)
VALUES
('$_POST[firstname]', '$_POST[email]', '$_POST[age]', '$_POST[state]', '$_POST[nude]' , '$_POST[acting]' , '$_POST[comments]', '".$photo."' ,'".$photo2."' , '".$photo3."' ,'".$photo4."' ,'".$photo5."')";

if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}

$first_name=$_POST['firstname'];
$last_name=$_POST['lastname'];
$email=$_POST['email'];
$Age=$_POST['Age'];
$state=$_POST['state'];
$photo=$_POST['photo'];
$nude=$_POST['nude'];
$acting=$_POST['acting'];
$comments=$_POST['comments'];

$photo=($_FILES['photo']['name']);
$photo2=($_FILES['photo2']['name']);
$photo3=($_FILES['photo3']['name']);
$photo4=($_FILES['photo4']['name']);
$photo5=($_FILES['photo5']['name']);

//This is the directory where images will be saved
$target = "images/uploaded/";
$targetx = $target . basename( $_FILES['photo']['name']);

//Writes the photo to the server
if(move_uploaded_file($_FILES['photo']['tmp_name'], $targetx))
{

//Tells you if its all ok
}
else {
//Gives and error if its not
}

$targetx = $target . basename( $_FILES['photo2']['name']);

//Writes the photo to the server
if(move_uploaded_file($_FILES['photo2']['tmp_name'], $targetx))
{
(etc.)

Fumigator
04-15-2009, 09:21 PM
How is it that you are using the variables $photo through $photo5 in your INSERT query before these variables are even assigned a value? Are they assigned a value prior to the INSERT query as well?

inebula88
04-15-2009, 09:47 PM
It is how I set the code up. I have it send an email at the bottom and go to a specific 'thank you' URL. This is the order I was able to get it to work. Here is the full code. I hope it is ok to post it all here. Thank you for taking the time to look at it!! I welcome all suggestions. Obviously I am not very good at this!



<?php
$con = mysql_connect("localhost","username","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db('my_db');


$sql="INSERT INTO Persons (FirstName, email, Age, state, nude, acting, comments, photo, photo2, photo3, photo4, photo5)
VALUES
('$_POST[firstname]', '$_POST[email]', '$_POST[age]', '$_POST[state]', '$_POST[nude]' , '$_POST[acting]' , '$_POST[comments]', '$_FILES[photo]' ,'$_FILES[photo2]' , '$_FILES[photo3]' ,'$_FILES[photo4]' ,'$_FILES[photo5]')";

if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
?>
<script language="JavaScript" type="text/JavaScript">
<!--
window.location.href = "http://www.mywebsite.com/formsubmitted.htm";
//-->
</script>
<?php

$first_name=$_POST['firstname'];
$last_name=$_POST['lastname'];
$email=$_POST['email'];
$Age=$_POST['Age'];
$state=$_POST['state'];
$photo=$_POST['photo'];
$nude=$_POST['nude'];
$acting=$_POST['acting'];
$comments=$_POST['comments'];

$photo=($_FILES['photo']['name']);
$photo2=($_FILES['photo2']['name']);
$photo3=($_FILES['photo3']['name']);
$photo4=($_FILES['photo4']['name']);
$photo5=($_FILES['photo5']['name']);

//This is the directory where images will be saved
$target = "images/uploaded/";
$targetx = $target . basename( $_FILES['photo']['name']);

//Writes the photo to the server
if(move_uploaded_file($_FILES['photo']['tmp_name'], $targetx))
{

//Tells you if its all ok
}
else {
//Gives and error if its not
}

$targetx = $target . basename( $_FILES['photo2']['name']);

//Writes the photo to the server
if(move_uploaded_file($_FILES['photo2']['tmp_name'], $targetx))
{

//Tells you if its all ok
}
else {
//Gives and error if its not
}

$targetx = $target . basename( $_FILES['photo3']['name']);

//Writes the photo to the server
if(move_uploaded_file($_FILES['photo3']['tmp_name'], $targetx))
{

//Tells you if its all ok
}
else {
//Gives and error if its not
}

$targetx = $target . basename( $_FILES['photo4']['name']);

//Writes the photo to the server
if(move_uploaded_file($_FILES['photo4']['tmp_name'], $targetx))
{

//Tells you if its all ok
}
else {
//Gives and error if its not
}

$targetx = $target . basename( $_FILES['photo5']['name']);

//Writes the photo to the server
if(move_uploaded_file($_FILES['photo5']['tmp_name'], $targetx))
{

//Tells you if its all ok
}
else {
//Gives and error if its not
}


$to = "modelinquiry@cash4fetishmodels.com";
$from_header = "From: modelinquiry@cash4fetishmodels.com";
$subject = "Model Request";
$contents = <<< emailbody
Dear Webmaster,

This person has requested more information:
First Name=$first_name
Email=$email
Age=$Age
State=$state
Photo=$photo
Nude=$nude
Acting=$acting
Comments=$comments


emailbody;
mail($to, $subject, $contents, $from_header);
mysql_close($con);
//echo header ("Location: http://cash4fetishmodels.com/formsubmitted.htm");

?>

Fumigator
04-15-2009, 11:32 PM
Your INSERT query has changed to use $_FILES['photo'] now. I can't believe this script even gets past the PHP parser; you can't embed complex variables inside a string like that without wrapping it in squiggly brackets. And finally, the variable $_FILES['photo'] is an array; you need to use $_FILES['photo']['name'].

inebula88
04-16-2009, 12:42 AM
When I do this:


$sql="INSERT INTO test (FirstName, email, Age, state, nude, acting, comments, photo, photo2, photo3, photo4, photo5)
VALUES
('$_POST[firstname]', '$_POST[email]', '$_POST[age]', '$_POST[state]', '$_POST[nude]' , '$_POST[acting]' , '$_POST[comments]', '$_FILES['photo']['name'] ', '$_FILES['photo2']['name'] ', '$_FILES['photo3']['name'] ', '$_FILES['photo4']['name']', '$_FILES['photo5']['name']')";

I get a parsing error.

When I do this:


$sql="INSERT INTO Persons (FirstName, email, Age, state, nude, acting, comments, photo, photo2, photo3, photo4, photo5)
VALUES
('$_POST[firstname]', '$_POST[email]', '$_POST[age]', '$_POST[state]', '$_POST[nude]' , '$_POST[acting]' , '$_POST[comments]', '$_FILES[photo][name]' ,'$_FILES[photo2][name]' , '$_FILES[photo3][name]' ,'$_FILES[photo4][name]' ,'$_FILES[photo5][name]')";

It uploads the picture to the server and puts this in my db:
Array[name]

Sorry I am so bad at this. I really do appreciate your help. And I have spent hours reading books and forums trying to get this right. Thank you again!

Fumigator
04-16-2009, 01:10 AM
You are missing a couple of concepts with regards to strings and arrays in PHP.

First concept to understand: when you embed a variable inside a double-quoted string, it has to be enclosed in squiggly brackets if it's anything more than a simple variable. Your array variables qualify as a complex variable (not that they are actually complex, it's just the terminology that PHP uses).

For example:



$myString = "Hi I am a double-quoted string. I will embed {$_FILES['photo']['name']} into the string.";


You could, if you prefer, break the string into little chunks and keep the variables outside the quotes altogether. Some people prefer this. Example:



$myString = "This variable here: " . $_FILES['photo']['name'] . " is outside the quotes but is still a part of the string.";


If you use a single-quoted string, no variables get parsed.

Next concept: associative arrays (arrays with indexes made of strings rather than just numbers) must have the index names enclosed in quotes. This array has an index name 'photo', and another level deep with an index named 'name':



$_FILES['photo']['name']


If you fail to use those quotes, PHP thinks you're referring to a CONSTANT, not a string. But, since PHP is a forgiving language, it translates that constant into a string for you. You will get into trouble, however, if you ever happen to have an index name that is the same as a constant. So you should always always always use quotes on your index names.

And now that you know you can enclose the variable in squiggly brackets, there's no reason to leave the quotes out.

inebula88
04-16-2009, 01:53 AM
I've been reading my php/mysql book all day and you explained it in one post!!!
I did what you said and it worked. I now have the image name showing in my db. There are lots of other things I'd like to do with this but I'll take it one step at a time. I wish there was something I could help you with... obviously not PHP/Mysql :) LOL!


$sql="INSERT INTO test (FirstName, email, Age, state, nude, acting, comments, photo, photo2, photo3, photo4, photo5)
VALUES
('$_POST[firstname]', '$_POST[email]', '$_POST[age]', '$_POST[state]', '$_POST[nude]' , '$_POST[acting]' , '$_POST[comments]', '{$_FILES['photo']['name']}' ,'{$_FILES['photo2']['name']}' , '{$_FILES['photo3']['name']}' ,'{$_FILES['photo4']['name']}' ,'{$_FILES['photo5']['name']}')";



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum