Jump to content

Recommended Posts

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 by glendango
Link to comment
https://forums.phpfreaks.com/topic/305078-edit-form-ignore-empty-file-value/
Share on other sites

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 by glendango

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.)

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 )

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 by glendango

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

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.   

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 by glendango

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.

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 by cyberRobot
Please use [code][/code] tags
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?

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..

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.