unknown101 Posted January 8, 2008 Share Posted January 8, 2008 Hi Guys, Im currently trying to create a basic web interface from which you can select a file from your hard disk and upload to a mysql database. This is the form: uploadafile1.php <form method="post" enctype="multipart/form-data" action="uploadfile.php"> <table width="350" border="0" cellpadding="1" cellspacing="1" class="box"> <tr> <td width="246"> <input type="hidden" name="MAX_FILE_SIZE" value="2000000"> <input name="userfile" type="file" id="userfile"> </td> <td width="80"><input name="upload" type="submit" class="box" id="upload" value=" Upload "></td> </tr> </table> </form> The php is: uploadfile.php <?php error_reporting(E_ALL); include 'connection.php'; if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0) { $fileName = $_FILES['userfile']['name']; $tmpName = $_FILES['userfile']['tmp_name']; $fileSize = $_FILES['userfile']['size']; $fileType = $_FILES['userfile']['type']; $fp = fopen($tmpName, 'r'); $content = fread($fp, filesize($tmpName)); $content = addslashes($content); fclose($fp); echo 'Content has been Gathered-1'; if(!get_magic_quotes_gpc()) { $fileName = addslashes($fileName); } echo 'All file information has been sucessfully collected-2'; $mysql_conn = @mysql_connect("localhost", "$db_user", "$db_pass"); if(isset($mysql_conn)) { echo 'Sorry there has been an error -3'; exit; } mysql_select_db('xxxx'); $query = "INSERT INTO upload (name, size, type, content ) ". "VALUES ('$fileName', '$fileSize', '$fileType', '$content')"; mysql_query($query) or die('Error, query failed'); echo "<br>File $fileName uploaded<br>"; } ?> I hit the upload button and i get to the error: "echo 'Sorry there has been an error -3'" I have double checked the connection.php file and the sql connection details all of which are fine. Can anyone see why im getting this problem? Many thanks Quote Link to comment https://forums.phpfreaks.com/topic/85042-uploading-to-sql/ Share on other sites More sharing options...
adam291086 Posted January 8, 2008 Share Posted January 8, 2008 it beacause your say if the connection is true then echo You need if(!isset($mysql_conn)) Quote Link to comment https://forums.phpfreaks.com/topic/85042-uploading-to-sql/#findComment-433681 Share on other sites More sharing options...
unknown101 Posted January 8, 2008 Author Share Posted January 8, 2008 oh dear i should have spotted that sorry, my bad. Thanks for that. Sadly its still not working though. Now when i hit the upload button i get the error "'Error, query failed' I have double checked the query and ensured all field names match, but still no change. Could anyone suggestion why I may be getting this error? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/85042-uploading-to-sql/#findComment-433848 Share on other sites More sharing options...
nikefido Posted January 8, 2008 Share Posted January 8, 2008 oh dear i should have spotted that sorry, my bad. Thanks for that. Sadly its still not working though. Now when i hit the upload button i get the error "'Error, query failed' I have double checked the query and ensured all field names match, but still no change. Could anyone suggestion why I may be getting this error? Thanks Looks like there's an error with teh query from this line: mysql_query($query) or die('Error, query failed'); change it to mysql_query($query) or die('Error, query failed ' . mysql_error()); and run the script again hopefully it will give you more clues as to where the problem is. ============= additionally, just so theres no confusion, i would change your query from $query = "INSERT INTO upload (name, size, type, content ) ". "VALUES ('$fileName', '$fileSize', '$fileType', '$content')"; to $query = "INSERT INTO upload (name, size, type, content ) ". "VALUES ('{$fileName}', '{$fileSize}', '{$fileType}', '{$content}')"; or $query = "INSERT INTO upload (name, size, type, content ) ". "VALUES ('" . $fileName . "', '" . $fileSize . "', '" . $fileType . "', '" . $content . "')"; This is just to be sure that the query is not reading the string "$filename" etc rather than the data in the php variable. Quote Link to comment https://forums.phpfreaks.com/topic/85042-uploading-to-sql/#findComment-433851 Share on other sites More sharing options...
revraz Posted January 8, 2008 Share Posted January 8, 2008 Probably should change that to mysql_error() instead of mysqli if that doesn't work. Quote Link to comment https://forums.phpfreaks.com/topic/85042-uploading-to-sql/#findComment-433853 Share on other sites More sharing options...
nikefido Posted January 8, 2008 Share Posted January 8, 2008 Probably should change that to mysql_error() instead of mysqli if that doesn't work. yes, def. typo is the result of habit from typing "mysqli" lately so much if you use mysqli, you need to supply the $link also as a parameter of "mysqli_error()" (I fixed the typo in my original reply) Quote Link to comment https://forums.phpfreaks.com/topic/85042-uploading-to-sql/#findComment-433854 Share on other sites More sharing options...
revraz Posted January 8, 2008 Share Posted January 8, 2008 Single quote around the variable will work fine in a SQL statement. Make sure the DB types match the type of data being inserted, but the mysql_error should tell you that now. Quote Link to comment https://forums.phpfreaks.com/topic/85042-uploading-to-sql/#findComment-433867 Share on other sites More sharing options...
unknown101 Posted January 8, 2008 Author Share Posted January 8, 2008 Thanks for the quick replies guys, still got some problems tho=( I have added the mysql_error() in and am now getting the following problem: "Error, query failed Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock'(2) Just so you know the SQL is below for how i created the table: CREATE TABLE upload ( id INT NOT NULL AUTO_INCREMENT, name VARCHAR(30) NOT NULL, type VARCHAR(30) NOT NULL, size INT NOT NULL, content MEDIUMBLOB NOT NULL, PRIMARY KEY(id) ); Regards Quote Link to comment https://forums.phpfreaks.com/topic/85042-uploading-to-sql/#findComment-433882 Share on other sites More sharing options...
revraz Posted January 8, 2008 Share Posted January 8, 2008 Something is wrong in your connection script. Quote Link to comment https://forums.phpfreaks.com/topic/85042-uploading-to-sql/#findComment-433883 Share on other sites More sharing options...
unknown101 Posted January 8, 2008 Author Share Posted January 8, 2008 Strange, the connection script only contains the username and password. I have used the same connection script/file to enter user details into the same SQL db.. the problem just seems to have surfaced when trying to upload a file ??? Quote Link to comment https://forums.phpfreaks.com/topic/85042-uploading-to-sql/#findComment-433894 Share on other sites More sharing options...
revraz Posted January 8, 2008 Share Posted January 8, 2008 Store the filename instead with a link to it? I am not sure what the criteria is to actually upload a file to MySQL. Quote Link to comment https://forums.phpfreaks.com/topic/85042-uploading-to-sql/#findComment-433897 Share on other sites More sharing options...
rhodesa Posted January 8, 2008 Share Posted January 8, 2008 Try changing your query to: $query = "INSERT INTO upload (`name`, `size`, `type`, `content` ) VALUES ('". mysql_real_escape_string($fileName)."','". mysql_real_escape_string($fileSize)."','". mysql_real_escape_string($fileType)."','". mysql_real_escape_string($content)."')"; Quote Link to comment https://forums.phpfreaks.com/topic/85042-uploading-to-sql/#findComment-433905 Share on other sites More sharing options...
unknown101 Posted January 8, 2008 Author Share Posted January 8, 2008 Still no joy.. thanks for the help tho guys, will continue with it tomorrow. Quote Link to comment https://forums.phpfreaks.com/topic/85042-uploading-to-sql/#findComment-433918 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.