PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
mikegotnaild
Forum Contributor
Posts: 173 Joined: Sat Feb 14, 2004 5:59 pm
Post
by mikegotnaild » Mon Feb 16, 2004 5:32 pm
Ok i have a form that takes in data and puts it in mysql. This form also uploads 2 files. How would i get the name of the files that people upload into mysql columns? ('imagefile' and 'mp3file'.) Heres what i tried: (imagefile and mp3file columns were blank when i checked in phpmyadmin)
Code: Select all
<?
//MySQL Variables
$host = "hostname";
$login_name = "username";
$password = "password";
//Connecting to MYSQL
MySQL_connect("$host","$login_name","$password");
//Select the database
MySQL_select_db("bandsandmembers") or die("Could not select database");
//Assign contents of form to variables
$bandname = $_POSTї'band_name'];
$description = $_POSTї'description'];
$history = $_POSTї'history'];
$influences = $_POSTї'influences'];
$genra = $_POSTї'genra'];
$email = $_POSTї'email'];
$website = $_POSTї'website'];
$imagefile = $_POSTї'imagefile']ї'name'];
$mp3file = $_POSTї'mp3file']ї'name'];
//This is the File upload part
$uploaddir = $_SERVERї'DOCUMENT_ROOT'].'/localmm/upload/';
$uploadfile1 = $uploaddir . $_FILESї'imagefile']ї'name'];
$uploadfile2 = $uploaddir . $_FILESї'mp3file']ї'name'];
if (!move_uploaded_file($_FILESї'imagefile']ї'tmp_name'], $uploadfile1)) {
print "ERROR: File is invalid";
print_r($_FILES);
}
if (!move_uploaded_file($_FILESї'mp3file']ї'tmp_name'], $uploadfile2)) {
print "ERROR: File is invalid";
print_r($_FILES);
}
$sql = "INSERT INTO nuke_bands (band_name, description, history, influences, genra, email, website, imagefile, mp3file) VALUES ('$bandname','$description','$history','$influences','$genra','$email','$website','$imagefile','$mp3file')";
$result = mysql_query($sql);
//Code to check if statement executed properly and display message
if ($result) {
header("Location: http://naild.com/localmm/modules.php?name=Band_List&file=sent");
} else {
echo("An error has occured");
}
MySQL_close()
?>
Last edited by
mikegotnaild on Mon Feb 16, 2004 8:23 pm, edited 2 times in total.
mikegotnaild
Forum Contributor
Posts: 173 Joined: Sat Feb 14, 2004 5:59 pm
Post
by mikegotnaild » Mon Feb 16, 2004 5:37 pm
I probably did something stupid :/.
DuFF
Forum Contributor
Posts: 495 Joined: Tue Jun 24, 2003 7:49 pm
Location: USA
Post
by DuFF » Mon Feb 16, 2004 5:42 pm
Looks good to me, but may need some debugging. Try adding in some "echos" so you can see whats going on.
Code: Select all
<?php
//MySQL Variables
$host = "hostname";
$login_name = "username";
$password = "password";
//Connecting to MYSQL
MySQL_connect("$host","$login_name","$password");
//Select the database
MySQL_select_db("bandsandmembers") or die("Could not select database");
//Assign contents of form to variables
$bandname = $_POST['band_name'];
$description = $_POST['description'];
$history = $_POST['history'];
$influences = $_POST['influences'];
$genra = $_POST['genra'];
$email = $_POST['email'];
$website = $_POST['website'];
$imagefile = $_POST['imagefile']['name'];
$mp3file = $_POST['mp3file']['name'];
//added
echo "Image Filename: " . $imagefile . "<br />\nMp3 Filename: " . $mp3file . "<br />\n";
//added
//This is the File upload part
$uploaddir = $_SERVER['DOCUMENT_ROOT'].'/localmm/upload/';
$uploadfile1 = $uploaddir . $_FILES['imagefile']['name'];
$uploadfile2 = $uploaddir . $_FILES['mp3file']['name'];
if (!move_uploaded_file($_FILES['imagefile']['tmp_name'], $uploadfile1)) {
print "ERROR: File is invalid";
print_r($_FILES);
}
if (!move_uploaded_file($_FILES['mp3file']['tmp_name'], $uploadfile2)) {
print "ERROR: File is invalid";
print_r($_FILES);
}
$sql = "INSERT INTO nuke_bands (band_name, description, history, influences, genra, email, website, imagefile, mp3file) VALUES ('$bandname','$description','$history','$influences','$genra','$email','$website','$imagefile','$mp3file')";
//added
echo "Query: " . $sql;
//added
$result = mysql_query($sql);
//Code to check if statement executed properly and display message
if ($result) {
// EDITED OUT SO YOU CAN SEE THE VARIABLES OUTPUTTED
//header("Location: http://naild.com/localmm/modules.php?na ... &file=sent");
} else {
echo("An error has occured: " . mysql_error()); //added more error info
}
mysql_close(); //this should be lowercase, functions are case sensitive
?>
See what that outputs.
mikegotnaild
Forum Contributor
Posts: 173 Joined: Sat Feb 14, 2004 5:59 pm
Post
by mikegotnaild » Mon Feb 16, 2004 5:46 pm
ok. OH i think i forgot something in my form. Ive never done this before but is it possible to add multiple NAMES to forms. For example:
<INPUT TYPE="FILE" NAME="mp3file, name" SIZE="50"> ?
DuFF
Forum Contributor
Posts: 495 Joined: Tue Jun 24, 2003 7:49 pm
Location: USA
Post
by DuFF » Mon Feb 16, 2004 7:28 pm
I don't think so. If you did that you would need to call POST using: $_POST['mp3file, name'].
mikegotnaild
Forum Contributor
Posts: 173 Joined: Sat Feb 14, 2004 5:59 pm
Post
by mikegotnaild » Mon Feb 16, 2004 8:12 pm
So would that work then ? If not. What can i do? In the long run I want to be able to have users submit information to the database and the file uploads than that information in the database is automatically displayed on a custom page for them including download links to their uploaded files. So i dont have to make every custom page for them by hand.
DuFF
Forum Contributor
Posts: 495 Joined: Tue Jun 24, 2003 7:49 pm
Location: USA
Post
by DuFF » Mon Feb 16, 2004 8:57 pm
Whats wrong with what you're doing right now?
mikegotnaild
Forum Contributor
Posts: 173 Joined: Sat Feb 14, 2004 5:59 pm
Post
by mikegotnaild » Mon Feb 16, 2004 9:08 pm
The mp3file and imagefile filenames dont show up in mysql
markl999
DevNet Resident
Posts: 1972 Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)
Post
by markl999 » Mon Feb 16, 2004 9:11 pm
$imagefile = $_POST['imagefile']['name'];
$mp3file = $_POST['mp3file']['name'];
Should be ...
$imagefile = $_FILES['imagefile']['name'];
$mp3file = $_FILES['mp3file']['name'];
?
DuFF
Forum Contributor
Posts: 495 Joined: Tue Jun 24, 2003 7:49 pm
Location: USA
Post
by DuFF » Mon Feb 16, 2004 9:44 pm
Ahhhh yes, I think thats it.
BTW, just because they come up blank is no reason to disband an entire idea. As you have seen it was just a minor error. Probably could have been discovered using some debugging.
(or just by letting mark review your code
)
mikegotnaild
Forum Contributor
Posts: 173 Joined: Sat Feb 14, 2004 5:59 pm
Post
by mikegotnaild » Mon Feb 16, 2004 10:01 pm
THank you ONCE AGAIN! You are a php GOD
mikegotnaild
Forum Contributor
Posts: 173 Joined: Sat Feb 14, 2004 5:59 pm
Post
by mikegotnaild » Mon Feb 16, 2004 10:05 pm
DuFF wrote: Ahhhh yes, I think thats it.
BTW, just because they come up blank is no reason to disband an entire idea. As you have seen it was just a minor error. Probably could have been discovered using some debugging.
(or just by letting mark review your code
)
I lack expirience
mikegotnaild
Forum Contributor
Posts: 173 Joined: Sat Feb 14, 2004 5:59 pm
Post
by mikegotnaild » Mon Feb 16, 2004 10:06 pm
But im gaining it when you guys point out what i did wrong.