Reply
upload mp3 file and info into database how??
Old 08-21-2004, 07:06 PM upload mp3 file and info into database how??
Experienced Talker

Posts: 34
I create a datebase named "mp3". Know I want to create a table called "contents" that will consist of

ID Primary Key
Song Name
Artist
Release Date
Additional Notes
File {contain mp3 file}

I will collect all this info through a PHP form. How do I load all this info into the table called "contents" and can you load mp3 files into an mysql database??

I have never dealt with MP3 file and MYSQL so I need some guidance

this is the PHP form so far


print<<<_HTML_
<table>
<form action="upload.php" method="post" enctype="multipart/form-data">

<tr><td><b>MP3 Info:</b></td></tr>
<tr><td>Song Name:</td><td><input type="text" name="name"></td></tr>
<tr><td>Artist: </td><td><input type="text" name="artist"></td></tr>
<tr><td>Release Date: </td><td> <input type="text" name="release_date"></td></tr>
<tr><td>Additional Notes</td></tr>
<tr><td></td><td><TEXTAREA NAME="note" COLS=40 ROWS=6></TEXTAREA></td></tr>
<tr><td>Select a File to Upload:</td>
<td><input type="file" name="mp3_file">
<input type="Submit" value="Upload File"></td></tr>

</form>
</table>
cedtech23 is offline
Reply With Quote
View Public Profile
 
When You Register, These Ads Go Away!
     
Old 08-21-2004, 07:51 PM
Unknown.

Posts: 1,692
To insert data into a table in mysql use this...

PHP Code:
$sql mysql_query("INSERT INTO tablename(column1, column2) VALUES ('$value1', '$value2')") or die (mysql_error()); 
You cant put files into mysql... You can only insert values..

So you would have to upload the file to the server and then add the path to the file into the mysql table

-James
Dark-Skys99 is offline
Reply With Quote
View Public Profile
 
Old 08-21-2004, 08:02 PM
Anacrusis's Avatar
Defies a Status

Posts: 2,099
Name: Adam
Location: Colchester CT
You can actually store files in a mySQL database by way of the BLOB dataype, although it's not reccomended.

As james said, the best way is to upload the file into a directory and store the path to that file in the database.
Anacrusis is offline
Reply With Quote
View Public Profile Visit Anacrusis's homepage!
 
Old 08-21-2004, 08:12 PM path location and type
Experienced Talker

Posts: 34
okay

so I would first create the table like this


$sql = CREATE TABLE mp3_files (
id int NOT NULL AUTO_INCREMENT Primary Key,
Song Name VARCHAR,
Artist Artist VARCHAR ,
Release_Date date,
Additional Notes VARCHAR,
Path ?? );


where "Path" would it me VARCHAR type?? and how would I store the absolute path of the file using PHP

Lets say I had a folder called MP3_Files on my server that would contain all my mp3 files and I uploaded an mp3 song called big poppa? How would I store this location so that when I go to retrieve this file it know the exact one??


Newbie






cedtech23 is offline
Reply With Quote
View Public Profile
 
Old 08-21-2004, 08:31 PM
Unknown.

Posts: 1,692
Yea just make the Path field VARCHAR

You could just store the path as a relative url to the script...

so like /MP3_files/filename.mp3

Another way you would do it is have a php upload script which uploads the file and adds all the information to the database aswell and adds the path automatically...

Check this tutorial...
http://www.phpfreaks.com/quickcode/F...loader/152.php

If you want I could always help with the upload coding if needed

-James
Dark-Skys99 is offline
Reply With Quote
View Public Profile
 
Old 08-21-2004, 09:12 PM
Experienced Talker

Posts: 34
thanks dark-skys99 but this is for a class and I need know how this works by creating it. My teacher made it clear that he will test us to make sure we create the program:

I decided to use move_uploaded_file function to move the file to the "mp3_files" directory but I am lost with what the first part on the fuction call should be. I mean the part I labeled ??? can someone give me some input. hear is the file below


<?php

if ($action == "load") {

$folder = "mp3_files";
move_uploaded_file(???,"$folder" );

}


?>

