By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
460,029 Members | 1,268 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 460,029 IT Pros & Developers. It's quick & easy.

'if' statement returns true in both cases

P: 10
I am having some difficulty getting the following to work:

=============================
Expand|Select|Wrap|Line Numbers
  1. if(isset($_POST['txt_edittitle']))
  2. {
  3. include('../includes/inc/misc.inc');
  4. $ed_id=$_POST['hid_id'];
  5. $edit_title=$_POST['txt_edittitle'];
  6. $edit_description=$_POST['txt_editdescription'];
  7.  
  8. if($_POST['txt_editfile'] != ' ')
  9. {
  10. echo "yes";
  11. $edit_filename = $_FILES['txt_editfile']['name'];
  12. $edit_filetemp = $_FILES['txt_editfile']['tmp_name'];
  13. $edit_filetype = $_FILES['txt_editfile']['type'];
  14. $edit_filesize = $_FILES['txt_editfile']['size'];
  15.  
  16. $edit_date=date("y-m-d");
  17. $edit_time=time("h:m:s");
  18.  
  19. $target_path2 = "docs/";
  20.  
  21. $target_path2 = $target_path2 . $edit_filename; 
  22.  
  23. move_uploaded_file($edit_filetemp, $target_path2);
  24.  
  25. mysql_query("UPDATE documents SET title='$edit_title', description='$edit_description', file_name='$edit_filename', file_size='$edit_filesize', file_type='$edit_filetype', date='$edit_date', time='$edit_time' WHERE id = '$ed_id'") or die(mysql_error()) ;
  26. }
  27. else
  28. {
  29. echo "no";
  30. mysql_query("UPDATE documents SET title='$edit_title', description='$edit_description' WHERE id = '$ed_id'") or die(mysql_error()) ;
  31.  
  32. }
  33.  
  34. }
  35.  
==========================
here is the form that sends the info
++++++++++++++++++++++++++
Expand|Select|Wrap|Line Numbers
  1. <form action="index.php" method="post" name="frm_uploadfile" enctype="multipart/form-data">
  2.         <input name="hid_member" type="hidden" value="<? echo $_SESSION["user"]; ?>" />
  3.  
  4.             <dl >
  5.  
  6.                 <dt class="normal">Title</dt>
  7.                     <dd><input name="<? if(isset($_GET['edit'])){ echo 'txt_edittitle'; } else { echo 'txt_title'; } ?>" type="text" id="<? if(isset($_GET['edit'])){ echo 'txt_edittitle'; } else { echo 'txt_title'; } ?>" size="38"  value="<? if(isset($_GET['edit'])){ echo $ed_title; } ?>" /></dd>
  8.                 <dt class="normal">Description</dt>
  9.                     <dd><textarea name="<? if(isset($_GET['edit'])){ echo 'txt_editdescription'; } else { echo 'txt_description'; } ?>" cols="35" rows="15" id="<? if(isset($_GET['edit'])){ echo 'txt_editdescription'; } else { echo 'txt_description'; } ?>"><? if(isset($_GET['edit'])){ echo $ed_description; } ?></textarea></dd>
  10.                 <dt class="normal">File</dt>
  11.                     <? 
  12.                     if(isset($_GET['edit']))
  13.                     { 
  14.                     echo "<dd class='normal'>";
  15.                     echo "Existing file:".$ed_filename."<br>";
  16.                     echo "<strong>Note:</strong> By entering a new file below, the existing file will automatically be written over."; 
  17.                     echo "</dd>";
  18.                     } 
  19.                     ?>
  20.                     <dd><input name="<? if(isset($_GET['edit'])){ echo 'txt_editfile'; } else { echo 'txt_file'; } ?>" type="file" id="<? if(isset($_GET['edit'])){ echo 'txt_editfile'; } else { echo 'txt_file'; } ?>"  size="25"  /></dd>
  21.  
  22.             </dl>
  23.             <input name="hid_id" type="hidden" value="<? echo $ed_id; ?>" />
  24.         <div align="right">
  25.         <input name="submit" type="submit" value="Upload File"  />        
  26.         </div>
  27.  
  28.         </form>
  29.  
++++++++++++++++++++++++++++

I put the echos in the first part to tell me which path it is taking and it is returning ;yes; in both cases.

The theory is that if the user chooses to edit the documents description, it is inserted into the above form so the user can modify the content, and if required, upload a new file to replace the last. So, if txt_editfile is empty, i wnt it to only change the title and/or description in the database, and if they have chosen a new file to upload, replace the details of the existing file in the database and the title and description of needed.

