Jump to content

whats wrong with this code


arden

Recommended Posts

<?phpif(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);if(!get_magic_quotes_gpc()){    $fileName = addslashes($fileName);}$con= mysql_connect("localhost", "root", "pass");if (!$con){die ('Could not connect: ' . mysql_error());}$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>";}?><form method="post" enctype="multipart/form-data"><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>

output:Error, query failedwhats wrong with this code?..as u can see, its an upload image script.. can any1 tell whats wrong?..can u help me correct it?..thx in advnce.

Link to comment
Share on other sites

We can't really see what the error is or where the query failed. Try adding mysql_error to the die statement's message:mysql_query($query) or die('Error, query failed: '.mysql_error());Then repost the error message you get.

Link to comment
Share on other sites

We can't really see what the error is or where the query failed. Try adding mysql_error to the die statement's message:mysql_query($query) or die('Error, query failed: '.mysql_error());Then repost the error message you get.
thx dude!..i forgot to select the database.. :)now a new problem comes..how can i know if it really uploaded the file?..
Link to comment
Share on other sites

i got no idea about what u are saying..
If you want to know if something is really in the database, you must check the database (i.e. request the thing right after you've inputted it). That's of course redunant in 99.99% of the cases, as if there's an error, MySQL will surely signal PHP about it. So, if there's no error, it's safe to assume the thing is in the database.If you mean how to check if the upload file (by the client) is OK, you can use the error constants, and the UPLOAD_ERR_OK in particular. If everything appears OK, $_FILES['userfile']['error'] will be equal to this consntant. See example #3 from the PHP manual as a sample code. While that code does it on an array of files (for the case of showing off THAT ability), making it for a single file is pretty much equivalent.
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...