BubbaQ Posted December 20, 2007 Share Posted December 20, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/82535-solved-php-mysql-upload-script-error/ Share on other sites More sharing options...
chigley Posted December 20, 2007 Share Posted December 20, 2007 or die(mysql_error()); ^^ Use that instead of 'Error, query failed' Quote Link to comment https://forums.phpfreaks.com/topic/82535-solved-php-mysql-upload-script-error/#findComment-419573 Share on other sites More sharing options...
Pancake Posted December 20, 2007 Share Posted December 20, 2007 VALUES ('$form_description', '$data', '$form_data_name', '$form_data_size', '$form_data_type')") I'm assuming that $form_description and such have been defined earlier? Quote Link to comment https://forums.phpfreaks.com/topic/82535-solved-php-mysql-upload-script-error/#findComment-419578 Share on other sites More sharing options...
cooldude832 Posted December 20, 2007 Share Posted December 20, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/82535-solved-php-mysql-upload-script-error/#findComment-419601 Share on other sites More sharing options...
Grant Holmes Posted December 21, 2007 Share Posted December 21, 2007 Bubba, I am a PHP/MySQL noobie, but I have one DB on Yahoo and they don't allow ANY imports other than from within PHPmyAdmin. Not that I've gotten THAT to work either, but make sure your server even allows it? Quote Link to comment https://forums.phpfreaks.com/topic/82535-solved-php-mysql-upload-script-error/#findComment-420450 Share on other sites More sharing options...
BubbaQ Posted December 21, 2007 Author Share Posted December 21, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/82535-solved-php-mysql-upload-script-error/#findComment-420500 Share on other sites More sharing options...
BubbaQ Posted December 21, 2007 Author Share Posted December 21, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/82535-solved-php-mysql-upload-script-error/#findComment-420504 Share on other sites More sharing options...
chigley Posted December 21, 2007 Share Posted December 21, 2007 mysql_select_db("uploads") or die(mysql_error()); ^^ Use that - for whatever reason the DB isn't being selected Quote Link to comment https://forums.phpfreaks.com/topic/82535-solved-php-mysql-upload-script-error/#findComment-420506 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.