glendango Posted September 21, 2017 Share Posted September 21, 2017 (edited) Hi. i have an edit form which displays info from a few tables in my database. This all works fine because i populate the input fields with php rows. Trouble i'am having is with FILE inputs.. I have 3 images which sit really nicely on the edit page next to 3 file inputs. Waiting for the user to update the image if they want to... trouble is if they dont update the image the input sends an empty value back into my 'images ' table. Is it possible to keep everything on this page or do i have to create an edit image button and take user to a different page so they can change an image one by one? been playing around with this for a while, it works for the first image.... but i cant get it to work for all 3 images. if($_FILES['file']['name'] == "") { Edited September 21, 2017 by glendango Quote Link to comment https://forums.phpfreaks.com/topic/305078-edit-form-ignore-empty-file-value/ Share on other sites More sharing options...
Sepodati Posted September 21, 2017 Share Posted September 21, 2017 If the file field is blank, as you're checking, then don't include that field in your update query. The field will retain it's current value. How are you naming the file fields? Quote Link to comment https://forums.phpfreaks.com/topic/305078-edit-form-ignore-empty-file-value/#findComment-1551739 Share on other sites More sharing options...
glendango Posted September 21, 2017 Author Share Posted September 21, 2017 (edited) but what if user does update 1 of the images...i want that to update. ( how am i naming file fields / how do u mean? thx) just read that again...do you mean 'as your checking = code... thats what iam trying to find out..what code will check for blank and leave the mysql column alone does this help ? if($_FILES['file']['name'] == "") $sql2= "UPDATE portal_images SET file='$file',fileb='$fileb' ,filec='$filec' WHERE id=$id"; Edited September 21, 2017 by glendango Quote Link to comment https://forums.phpfreaks.com/topic/305078-edit-form-ignore-empty-file-value/#findComment-1551741 Share on other sites More sharing options...
mac_gyver Posted September 21, 2017 Share Posted September 21, 2017 the ['name'] element will be empty for reasons other than no file being selected. you must always use the ['error'] element in determining what to do with the submitted data. if a file isn't selected for a type='file' field, the ['error'] element for that field will be UPLOAD_ERR_NO_FILE (a value of 4.) your form processing code would need to detect that error value not include that that particular db table column in the update query (or perhaps more simply update it to its existing value.) Quote Link to comment https://forums.phpfreaks.com/topic/305078-edit-form-ignore-empty-file-value/#findComment-1551744 Share on other sites More sharing options...
glendango Posted September 21, 2017 Author Share Posted September 21, 2017 thanks for replies... iam not sure why i dont understand anything your saying.. if a user submits a file from the 'file input' it works absolutly fine..my problem is if the user doesnt select a new file it populates database with 'nothing' ...which i understand why ... i dont know how to code to say: if value is nothing...ignore the value and leave the database entry ( from a different page alone ) Quote Link to comment https://forums.phpfreaks.com/topic/305078-edit-form-ignore-empty-file-value/#findComment-1551747 Share on other sites More sharing options...
glendango Posted September 21, 2017 Author Share Posted September 21, 2017 (edited) this works for the first image = 'file' but not for the rest,( i take out the 'file' in the first update and put back in second update so user can change the image if they want to.) ,,is there a way to have 3 if's in 1 line? if($_FILES['file']['name'] == "") { $sql2= "UPDATE portal_images SET fileb='$fileb',typeb='$file_typeb',sizeb='$file_sizeb',filec='$filec',typec='$file_typec',sizec='$file_sizec' WHERE id=$id"; }else{ $sql2= "UPDATE portal_images SET file='$file',type='$file_type',size='$file_size', fileb='$fileb',typeb='$file_typeb',sizeb='$file_sizeb',filec='$filec',typec='$file_typec',sizec='$file_sizec' WHERE id=$id"; } mysqli_query($conn, $sql2); Edited September 21, 2017 by glendango Quote Link to comment https://forums.phpfreaks.com/topic/305078-edit-form-ignore-empty-file-value/#findComment-1551752 Share on other sites More sharing options...
Psycho Posted September 21, 2017 Share Posted September 21, 2017 First of all, it looks like you are injecting the user data directly in to the query - leaving your code wide open to SQL injection. You need to learn to use prepared statements. As to your problem, I would create an UPDATE query that updates that field conditionally based on the passed value. If the value is not an empty string, then set the DB field to that value, else set the value to it's current DB value. The prepared statement might look like this: UPDATE portal_images SET file = :file, fileb = :fileb filec = IF(:filec1<>'', :filec2, filec) WHERE id = :id I don't think you can use the same assignment twice in a prepared statement, so the value would have to be bound twice (filec1 and filec2). Or in your existing code (which is likely a security risk): UPDATE portal_images SET file = '$file', fileb = '$fileb' filec = IF('$filec'<>'', '$filec', filec) WHERE id = $id Quote Link to comment https://forums.phpfreaks.com/topic/305078-edit-form-ignore-empty-file-value/#findComment-1551755 Share on other sites More sharing options...
glendango Posted September 21, 2017 Author Share Posted September 21, 2017 thanks alot i will try this in morning..12 hours is enough today... just for interest,i cant find this answer anywhere on web... stack no good.etc. am i just trying to do a stupid thing?? what is best practice for allowing user to edit / update a few pictures....never thought this would be so hard to figure out. Quote Link to comment https://forums.phpfreaks.com/topic/305078-edit-form-ignore-empty-file-value/#findComment-1551756 Share on other sites More sharing options...
glendango Posted September 22, 2017 Author Share Posted September 22, 2017 (edited) hi that code doesnt work.. i can upload pictures with the code by selcting another file but it clears them again if i upload with empty values. the IF statement doesnt go 'red' so is it supposed to be there? $sql2= "UPDATE portal_images SET file = '$file', fileb = '$fileb', filec = if ('$filec'<>'', '$filec', 'filec') WHERE id = $id"; Edited September 22, 2017 by glendango Quote Link to comment https://forums.phpfreaks.com/topic/305078-edit-form-ignore-empty-file-value/#findComment-1551789 Share on other sites More sharing options...
Barand Posted September 22, 2017 Share Posted September 22, 2017 The aim is to update with the current column value if it is blank filec = if ('$filec'<>'', '$filec', 'filec') ˆ ˆ That code, with the quotes, will replaces with the the literal string "files". Remove the quotes. Quote Link to comment https://forums.phpfreaks.com/topic/305078-edit-form-ignore-empty-file-value/#findComment-1551792 Share on other sites More sharing options...
glendango Posted September 22, 2017 Author Share Posted September 22, 2017 (edited) thx sen , so what should it look like? $sql2= "UPDATE portal_images SET file = '$file', fileb = '$fileb', filec = if ('$filec'<>'', '$filec', filec) WHERE id = $id"; i am now writing code so a 'modal' appears when user clicks edit image and i delete the 'file input box' from the edit page..... should i have done this all along? Edited September 22, 2017 by cyberRobot Please use [code][/code] tags Quote Link to comment https://forums.phpfreaks.com/topic/305078-edit-form-ignore-empty-file-value/#findComment-1551794 Share on other sites More sharing options...
mac_gyver Posted September 22, 2017 Share Posted September 22, 2017 i am now writing code so a 'modal' appears when user clicks edit image and i delete the 'file input box' from the edit page..... should i have done this all along? no matter what you do in the client/browser, your server-side form processing code will still need to determine what it should do if an image was successfully uploaded, if no image was selected, or if there is a different type of upload error, some of which will have a non-empty ['name'] element. and, in looking at your database table design, you should not be storing image data in multiple columns in a single row, but a separate row for each image. which triggered a thought. if you are replacing an existing image, why don't you just use the same destination name and let move_uploaded_file() replace the existing file with the new one? Quote Link to comment https://forums.phpfreaks.com/topic/305078-edit-form-ignore-empty-file-value/#findComment-1551810 Share on other sites More sharing options...
glendango Posted September 22, 2017 Author Share Posted September 22, 2017 iam not trying to upload a diferent image every time and i have no memory and so cant just write code .. .. i am lost.. i spend 12 hours a day and retain nothing... i give up...for this week anyway Quote Link to comment https://forums.phpfreaks.com/topic/305078-edit-form-ignore-empty-file-value/#findComment-1551817 Share on other sites More sharing options...
glendango Posted September 23, 2017 Author Share Posted September 23, 2017 so Mac ,,, if you save images under id in 1 column, how do you call the same 3 pictures everytime. i am building an app that will display 3 pictures. 1 main picture and 2 smaller ones underneath. If the images/ paths get saved into the same column how would you call them in the correct order ? With col 1 2 3 you obviously call the column with the id.... not sure how this is done with 1 col. cheers.. sorry for splitting original question.. Quote Link to comment https://forums.phpfreaks.com/topic/305078-edit-form-ignore-empty-file-value/#findComment-1551826 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.