Page 1 of 1

How do i do this..?

Posted: Mon Feb 16, 2004 5:32 pm
by mikegotnaild
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&#1111;'band_name'];
$description = $_POST&#1111;'description'];
$history = $_POST&#1111;'history'];
$influences = $_POST&#1111;'influences'];
$genra = $_POST&#1111;'genra'];
$email = $_POST&#1111;'email'];
$website = $_POST&#1111;'website'];
$imagefile = $_POST&#1111;'imagefile']&#1111;'name'];
$mp3file = $_POST&#1111;'mp3file']&#1111;'name'];
    
   //This is the File upload part
   $uploaddir = $_SERVER&#1111;'DOCUMENT_ROOT'].'/localmm/upload/';
   $uploadfile1 = $uploaddir . $_FILES&#1111;'imagefile']&#1111;'name']; 
   $uploadfile2 = $uploaddir . $_FILES&#1111;'mp3file']&#1111;'name'];
   
   
   if (!move_uploaded_file($_FILES&#1111;'imagefile']&#1111;'tmp_name'], $uploadfile1)) &#123; 
       print "ERROR: File is invalid"; 
       print_r($_FILES); 
   &#125; 

   if (!move_uploaded_file($_FILES&#1111;'mp3file']&#1111;'tmp_name'], $uploadfile2)) &#123; 
       print "ERROR: File is invalid"; 
       print_r($_FILES); 
   &#125;

$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) &#123;
header("Location: http://naild.com/localmm/modules.php?name=Band_List&file=sent");
&#125; else &#123;
echo("An error has occured");
&#125;

MySQL_close()
?>

Posted: Mon Feb 16, 2004 5:37 pm
by mikegotnaild
I probably did something stupid :/.

Posted: Mon Feb 16, 2004 5:42 pm
by DuFF
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.

Posted: Mon Feb 16, 2004 5:46 pm
by mikegotnaild
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"> ?

Posted: Mon Feb 16, 2004 7:28 pm
by DuFF
I don't think so. If you did that you would need to call POST using: $_POST['mp3file, name'].

Posted: Mon Feb 16, 2004 8:12 pm
by mikegotnaild
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.

Posted: Mon Feb 16, 2004 8:57 pm
by DuFF
Whats wrong with what you're doing right now?

Posted: Mon Feb 16, 2004 9:08 pm
by mikegotnaild
The mp3file and imagefile filenames dont show up in mysql

Posted: Mon Feb 16, 2004 9:11 pm
by markl999
$imagefile = $_POST['imagefile']['name'];
$mp3file = $_POST['mp3file']['name'];
Should be ...
$imagefile = $_FILES['imagefile']['name'];
$mp3file = $_FILES['mp3file']['name'];

?

Posted: Mon Feb 16, 2004 9:44 pm
by DuFF
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 :D )

Posted: Mon Feb 16, 2004 10:01 pm
by mikegotnaild
THank you ONCE AGAIN! You are a php GOD

Posted: Mon Feb 16, 2004 10:05 pm
by mikegotnaild
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 :D )
I lack expirience :) :(

Posted: Mon Feb 16, 2004 10:06 pm
by mikegotnaild
But im gaining it when you guys point out what i did wrong. :)