...

View Full Version : Why Doesn't This Work?



Cerby
11-14-2007, 03:14 PM
Hi. The following code, is my attempt of uploading an MP3 file to the server, and then storing the path of the file inside a database. Why doesn't the line of code shown in bold work?


if (($_FILES["file"]["type"] == "audio/mpeg3"))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];

$sql="INSERT INTO Music (Path)
VALUES
('"upload/" . $_FILES["file"]["name"]')";
}
}
}
else
{
echo "Invalid file";
}

CFMaBiSmAd
11-14-2007, 03:29 PM
All you are doing is setting a variable named $sql. The code is not executing any database query. Reading this might help - http://php.net/mysql_query

Cerby
11-14-2007, 03:32 PM
It works fine when changing the following code from:

this:


if (($_FILES["file"]["type"] == "audio/mpeg3"))

to this:


if (($_FILES["file"]["type"] == "image/gif"))

CFMaBiSmAd
11-14-2007, 04:04 PM
Why doesn't the line of code shown in bold work?LOL. That is kind of funny in a sad sort of way. :eek:

There is a difference between a line of code not working and the block of code it is present in not executing because a conditional statement is false and is skipping over it. I am guessing that you were getting the "Invalid file" output? If that was the case, you should have stated so in your post.

Using a programming language requires that you have a basic understanding of what the code means and is doing. Changing the conditional test and having the code start "working" would indicate that the upload ['type'] element is different than what you expect and different than what the conditional statement was comparing it with. Echo out $_FILES["file"]["type"] to see what it actually contains.

Edit: In your previous thread on the same subject http://www.codingforums.com/showthread.php?t=126043, you were also told how to determine and debug the ['type'] information -

Also, when an invalid file type is detected, the code should display the type information it did receive in the ['type'] element so that you have some debugging information.

Inigoesdr
11-14-2007, 07:46 PM
You're missing a concatenation operator(.) after $_FILES["file"]["name"] in your query too.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum