PHP insert form/upload photo problems
Posted: Sat Mar 13, 2010 6:22 pm
Hi,
I'm having trouble with my code. I have one page where a user can upload a photo and send it to a folder on my server. On that page they also fill out a form with info about the photo. All of this is sent to the next page and then the file is moved to the folder and the info from the rest of the form should be sent to my database. I first made the page with only the upload functionality and it worked great. When I added the rest it stopped working. This is the first project I have hand coded... so....
Anyway if you don't mind could you look over my code and point out any mistakes? Thanks!
I'm having trouble with my code. I have one page where a user can upload a photo and send it to a folder on my server. On that page they also fill out a form with info about the photo. All of this is sent to the next page and then the file is moved to the folder and the info from the rest of the form should be sent to my database. I first made the page with only the upload functionality and it worked great. When I added the rest it stopped working. This is the first project I have hand coded... so....
Anyway if you don't mind could you look over my code and point out any mistakes? Thanks!
Code: Select all
<?php
// properties of the uploaded file
$filename = $_FILES["myfile"]["name"];
$type = $_FILES["myfile"]["type"];
$size = $_FILES["myfile"]["size"];
$temp = $_FILES["myfile"]["tmp_name"];
$error = $_FILES["myfile"]["error"];
$title = $_POST["title"];
$date = $_POST["date_taken"];
$place = $_POST["place_taken"];
$skier = $_POST["skier"];
$photographer = $_POST["photographer"];
if ($error > 0)
die("Error uploading file! Code $error.");
else
{
if($size > 500000) //conditions for the file
{
die("That's too big... THAT'S WHAT SHE SAID!");
}
else
{
move_uploaded_file($temp, "images/user_uploads/".$filename);
echo "Upload complete!";
}
}
?>
<?php
// database connection info
$hostname_main_db = "localhost";
$database_main_db = "skisite";
$username_main_db = "root";
$password_main_db = "root";
mysql_pconnect($hostname_main_db, $username_main_db, $password_main_db) or die("Server connection failed.");
echo "Connection to the server was successful.<br/>";
mysql_select_db("$database_main_db") or die("Couldn't connect to the database.")
echo "Database was selected successfully.<br/>";
// form elements from last page
$title = $_POST["title"];
$date = $_POST["date_taken"];
$place = $_POST["place_taken"];
$skier = $_POST["skier"];
$photographer = $_POST["photographer"];
$photo_url = $_FILES["myfile"]["name"];
// insert info into database
mysql_query(INSERT INTO photos (name, date_taken, place_taken, skier, photographer, image_url) VALUES ('$title', '$date', '$place', '$skier', '$photographer', 'images/user_uploads/$photo_url');
mysql_close();
?>