greenday Posted July 5, 2007 Share Posted July 5, 2007 Hi, I am using a form to upload 5 image names to a database, and store the file in a folder. However, if only 4 or less of the five fields are filled out, the databases default image 'no image to display' is overwritten by nothing. Is there any way to write an if field is blank, use default.gif file, else use uploaded file? Here is the code: mysql_query("UPDATE `listtable` SET `mainpic` = '".$_FILES['pic']['name'][0]."', `pic2` = '".$_FILES['pic']['name'][1]."', `pic3` = '".$_FILES['pic']['name'][2]."', `pic4` = '".$_FILES['pic']['name'][3]."', `pic5` = '".$_FILES['pic']['name'][4]."', `pic6` = '".$_FILES['pic']['name'][5]."' WHERE `placeid` = '$plid'"); Thanks. Link to comment https://forums.phpfreaks.com/topic/58493-solved-upload-default-pic/ Share on other sites More sharing options...
stoker Posted July 5, 2007 Share Posted July 5, 2007 something like this perhaps - I only did it on pic 5 and 6 so you can see the diff.. $default = 'mypic.png'; mysql_query("UPDATE `listtable` SET `mainpic` = '".$_FILES['pic']['name'][0]."', `pic2` = '".$_FILES['pic']['name'][1]."', `pic3` = '".$_FILES['pic']['name'][2]."', `pic4` = '".$_FILES['pic']['name'][3]."', `pic5` = '".( empty($_FILES['pic']['name'][4]) ? $default : $_FILES['pic']['name'][4] )."', `pic6` = '".( empty($_FILES['pic']['name'][5]) ? $default : $_FILES['pic']['name'][5] )."' WHERE `placeid` = '$plid'"); On another note, this is extremely insecure - crafted filenames could inject you sql - you really need to clean input and output! Link to comment https://forums.phpfreaks.com/topic/58493-solved-upload-default-pic/#findComment-290096 Share on other sites More sharing options...
teng84 Posted July 5, 2007 Share Posted July 5, 2007 pic1=(!empty($_FILES['pic']['name'][5])?$_FILES['pic']['name'][5]:'put the defulat'; insert to the db pic1 Link to comment https://forums.phpfreaks.com/topic/58493-solved-upload-default-pic/#findComment-290097 Share on other sites More sharing options...
greenday Posted July 5, 2007 Author Share Posted July 5, 2007 Thanks guys, great help, managed to get it working just as I wanted Link to comment https://forums.phpfreaks.com/topic/58493-solved-upload-default-pic/#findComment-290145 Share on other sites More sharing options...
greenday Posted July 5, 2007 Author Share Posted July 5, 2007 also @stoker - thanks for pointing the security risk out to me. However, on this occasion its only for an admin area, so only staff could have access to it. Link to comment https://forums.phpfreaks.com/topic/58493-solved-upload-default-pic/#findComment-290147 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.