rawky1976 Posted February 1, 2007 Share Posted February 1, 2007 Hello I have a MySQL database of document properties that is populated via an upload forms INSERT function. I have a search results page that displays these properties in a dynamic repeating table. I want the document title to be a link that opens the document itself. This currently works if I enter into the document name field the actual document title INCLUDING FILE EXTENSION, but not if I dont add the file extension. I have attached two files listing the INSERT function and the code for the search results table. Hope someone can help! Mark [attachment deleted by admin] Link to comment https://forums.phpfreaks.com/topic/36691-lniks-without-file-extensions/ Share on other sites More sharing options...
Psycho Posted February 1, 2007 Share Posted February 1, 2007 You need to either: 1) Add a new column to the database for the file extension. Then you can display the file name and use the filename+extension in the actual link url. OR 2) Include the entire file name (including extension) in the filename field and use some string manipulation to display everything up to the last period when displaying just the file name. I would prefere option #1, but here is some sample code for option #2. <?php $file = "myfile.doc"; $filename = substr($file, 0, strrpos($file, ".")); $fileext = substr($file, strrpos($file, ".")); echo "File Name & extension: " . $file . "<br>"; // mydoc.doc echo "File Name: " . $filename . "<br>"; // mydoc echo "File Extension: " . $fileext . "<br>"; // .doc ?> Link to comment https://forums.phpfreaks.com/topic/36691-lniks-without-file-extensions/#findComment-175006 Share on other sites More sharing options...
rawky1976 Posted February 1, 2007 Author Share Posted February 1, 2007 I think i would prefer option 1 too. But how do I grab the file extension for the new field? I have a file control on the form, so what's the code to stip the bit i need and then insert it into the table? Thanks, Mark! Link to comment https://forums.phpfreaks.com/topic/36691-lniks-without-file-extensions/#findComment-175011 Share on other sites More sharing options...
Psycho Posted February 2, 2007 Share Posted February 2, 2007 Um, you would use pretty much the same code I posted above to take the file name and split it up into two parts. Using your existing code I made some modifications (you would need to add the column "document_ext" to the table.): if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "uploadForm")) { $file = GetSQLValueString($_POST['docNameField'], "text"); $filename = substr($file, 0, strrpos($file, ".")); $fileext = substr($file, strrpos($file, ".")); $insertSQL = sprintf("INSERT INTO document (document_name, document_ext, relating_to, about, category_id, format_id, submitter_id) VALUES (%s, %s, %s, %s, %s, %s)", $filename, $fileext, GetSQLValueString($_POST['relatingToField'], "text"), GetSQLValueString($_POST['aboutField'], "text"), GetSQLValueString($_POST['categoryList'], "text"), GetSQLValueString($_POST['formatList'], "text"), GetSQLValueString($_POST['submitterList'], "text")); mysql_select_db($database_ConnKnowledgeBase, $ConnKnowledgeBase); $Result1 = mysql_query($insertSQL, $ConnKnowledgeBase) or die(mysql_error()); } On a side note, I just don't understand why so many people use sprintf for creating their queries. IMHO, it makes it more difficult to read the code as you need to look back and forth to see which field each piece of data is going into. Link to comment https://forums.phpfreaks.com/topic/36691-lniks-without-file-extensions/#findComment-175180 Share on other sites More sharing options...
rawky1976 Posted February 3, 2007 Author Share Posted February 3, 2007 I can see how that would work, but since pasting it in I get the file uploaded to the webserver but the insert doesn't work (nothing in the database)?!?!?! Link to comment https://forums.phpfreaks.com/topic/36691-lniks-without-file-extensions/#findComment-176415 Share on other sites More sharing options...
Destruction Posted February 4, 2007 Share Posted February 4, 2007 There are only 6 values in the statement and 7 fields - you may wish to check and add another %s if appropriate Dest Link to comment https://forums.phpfreaks.com/topic/36691-lniks-without-file-extensions/#findComment-176452 Share on other sites More sharing options...
rawky1976 Posted February 4, 2007 Author Share Posted February 4, 2007 Yeah sorry I should've said I'd already spotted that one. I'll have another look over it and get back if I need to. Mark Link to comment https://forums.phpfreaks.com/topic/36691-lniks-without-file-extensions/#findComment-176775 Share on other sites More sharing options...
Psycho Posted February 5, 2007 Share Posted February 5, 2007 Post any errors you get. If you get no errors, echo the value of $insertSQL to your page and see if it contains what you think it should. Link to comment https://forums.phpfreaks.com/topic/36691-lniks-without-file-extensions/#findComment-177135 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.