Jump to content

Recommended Posts

Hi there,

 

I have a form with around 100 variables. Some variables are required to be filled in order for the form to be process, while some are not. At the moment if a non-required field isn't filled in, the form WILL submit, but I recieve a undefined variable error at the top of the page after submission.

 

Now, I initially tried to submit the non required fields like this

foreach($notRequired_fields as $fieldname) {
			if (isset($_POST[$fieldname])) {
				$query = "INSERT INTO smartphones (
					$fieldname
					) VALUES (
					'{$fieldname}',
					)";
			}

 

This worked but failed misserable as you could imagine (should have seen it coming). So what I plan to do next is to make all the unset variables  =  0 to get rid of the warning. Is this a good idea or bad practice? What's good practice when it comes to non required fields in a database? I ask because im still learning and I'm not sure if in the future It will be punitive for me to have empty rows in my database.

 

If it IS ok to leave the variables empty in the database, how do I go about getting around the undefined variable error?

 

Thanks in advance!

Edited by NiTx
Link to comment
https://forums.phpfreaks.com/topic/275842-dealing-with-non-required-fields/
Share on other sites

The code you gave would not trigger an undefined variable error... so the error is coming from somewhere else in your script. And the value should be '{$_POST[$fieldname]}' instead of '$fieldname' unless you want every value just to equal the field name.

 

Also you say you have 100 variables yet you are creating a new row for every single field that has a value? I don't think that's what you are trying to do?

Edited by akphidelt2007

Sorry, it was originally '{$_POST[$fieldname]}' I just rewrote the code in a hurry. As you pointed out, what happened was it created a new row for every value. I'm not sure how to make a loop where it doesn't create a new fow for each value (still researching, but if you can show me that would be fantastic). However, like I said before I have a solution which is to do this


 

if(isset($_POST['freq_edge'])) { $freq_edge = mysql_prep($_POST['freq_edge']); } else { $freq_edge = 0; }

 

I'm just not convinced that its kosher. I want to find a better solution, or have someone point me in the right direction to one.

Edited by NiTx

The way I handle larger fields is to create an array of the fields and an array of the values and implode them out at the end... for instance...

 

 

$requiredFields = Array('reqfld1'=>'str','reqfld2'=>'int');
$fields = $values = Array();
$error = false;
 
foreach($_POST as $field => $value)
{
   //check to see if it's empty
   if(!$value)
   {
       //check to see if it's required and note that there is an error. There is a lot more that you should do to track this error, but this is just for an example
       if(isset($requiredFields[$field]))
       {
           $error = true;
        }
    }
    else
    {
         //record the fields and values here. For fields you usually have to create an array or a table of fields and their requirements like if they are ints or dates, etc
         //but for simplicity sake
         $fields[] = $field;
         $values[] = is_int($value) ? $value : "'$value'";
     }
}
 
$qry = "INSERT INTO mytable (".implode(',',$fields).") VALUES (".implode(',',$values).")";
 

IF a field is not required in your form, then that field in the database should be set to allow NULLs..

 

As for your idea of checking for optionals... It looks good to me.. but I would make use of the null keyword.. and use a ternary operator

 

$optionalOne = isset($_POST['optionalOne']) ? $_POST['optionalOne'] : null;
$optionalTwo = isset($_POST['optionalTwo']) ? $_POST['optionalTwo'] : null;
$optionalThree = isset($_POST['optionalThree']) ? $_POST['optionalThree'] : null;

 

Then, you can have a full INSERT statement with all 100 fields in it.  Anything that is optional and is not filled out will go into the database as null.

Edited by Zane

The way I handle larger fields is to create an array of the fields and an array of the values and implode them out at the end... for instance...

 

Just goes to show you can only learn so much on lynda.com. My coding skills are still at a rudimentary level, so thanks! Will definitely read up some more on the implode. Given all the large arrays I use, I think it will come in handy!

 

 

As for your idea of checking for optionals... It looks good to me.. but I would make use of the null keyword.. and use a ternary operator

 

Nice to know I was on the right track! Thanks for introducing me to this, I've seen it used before but didn't know what to search in google. I did some reading on it just now, would you say it's mainly used for smaller if else statements like mine and that for a larger argument you would just use an ELSE IF?

 

Again thanks a ton!

ould you say it's mainly used for smaller if else statements like mine and that for a larger argument you would just use an ELSE IF?

I usually only ever use the ternary operators if I am dealing with ONE string.  As you can see, all that it does is sign One value to a variable.  If I were to use and IF/ELSE statement, I could add other factors to it: like querying the database, doing calculations, echoing more information.  In your example, you will only ever have two options for your value..  NULL or what is being held in your POST variable.  That is the only reason I pointed it out

 

I'm not suggesting either that you cannot add functions to this form.

 

$optionalOne = isset($_POST['optionalOne']) ? substr(strtoupper(strtolower($_POST['optionalOne']))),0,5) : is_null(null);

The above example is not exactly useful to you but it will work... and it will only execute if that POST variable is set.

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.