Jump to content

[SOLVED] PHP mySQL Upload Script Error


BubbaQ

Recommended Posts

I am trying to upload files using PHP into a mySQL database and I have the following code in an upload.php file that is called by an html form.

 

<?php 
mysql_connect("localhost","fw_admin","mypasswordhere") or die ('Error connecting to mysql database'); 
mysql_select_db("uploads"); 
$data = addslashes(fread(fopen($form_data, "r"), filesize($form_data))); 
$result=MYSQL_QUERY("INSERT INTO uploads (description, data, filename, filesize, filetype) ". "VALUES ('$form_description', '$data', '$form_data_name', '$form_data_size', '$form_data_type')") or die('Error, query failed'); 
$id= mysql_insert_id(); 
print "<p>File ID: <b>$id</b><br>"; 
print "<p>File Name: <b>$form_data_name</b><br>"; 
print "<p>File Size: <b>$form_data_size</b><br>"; 
print "<p>File Type: <b>$form_data_type</b><p>"; 
print "To upload another file <a href=http://workspace.mydomain.org/admin> Click Here</a>"; 
?> 

 

When I submit the form, it evidently connects to the db fine, but returns "Error, query failed", so it is dying on the mySQL query. But I can't figure out why.

 

I have double-checked the database field names, and when I take out the error trapping in the query it posts the "print" statements correctly with file size so it seems to read teh file correctly, etc., but nothing is added to the db...

 

Questions or Suggestions???

 

Thanks,

 

BubbaQ

Link to comment
Share on other sites

lets try this and see if we produce some errors

<?php 
mysql_connect("localhost","fw_admin","mypasswordhere") or die ('Error connecting to mysql database'); 
mysql_select_db("uploads"); 
$data = addslashes(fread(fopen($form_data, "r"), filesize($form_data))); 
$q = "INSERT INTO `uploads` (`description`, `data`, `filename`, `filesize`, `filetype`) VALUES('".$form_description."', '".$data."', '".$form_data_name."', '".$form_data_size."', '".$form_data_type."')";
$result = mysql_query($q) or die(mysql_error()."<br />".$q);
$id= mysql_insert_id(); 
print "<p>File ID: <b>$id</b><br>"; 
print "<p>File Name: <b>$form_data_name</b><br>"; 
print "<p>File Size: <b>$form_data_size</b><br>"; 
print "<p>File Type: <b>$form_data_type</b><p>"; 
print "To upload another file <a href=http://workspace.mydomain.org/admin> Click Here</a>"; 
?> 

 

If it errors report back the error

Link to comment
Share on other sites

Grant

Thanks for the suggestion, but I know I have the permissions and access needed.

 

Cooldude & Chigley

When I make your changes, I get a "No database Selected" Error... though I thought I had that syntax correct.

 

Pancake

Here is the form code that calls the upload.php file. The form_description is a field in the form that I also want added to each entry in the db.

 

<form method="post" action="upload.php" enctype="multipart/form-data"> 
Description:<br> 
<input type="text" name="form_description" size="40"> 
<input type="hidden" name="MAX_FILE_SIZE" value="2000000"> 
<br>File to upload:<br> 
<input type="file" name="form_data" size="40"> 
<p><input type="submit" name="submit" value="submit"> 
</form> 

 

Thanks for your help. Evidently I am connecting to the database server but not selecting a db somehow.

 

What now?

 

BubbaQ

Link to comment
Share on other sites

I got it...

 

I noticed that somehow I had put the name of the table in the mysql_db_select string.

 

"uploads" is the name of the table, but not the name of the db itself.

 

Thanks for the help guys... Problem solved.

 

Now I just have to create the page to list out the files so people can dload them.

 

Thanks again!

 

BubbaQ

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.