<html>
<head>
<title></title>
</head>
<body>
<table>
<form action="upload.php" method="post" enctype="multipart/form-data">

<tr><td><b>MP3 Info:</b></td></tr>
<tr><td>Song Name:</td><td><input type="text" name="name"></td></tr>
<tr><td>Artist: </td><td><input type="text" name="artist"></td></tr>
<tr><td>Release Date: </td><td> <input type="text" name="release_date"></td></tr>
<tr><td>Additional Notes</td></tr>
<tr><td></td><td><TEXTAREA NAME="note" COLS=40 ROWS=6></TEXTAREA></td></tr>
<tr><td>Select a File to Upload:</td>
<td><input type="file" name="mp3_file">
<input type="Submit" name=action value="Load"></td></tr>
</form>
</table>
</body>
</html>
cedtech23 is offline
Reply With Quote
View Public Profile
 
Old 08-22-2004, 12:01 AM
Experienced Talker

Posts: 34
I am trying to use the move_uploaded_file function to copy a file from my hard drive and move it into a directory on the server. The first agrument that I used with move_uploaded_file function is $uploadedfile because in my form I have the following <input type = "file" name = "uploadedfile"> the second argument is $folder because this variable contain the location of the directory that I want to save the file to

$folder = "C:\Program Files\Apache Group\Apache2\htdocs\mp3_files";

When I run a test of uploading a file and copying it to directory I get the following errors


Warning: move_uploaded_file(C:\Program Files\Apache Group\Apache2\htdocs\mp3_files) [function.move-uploaded-file]: failed to open stream: Permission denied in C:\Program Files\Apache Group\Apache2\htdocs\upload.php on line 6

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move 'C:\WINDOWS\TEMP\php4.tmp' to 'C:\Program Files\Apache Group\Apache2\htdocs\mp3_files' in C:\Program Files\Apache Group\Apache2\htdocs\upload.php on line 6


what am I doing wrong??? How can I correct the problem??




?php

if ($_POST['action']) {

$folder = "C:\Program Files\Apache Group\Apache2\htdocs\mp3_files";
move_uploaded_file($uploadedfile, $folder);

}


?>

<html>
<head>
<title></title>
</head>
<body>
<table>
<form action="upload.php" method="post" enctype="multipart/form-data">

<tr><td><b>MP3 Info:</b></td></tr>
<tr><td>Song Name:</td><td><input type="text" name="name"></td></tr>
<tr><td>Artist: </td><td><input type="text" name="artist"></td></tr>
<tr><td>Release Date: </td><td> <input type="text" name="release_date"></td></tr>
<tr><td>Additional Notes</td></tr>
<tr><td></td><td><TEXTAREA NAME="note" COLS=40 ROWS=6></TEXTAREA></td></tr>
<tr><td>Select a File to Upload:</td>
<td><input type = "file" name = "uploadedfile">
<input type="Submit" name="submit" value="UpLoad">
<input type="hidden" name="action" value=1>
</td></tr>
</form>
</table>
</body>
</html>
cedtech23 is offline
Reply With Quote
View Public Profile
 
Old 08-22-2004, 03:05 PM Yea upload ur file in MySQL here is source
we4tech's Avatar
Junior Talker

Posts: 2
although it is not recommended

but just ur interest u can use this code:

firstly u need to create a table like this:

CREATE TABLE MP3 (
MP3_NAME TEXT,
MP3_DATA LONGBLOB,
);

HTML Code:
<html>
<head><title>Just test </title></head>
<body>
<!--html file-->
<!/**
    * Here your form items
    */-->
<form action="action_handler.php?id=save_mp3" method="post" enc="mulitpart/formdata">

Enter name <input type="text" name="txtName" />
Enter Mp3 file<input type="file" name="fileMp3" />
<input type="submit" value="Save" />
</form>
<!--html end-->
</body>
</html>
here ur php code

PHP Code:
# file name: action_handler.php
<?php
/**
 * Functions
 */
 /**
   * initialize ur Database connection
   * think ur database connection is $getConn
   * string
   */

