/    Sign up×
Community /Pin to ProfileBookmark

Place file information into database (HELP)

Hello everyone!

I’m looking for a simple bit of help…

This is my script (the action of a form submit) that uploads a file to my server.

[code=php]<?php
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.
$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.’);
?>[/code]

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!

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@nebchill26Jul 11.2008 — replace your or die statement with this and see whats your error

[CODE]
or die ('MySQL Error:'.mysql_error());
[/CODE]
Copy linkTweet thisAlerts:
@cktsauthorJul 11.2008 — All I get is:

"Successful

File Name :m858397972Glieseupdated.jpg

File Size :87457

File Type :image/jpeg

MySQL Error:"

If I set it to mysqli_error, I get a "parameter expects 1, null given" type of warning.
Copy linkTweet thisAlerts:
@nebchill26Jul 11.2008 — oh sorry my bad,i read it as mysql not mysqli,im getting dizzy here,but anyways,check your variables
[CODE]
//Get the parent's ID number.
$fID = $_GET['fID'];
//Get the variables from our modFile form.
$title=$_POST['title'];
[/CODE]


try replacing POST and GET with REQUEST
Copy linkTweet thisAlerts:
@cktsauthorJul 11.2008 — That didn't work either. Thanks for the help!

Anyone else have any ideas?
Copy linkTweet thisAlerts:
@cktsauthorJul 11.2008 — Is this script really that bad?! Is it really that hard to insert some details, like the file path into the database?

I'm not trying to sound rude, but I really, really need some direction on this...

Thanks!
Copy linkTweet thisAlerts:
@callumdJul 11.2008 — Get rid of your die statement all together, so that the system throws a proper error.

Post the error in full here (should include a line number, a value it doesn't understand, etc) and it will give us a clue.

I suspect the problem lies in your SQL query. Have you properly escaped every value that you want to insert in to the database, using the function [b]mysqli_real_escape_string()[/b]?

If the error lies in your SQL syntax, we may not be able to detect it without knowing your database structure.
×

Success!

Help @ckts spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...