Jump to content

Test form field


lbh2011

Recommended Posts

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
{

Link to comment
https://forums.phpfreaks.com/topic/272986-test-form-field/
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/272986-test-form-field/#findComment-1404843
Share on other sites

@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());; }

Link to comment
https://forums.phpfreaks.com/topic/272986-test-form-field/#findComment-1404851
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/272986-test-form-field/#findComment-1404854
Share on other sites

  Quote
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"

Link to comment
https://forums.phpfreaks.com/topic/272986-test-form-field/#findComment-1404856
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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