What is happening is, if i choose a new file to upload, the right code is being run, however, if i only change the description ot the title, it is deleting the existing document details and returning 'yes' (showing it is running the part it shouldnt.

I have tried using isset(), !empty() and something else that tests if an array is empty, cant remember what it was.

I hope I have explained this enough for someone to help me out.

James
Jul 30 '07 #1
Share this Question
Share on Google+
9 Replies


nathj
Expert 100+
P: 938
Hi jamesmcd999,

Welcome to TSDN!

I 've read over the code you posted and the problem description. I first want to check that I understand what you are asking.

Is the problem with the line:
Expand|Select|Wrap|Line Numbers
  1. if($_POST['txt_editfile'] != ' ')
  2.  
What is displayed on screen, under both approaches (a should be yes and a should be no) if you add the following line just before the problematice 'if':
Expand|Select|Wrap|Line Numbers
  1. echo $_POST['txt_editfile'];
  2.  
Let me know what that comes up with, it may shed some light on the matter.

Cheers
nathj
Jul 30 '07 #2

P: 10
Thanks for your post!

I am 99.9% sure it is that 'if' statement at fault.

I tried what you suggested (echo $_POST['txt_editfile'];), it returned nothing.

I tend to think it may be something to do with the fact that $_POST['txt_editfile'] is an array. I just cant seem to find how to make it work!

James
Jul 30 '07 #3

nathj
Expert 100+
P: 938
Thanks for your post!

I tend to think it may be something to do with the fact that $_POST['txt_editfile'] is an array. I just cant seem to find how to make it work!

James

Do you mean that 'txt_editfile' is in fact an array loaded into the $_POST? Or that $_POST is an array, which it is.

Perhaps you could try trplacing the echo line I suggested with:
Expand|Select|Wrap|Line Numbers
  1. print_r($_POST);
  2.  
This will show you everything in the $_POST array - the key name and the value.

Let me know what you get.

Cheers
nathj
Jul 30 '07 #4

P: 10
Do you mean that 'txt_editfile' is in fact an array loaded into the $_POST? Or that $_POST is an array, which it is.

Perhaps you could try trplacing the echo line I suggested with:
Expand|Select|Wrap|Line Numbers
  1. print_r($_POST);
  2.  
This will show you everything in the $_POST array - the key name and the value.

Let me know what you get.

Cheers
nathj
txt_editfile is an array loaded into $_POST.

I put the code you suggested before the 'if', it returned;

Array ( [hid_member] => James [txt_edittitle] => document title [txt_editdescription] => this is a document [hid_id] => v06srpdvzt [submit] => Upload File )

James
Jul 30 '07 #5

nathj
Expert 100+
P: 938
txt_editfile is an array loaded into $_POST.

I put the code you suggested before the 'if', it returned;

Array ( [hid_member] => James [txt_edittitle] => document title [txt_editdescription] => this is a document [hid_id] => v06srpdvzt [submit] => Upload File )

James
txt_editfile does not appear to be loaded into the $_POST array. Perhaps this is because you are loading an array into an array? I'm not sure, I've never tried it.
Perhaps try the following, I must admit I am guessing a bit here:
Expand|Select|Wrap|Line Numbers
  1. print_r($_SESSION['txt_editfile']);
  2.  
This should at least tell us if the editfile array has been loaded and what is in it.

I'm sorry this is all a bit 'suck it and see' but hopefully we should shed some light on things and get this working properly for you.

Cheers
nathj
Jul 30 '07 #6

P: 10
An array inside an array makes more sense!

I tried

Expand|Select|Wrap|Line Numbers
  1. print_r($_SESSION['txt_editfile']);
  2.  
and it returned nothing. However, when i replaced $_SESSION with $_FILES it outputted the following;

Array ( [name] => New_BDM_Email.doc [type] => application/msword [tmp_name] => /tmp/phpdKrUtk [error] => 0 [size] => 32256 )

What does this tell us?
Jul 30 '07 #7

nathj
Expert 100+
P: 938
An array inside an array makes more sense!

I tried

Expand|Select|Wrap|Line Numbers
  1. print_r($_SESSION['txt_editfile']);
  2.  
and it returned nothing. However, when i replaced $_SESSION with $_FILES it outputted the following;

Array ( [name] => New_BDM_Email.doc [type] => application/msword [tmp_name] => /tmp/phpdKrUtk [error] => 0 [size] => 32256 )

What does this tell us?
The first thing it tells me is that I made a thinko ( one step removed from a typo) on my last post. I typed $_SESSION but meant $_POST. Sorry about that.

The $_FILES[] array is a PHP global that contains file upload information. As you are uploading files PHP uses the $_FILES array to store the information.

This could be the solution you are looking for. Try the IF test as follows:
Expand|Select|Wrap|Line Numbers
  1. if (!empty($_FILES['name'])); // has a file been loaded
  2.  
This is then testing the PHP $_FILES array which you have shown has information in it. So this should do the trick.

Here's some more info on $_FILES
or try the PHP Manual.

Cheers
nathj
Jul 30 '07 #8

P: 10
The first thing it tells me is that I made a thinko ( one step removed from a typo) on my last post. I typed $_SESSION but meant $_POST. Sorry about that.

The $_FILES[] array is a PHP global that contains file upload information. As you are uploading files PHP uses the $_FILES array to store the information.

This could be the solution you are looking for. Try the IF test as follows:
Expand|Select|Wrap|Line Numbers
  1. if (!empty($_FILES['name'])); // has a file been loaded
  2.  
This is then testing the PHP $_FILES array which you have shown has information in it. So this should do the trick.

Here's some more info on $_FILES
or try the PHP Manual.

Cheers
nathj

That done the trick, thanks for that. Really appreciate you taking the time for helping me out.

Regards
James
Jul 30 '07 #9

nathj
Expert 100+
P: 938
That done the trick, thanks for that. Really appreciate you taking the time for helping me out.

Regards
James
I'm glad it's working, any time you get stuck just post here and someone will help.

All the best with the rest of the project.

nathj
Jul 30 '07 #10

Post your reply

Sign in to post your reply or Sign up for a free account.