function saveMp3($getConn) {
   
$getName=$_POST["txtName"];
   
$getFile=$_FILES["fileMp3"];
   
   
$getTmpName=$getFile["tmp_name"];
   
   
$fOpen=fopen($getTmpName,"r");
   
$getData=fread($fOpen,filesize($getTmpName));
   
fclose($fOpen);

   
$getData=addslashes($getData);

   
$bSQL="INSERT INTO MP3 VALUES ('$getName', '$getData')";
   
mysql_query($bSQL) or die("Error to insert new data");
     
}

/**
 * listener
 */
$getID=$_GET["id"];

/*
 * action
 */

switch($getID) {
  case 
"save_mp3":
     
saveMp3($getConn);
     break;
}
?>

I think it will help

SEND UR FEEDBACK to
NHM Tanveer hossain khan (hasan)
hasan83bd@yahoo.com
http://we4tech.com
we4tech is offline
Reply With Quote
View Public Profile
 
Old 08-22-2004, 11:23 PM $sql="CREATE TABLE syntax error
Experienced Talker

Posts: 34
Thanks for the input , I am going to go with having the mp3 stored in a directory and then copying the file path to the database


I create this file listed below for the database but I am getting this error when I run it:

Can't create mp3_files table You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'Name VARCHAR, Artist VARCHAR , Release_Date date, Additional

The syntax looks good to me does someone see something that I am missing??


<?php

$host= "localhost";
$dbuser= "unsernamer";
$dbpass= "password";
$database= "mp3_db";
$table= "mp3_files";


$link= @mysql_connect($host, $dbuser, $dbpass)
or die ('I cannot connect to the database because: ' . mysql_error());



if (! @mysql_select_db($database,$link)){

mysql_query("CREATE DATABASE $database");

}
else
{
$sql="CREATE TABLE $table (
id int NOT NULL AUTO_INCREMENT Primary Key,
Song Name VARCHAR,
Artist VARCHAR ,
Release_Date date,
Additional_Notes VARCHAR,
Path VARCHAR )";

$query1 = mysql_query($sql);


if ($query1){
print "<p>$table table created</p>";
} else {
$error = 1;
echo("<p>Can't create $table table ". mysql_error() ."<p>");
}
cedtech23 is offline
Reply With Quote
View Public Profile
 
Old 08-22-2004, 11:43 PM
Unknown.

Posts: 1,692
Change it to this and it should work...

PHP Code:
<?php

$host
"localhost";
$dbuser"unsernamer";
$dbpass"password";
$database"mp3_db";
$table"mp3_files";

$link= @mysql_connect($host$dbuser$dbpass)
or die (
'I cannot connect to the database because: ' mysql_error());


if (! @
mysql_select_db($database,$link)){

mysql_query("CREATE DATABASE $database");

}
else
{
$sql="CREATE TABLE `$table` (
`id` INT NOT NULL AUTO_INCREMENT ,
`Song_Name` VARCHAR(255) NOT NULL ,
`Artist` VARCHAR(255) NOT NULL ,
`Release_Date` DATE NOT NULL ,
`Additional_Notes` VARCHAR(255) NOT NULL ,
`Path` VARCHAR(255) NOT NULL ,
PRIMARY KEY (`id`) 
)" 
;

$query1 mysql_query($sql);


if (
$query1) {
print 
"<p>$table table created</p>";
} else {
$error 1;
echo(
"<p>Can't create $table table "mysql_error() ."<p>");

}
?>
It was because of the SQL query... To work correctly it needed to be set out like..

$sql="CREATE TABLE `$table` (
`id` INT NOT NULL AUTO_INCREMENT ,
`Song_Name` VARCHAR(255) NOT NULL ,
`Artist` VARCHAR(255) NOT NULL ,
`Release_Date` DATE NOT NULL ,
`Additional_Notes` VARCHAR(255) NOT NULL ,
`Path` VARCHAR(255) NOT NULL ,
PRIMARY KEY (`id`)
)" ;

Hope this helps

-James
Dark-Skys99 is offline
Reply With Quote
View Public Profile
 
Old 08-24-2004, 07:40 AM Problem inserting info into database
Experienced Talker

