lbh2011 Posted January 11, 2013 Share Posted January 11, 2013 Hello! Please could someone have a look at the following code and suggest why upload.php is not being included. I basically want to test whether the form field (upload) contains a value (local file path). If it does contain a value I want to include a file upload script but if it doesn't I want to continue to the next section which processes the form. Due to the way the rest of the code is structured I can't check for a blank field first before continuing to the next step. $upload = trim(mysql_real_escape_string($_POST["upload"])); if(!isset($upload)) { include 'upload.php'; } else { Quote Link to comment Share on other sites More sharing options...
MDCode Posted January 11, 2013 Share Posted January 11, 2013 ! basically means return false on. so !isset() will look if it isn't set. Quote Link to comment Share on other sites More sharing options...
lbh2011 Posted January 11, 2013 Author Share Posted January 11, 2013 Is that the correct code to use then if I want to include upload.php if a value is submitted? Quote Link to comment Share on other sites More sharing options...
MDCode Posted January 11, 2013 Share Posted January 11, 2013 (edited) Remove the !. The mysql_real_escape_string() and trim() you added may also return it true. Edited January 11, 2013 by SocialCloud Quote Link to comment Share on other sites More sharing options...
Barand Posted January 11, 2013 Share Posted January 11, 2013 if(!isset($upload)) $upload will always be set. You have just set it on the previous line. Quote Link to comment Share on other sites More sharing options...
lbh2011 Posted January 11, 2013 Author Share Posted January 11, 2013 OK! Thanks for your help. Quote Link to comment Share on other sites More sharing options...
lbh2011 Posted January 11, 2013 Author Share Posted January 11, 2013 (edited) Does anyone know why this won't work. Even when nothing is entered into the file upload field, it is processed as if something was... $file_upload = trim(mysql_real_escape_string($_POST["file_upload"])); if(isset($file_upload)) { include ('contract_upload.php'); $rsUpdate = mysql_query("UPDATE contract SET date_added = '$date_added', file_upload = '$upload_path' WHERE id = '$id' ");} else { $rsUpdate = mysql_query("UPDATE contract SET date_added = '$date_added' WHERE id = '$id' ");} if($rsUpdate) { echo "Successfully Update";} else { die('Invalid query: '.mysql_error());; } [EDIT] This is for an update form by the way, hence why I don't want to overwrite the $upload_path record in the database if a new file is not uploaded... Edited January 11, 2013 by lbh2011 Quote Link to comment Share on other sites More sharing options...
haku Posted January 11, 2013 Share Posted January 11, 2013 if(isset($file_upload)) { You set $file_upload right above this line. So it will always be set, meaning it will always be true. Quote Link to comment Share on other sites More sharing options...
lbh2011 Posted January 11, 2013 Author Share Posted January 11, 2013 OK In that case how to I work out if it contains a value or not? Quote Link to comment Share on other sites More sharing options...
DavidAM Posted January 11, 2013 Share Posted January 11, 2013 You need to check $_POST['file_upload'] itself. Although, I think it will always be set as well, so you want to check to see if it is empty. Or check to see if your $file_upload variable is empty. Quote Link to comment Share on other sites More sharing options...
lbh2011 Posted January 11, 2013 Author Share Posted January 11, 2013 @DavidAM I've tried the following but this doesn't appear to work either... $file_upload = trim(mysql_real_escape_string($_POST["file_upload"])); if(empty($_POST["file_upload"])) { include ('contract_upload.php'); $rsUpdate = mysql_query("UPDATE contract SET date_added = '$date_added', file_upload = '$upload_path' WHERE id = '$id' ");} else { $rsUpdate = mysql_query("UPDATE contract SET date_added = '$date_added' WHERE id = '$id' ");} if($rsUpdate) { echo "Successfully Update";} else { die('Invalid query: '.mysql_error());; } Quote Link to comment Share on other sites More sharing options...
haku Posted January 11, 2013 Share Posted January 11, 2013 You are running mysql_real_escape_string() before necessary. You only need to run that function if there is a value on which to run it. Change your code to this: $file_upload = trim($_POST["file_upload"]); If you are still running problems, then echo $file_upload before your conditional, as it will contain a value of some sorts. You can then run mysql_real_escape_string() inside your if() conditional. Quote Link to comment Share on other sites More sharing options...
DavidAM Posted January 11, 2013 Share Posted January 11, 2013 if(empty($_POST["file_upload"])) { include ('contract_upload.php'); $rsUpdate = mysql_query("UPDATE contract SET date_added = '$date_added', file_upload = '$upload_path' WHERE id = '$id' ");} else { $rsUpdate = mysql_query("UPDATE contract SET date_added = '$date_added' WHERE id = '$id' ");} Think about the code you write. That statement says: "If the user did NOT upload a file, then record the name of the file they DID upload" Quote Link to comment Share on other sites More sharing options...
haku Posted January 11, 2013 Share Posted January 11, 2013 Hah - I was so busy looking at syntax, I ignored the logic Quote Link to comment 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.