Jump to content

Recommended Posts

I have a form with a load of entry fields. I want to check to see if the field is empty, if the fields empty i want the variable to be set to null.

I have all these variables...
[code]
$user_id = $userdata['user_id'];
$f_name = $_POST['first_name'];
$l_name = $_POST['last_name'];
$gfx = $_POST['hw_gfx'];
$mb = $_POST['hw_mb'];
$pro = $_POST['hw_pro'];
$mem = $_POST['hw_mem'];
$mouse = $_POST['hw_mouse'];
$fgame = $_POST['fav_game'];
$ffilm = $_POST['fav_film'];
$fbook = $_POST['fav_book'];
$notes = $_POST['notes'];[/code]

Please tell me there is a better way than

[code] if (empty($_POST['first_name'])) {
$f_name = NULL;
} else {
$f_name = $_POST['first_name'];
} [/code]
How about a function?

[code]<?php
$user_id = set_to_null($userdata['user_id']);
$f_name = set_to_null($_POST['first_name']);
$l_name = set_to_null($_POST['last_name']);
$gfx = set_to_null($_POST['hw_gfx']);
$mb = set_to_null($_POST['hw_mb']);
$pro = set_to_null($_POST['hw_pro']);
$mem = set_to_null($_POST['hw_mem']);
$mouse = set_to_null($_POST['hw_mouse']);
$fgame = set_to_null($_POST['fav_game']);
$ffilm = set_to_null($_POST['fav_film']);
$fbook = set_to_null($_POST['fav_book']);
$notes = set_to_null($_POST['notes']);

function set_to_null($var){
  if (empty($var)){
      $var = NULL;
  }
  return $var;
}
?>[/code]
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.