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

Edited by lbh2011
Link to comment
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
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
Share on other sites

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
Share on other sites

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.