Posts: 34
I have two php files one check to see if a database and table is present if it's not it create both (check.php). The other collects info from a form and then inputs that data to the database and copy the mp3 file to a location on my computer(masterupload.php).

So far the only to things I know thats working is that it will create a database or table if one is not present {check.php files does that} and that it will copy the file to a location on the server {masterupload.php does that} But the problem I am having is that it won't copy info from the form to the table here are the two php files in question:


Why am I unable to write info from the form to the mysql table???


this is the masterupload.php whick includes an call to the check.php file via include()

<?php


if ($_POST['action']) {

/*$dir = "/home/nucity44/public_html/mp3_files/";*/
$dir = "C:\Program Files\Apache Group\Apache2\htdocs\mp3_files\\";

$uploadfile = $dir . $_FILES['mp3_file']['name'];

move_uploaded_file($_FILES['mp3_file']['tmp_name'], $uploadfile);

include 'check.php';

$sql = "INSERT INTO $table (Song_Name, Artist, Release_Date, Additional_Notes, Path )
VALUES ($_POST[name], $_POST[artist], $_POST[date], $_POST[notes],$uploadfile)";

mysql_query($sql);
}
?>


check.php

<?php

$host= "localhost";
$dbuser= "usernamer";
$dbpass= "password";
$database= "mp3_db";
$table= "mp3_files";


$link= @mysql_connect($host, $dbuser, $dbpass)
or die ('I cannot connect to the database because: ' . mysql_error());


if (! @mysql_select_db($database,$link)){

mysql_query("CREATE DATABASE $database");

}
else
{

if(! @mysql_query("SELECT * FROM `$table` LIMIT 0,1")){


$sql="CREATE TABLE $table (
id INT NOT NULL auto_increment ,
Song_Name VARCHAR(255) NOT NULL ,
Artist VARCHAR(255) NOT NULL ,
Release_Date DATE NOT NULL ,
Additional_Notes VARCHAR(255) NOT NULL ,
Path VARCHAR(255) NOT NULL ,
PRIMARY KEY (id)
)" ;

mysql_query($sql);
}
}
?>
cedtech23 is offline
Reply With Quote
View Public Profile
 
Old 03-20-2008, 06:39 PM Re: upload mp3 file and info into database how??
lighthugger's Avatar
Novice Talker

Posts: 5
Name: Greasy Fish
Iv used a variation of this code to generate a similar database. My question is how do i actually write the form to write to this database?
__________________
www.greasyfringe.com
lighthugger is offline
Reply With Quote
View Public Profile Visit lighthugger's homepage!
 
Old 03-21-2008, 04:07 AM Re: upload mp3 file and info into database how??
Ultra Talker

Posts: 308
Forms automatically don't write data into database, forms are used to collect data and then you use php to process that data and store it into your db.
__________________
tiny url
dman_2007 is offline
Reply With Quote
View Public Profile Visit dman_2007's homepage!
 
Old 03-21-2008, 12:46 PM Re: upload mp3 file and info into database how??
lighthugger's Avatar
Novice Talker

Posts: 5
Name: Greasy Fish
Right so would I be right in saying that I just need to connect a form upto the code I already have and that should be that?
__________________
www.greasyfringe.com
lighthugger is offline
Reply With Quote
View Public Profile Visit lighthugger's homepage!
 
Old 03-22-2008, 03:41 AM Re: upload mp3 file and info into database how??
Ultra Talker

Posts: 308
yes, that is right.
__________________
tiny url
dman_2007 is offline
Reply With Quote
View Public Profile Visit dman_2007's homepage!
 
Old 03-25-2008, 12:57 PM Re: upload mp3 file and info into database how??
lighthugger's Avatar
Novice Talker

Posts: 5
Name: Greasy Fish
This is what iv put together so far, unfortunatly it doesnt quite work.

<!-- HTML -->
<form enctype="multipart/form-data" action="masterupload.php">

