|
This topic comprises 3 pages: 1 2 3
|
Author
|
Topic: PHP file upload: place file information into database (HELP)
|
Andrew McCrea
Jedi Master Film Handler
Posts: 645
From: Winnipeg, Manitoba, Canada
Registered: Nov 2000
|
posted 07-10-2008 10:30 PM
Hey everyone!
I'm hoping there's someone out there that's pretty good with PHP that could help me with this script...
This is my script (the action of a form submit) that uploads a file to my server.
quote:
include('config.php');
//Connect to database $cxn = mysqli_connect($host, $username, $password, ********) or die ("No connection can be made."); //Get the parent's ID number from URL. $fID = $_GET['fID']; //Get the variables from our modFile form. $title=$_POST['title']; $desc=$_POST['desc']; $fGenre=$_POST['fGenre']; //Get the user's name for proper credit. $userName=$_SESSION['name'];
// Get the file name and add "m" for MOD file. $file_name = $_FILES['modFile']['name']; $uploadLetter=m;
// Random 7 digit number to add to our file name $random_digit=rand(0000000000,9999999999);
//Here's how we create the new file name. $new_file_name=$uploadLetter.$random_digit.$file_name;
//set where you want to store files $path= "files/fileUploads/mods/".$new_file_name;
if($modFile !=none) {
if(move_uploaded_file($_FILES['modFile']['tmp_name'], $path)) { echo "Successful<BR/>";
echo "File Name :".$new_file_name."<BR/>"; echo "File Size :".$_FILES['modFile']['size']."<BR/>"; echo "File Type :".$_FILES['modFile']['type']."<BR/>"; } else { echo "Error"; } }
mysqli_query($cxn, "INSERT INTO fileAlt (parent, fileName, desc, fileGenre, uploader, filePath) VALUES ('$fID', '$title', '$desc', '$fGenre', '$userName', '$path')") or die ('FAILED.');
The files upload properly, but no information is stored in the database, and I get the die error 'FAILED'.
Could anyone help me correct this script? I'm sure the solution is fairly obvious, but I'm still learning and have tried so many INSERT statement configurations that haven't worked.
Thanks!
| IP: Logged
|
|
Mark J. Marshall
Film God
Posts: 3188
From: New Castle, DE, USA
Registered: Aug 2002
|
posted 07-10-2008 10:51 PM
To get a clue about why a MySQL query is failing, try using this:
die(mysql_error());
The only problem is that you don't want to leave that there because hackers can gain info about your database from the errors. But in cases like this, you can use this to see what MySQL is complaining about.
The other thing you can try is instead of doing this: mysqli_query($cxn, "INSERT INTO fileAlt (parent, fileName, desc, fileGenre, uploader, filePath) VALUES ('$fID', '$title', '$desc', '$fGenre', '$userName', '$path')")
...do this: $query = "INSERT INTO fileAlt (parent, fileName, desc, fileGenre, uploader, filePath) VALUES ('$fID', '$title', '$desc', '$fGenre', '$userName', '$path')"; echo($query); mysqli_query($cxn, $query);
Then you can actually see what PHP is doing to create your query. And if you like, you can copy and paste that query into something like phpMyAdmin manually to see what happens. Again, this is only for debugging purposes. Once you work it out, get rid of the echo() statement.
Sometimes a name like "O'Malley" will screw up your query because of the single quote. If you're running a whole bunch of names, you might not think of something like that at first.
If that doesn't help, let me know what happens and we'll see what else I can think of.
| IP: Logged
|
|
Andrew McCrea
Jedi Master Film Handler
Posts: 645
From: Winnipeg, Manitoba, Canada
Registered: Nov 2000
|
posted 07-10-2008 11:05 PM
I tried the error thing, and received no error output. I then tried mysqli_error and got a "parameter expected 1, null given" type of warning.
Interesting... Here's all the echo output:
quote: Successful File Name :m669703611Triple-star_sunset.jpg File Size :46390 File Type :image/jpeg INSERT INTO fileAlt (parent, fileName, desc, fileGenre, uploader, filePath) VALUES ('1', 'test', 'test', 'test', '', 'files/fileUploads/mods/m669703611Triple-star_sunset.jpg')
Also, I tried running the insert query in phpMyAdmin and I got this:
quote: Error
SQL query:
INSERT INTO fileAlt( parent, fileName, DESC , fileGenre, uploader, filePath ) VALUES ( '1', 'test', 'test', 'test', '', 'files/fileUploads/mods/m669703611Triple-star_sunset.jpg' )
MySQL said: Documentation #1064 - 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 'desc, fileGenre, uploader, filePath) VALUES ('1', 'test', 'test', 'test', '', 'f' at line 1
EDIT: The problem turned out to be that 'DESC' is an obscure keyword in PHP or MySQL, so its all fixed now!
Thanks a billion for the tip because it got me thinking creatively again!
| IP: Logged
|
|
|
|
|
|
|
Andrew McCrea
Jedi Master Film Handler
Posts: 645
From: Winnipeg, Manitoba, Canada
Registered: Nov 2000
|
posted 07-24-2008 02:42 AM
OK, so here's something I really need help on, and I don't think its anything major, I just can't sort through everything on Google to really understand what it is I have to do...
I'm building the profile for the users on my site, and I want to show the user's friends a la facebook, with a table like this:
Now, I know how to display images dynamically (like the person's avatar when profile.php is set to show theirs, etc.
What I don't know is how to fetch 6 random rows from the friends table where they're a fan of the user, and display them in the table where I specify... i.e. $row[1]['avatar'] $row[1]['name'] $row[2]['avatar'] $row[2]['name'] etc.
I'm sure there's a simple way to do this, I just haven't been able to understand it enough yet to really get it with all my trying...
I appreciate any help I can get!
| IP: Logged
|
|
|
Andrew McCrea
Jedi Master Film Handler
Posts: 645
From: Winnipeg, Manitoba, Canada
Registered: Nov 2000
|
posted 07-24-2008 10:35 AM
Yes, I know how to do the grab 6 random and place the echo tag in the image... I've got all of that working on the site so far...
My table looks like this: profileFriends (table name) id, person, friend, fAvatar
When someone adds someone as a friend, the user of the site is placed in "person", the friend's name is placed in "friend", and the friend's avatar path is placed in fAvatar (this is updated by the avatar upload script when the avatar is changed).
So, what I'm really looking to do is build an html table, and then pop the image tags and the link tags for the name in each column and each row, so that it displays 6 random friends in this format.
| IP: Logged
|
|
|
|
|
|
|
All times are Central (GMT -6:00)
|
This topic comprises 3 pages: 1 2 3
|
Powered by Infopop Corporation
UBB.classicTM
6.3.1.2
The Film-Tech Forums are designed for various members related to the cinema industry to express their opinions, viewpoints and testimonials on various products, services and events based upon speculation, personal knowledge and factual information through use, therefore all views represented here allow no liability upon the publishers of this web site and the owners of said views assume no liability for any ill will resulting from these postings. The posts made here are for educational as well as entertainment purposes and as such anyone viewing this portion of the website must accept these views as statements of the author of that opinion
and agrees to release the authors from any and all liability.
|