<?php
$Mix_Namei = getenv("Mix_Name");
$DJi = getenv ("DJ");
$Additional_notesi = getenv ("Additional_Notes");
?>
<input type="hidden" name="Mix Name" value="<?php echo $Mix_Namei ?>" />
<input type="hidden" name="DJ Name" value="<?php echo $DJi ?>" />
<input type="hidden" name="Mix Discription" value="<?php echo $Additional_Notesi ?>" />

DJ Name <br />
<input type="text" name="DJ Name" size="35" />
<br />
Mix Name<br />
<input type="text" name="Mix Name" size="35" />
<br />
<br />
Mix Discription
<br />
<textarea name="Mix Discription" rows="4" cols="40"></textarea>
<br />
<br /><input type="hidden" name="MAX_FILE_SIZE" value="100000000" />
Choose a file to upload:<br />
<input name="uploadedfile" type="file" />
<br />
<br />
<input type="submit" value="submit" />
<br />
</form>

<!-- check.php -->
<?php
$host= "";
$dbuser= "";
$dbpass= "";
$database= "";
$table= "mp3_files";

$link= @mysql_connect($host, $dbuser, $dbpass)
or die ('I cannot connect to the database because: ' . mysql_error());

if (! @mysql_select_db($database,$link)){
mysql_query("CREATE DATABASE $database");
}
else
{
if(! @mysql_query("SELECT * FROM `$table` LIMIT 0,1")){

$sql="CREATE TABLE $table (
id INT NOT NULL auto_increment ,
Mix_Name VARCHAR(255) NOT NULL ,
DJ VARCHAR(255) NOT NULL ,
Additional_Notes VARCHAR(50) NOT NULL ,
Path VARCHAR(255) NOT NULL ,
PRIMARY KEY (id)
)" ;
mysql_query($sql);
}
}
?>


<!-- masterupload.php -->
<?PHP

if ($_POST['action']) {
$dir = "http://www.";
$uploadfile = $dir . $_FILES['mp3_file']['name'];
move_uploaded_file($_FILES['mp3_file']['tmp_name'], $uploadfile);
include 'check.php';
$sql = "INSERT INTO $table (Mix_Name, DJ, Additional_Notes, Path )
VALUES ($_POST[name], $_POST[DJ], $_POST[notes],$uploadfile)";
mysql_query($sql);
}
?>
__________________
www.greasyfringe.com
lighthugger is offline
Reply With Quote
View Public Profile Visit lighthugger's homepage!
 
Old 04-07-2008, 11:07 AM Re: upload mp3 file and info into database how??
lighthugger's Avatar
Novice Talker

Posts: 5
Name: Greasy Fish
I v made some changes, heres my code at the mo. When i hit submit the url changes to incorporate the information but nothing actually happens. Dam this is a tricky one...


masterupload.php

<DIV style="MARGIN: 5px 20px 20px"><DIV class=smallfont style="MARGIN-BOTTOM: 2px">
PHP Code:
<?PHPif ($_SERVER['REQUEST_METHOD'] == "POST") {$dir "/mp3_files/";$uploadfile $dir $_FILES['mp3_file']['name'];move_uploaded_file($_FILES['mp3_file']['tmp_name'], $uploadfile);include 'check.php';$sql "INSERT INTO $table (Mix_Name, DJ, Additional_Notes, Path )VALUES ($_POST[name], $_POST[DJ], $_POST[notes],$uploadfile)";mysql_query($sql);}?>

check.php
PHP Code:


<?php $host""$dbuser""$dbpass""$database""$table"mp3_files"$link= @mysql_connect($host$dbuser$dbpass)or die ('I cannot connect to the database because: ' mysql_error());if (! @mysql_select_db($database,$link)){mysql_query("CREATE DATABASE $database");}else{ if(! @mysql_query("SELECT * FROM `$table` LIMIT 0,1")){$sql="CREATE TABLE $table (id INT NOT NULL auto_increment ,Mix_Name VARCHAR(255) NOT NULL ,DJ VARCHAR(255) NOT NULL ,Additional_Notes VARCHAR(50) NOT NULL ,Path VARCHAR(255) NOT NULL ,PRIMARY KEY (id))" ;mysql_query($sql);}}?>
html upload page code
HTML Code: