Jump to content

ZERO's and NULL's in form fields


MargateSteve

Recommended Posts

I have got another of those problems that should be easy to solve but I cannot get my head round it!

 

I have a form to enter data into a table and one of the fields can be left blank. However, I'd data is entered in the field it can be a Zero. This is leaving me a problem as I cannot find a way to get the script to insert NULL if the field is blank but actually put the Zero in if that is what is entered.

 

I have tried several permutations of nested if's using empty or isset, but whatever I try, the same result is inserted in both scenarios.

 

The arguments I need to pass are......

If field is empty INSERT null

If field is 0 INSERT 0

If field is not empty and not 0 INSERT field

 

Thanks in advance for any suggestions.

 

Steve

Link to comment
Share on other sites

That did not work unfortunately, unless I have tried to use it in the wrong context.

 

Whether I leave the field blank, or enter a zero, it still enters a zero into the table.

 

$hg = isset($_POST['homegoals']) && ctype_digit($_POST['homegoals'])  ? (int) $_POST['homegoals'] : NULL;

$thismatch = $_POST['match'];

mysql_query
  (
  "
  UPDATE 
  all_games 
   SET 
   home_goals = ' " .$hg. " '
   WHERE 
   all_games_id = '".$thismatch."' 
  ") or die(mysql_error());  
header("Location: matchupdate.php?thedate=".$_POST['date']."");

Link to comment
Share on other sites

Is there a default value of 0 for the field by chance?

The actual field is DEFAULT NULL and when a new entry is made it shows up as null. There is no need to put a zero in on INSERT as the fields are intended to be initally blank (they are soccer fixtures and will remain blank until the game is played) so I have only tested it via UPDATE.

 

I have actually come up with a solution that is far from elegant but does the job....

if(!empty($_POST['homegoals']))
{
	$hg = "'".$_POST['homegoals']."'";
}
else if ($_POST['homegoals'] == '0')
{
	$hg = '0';
}
else
{
	$hg = 'NULL';	
}

**SNIPPED OTHER POST TESTS**

$thismatch = $_POST['match'];

mysql_query
  (
  "
  UPDATE 
  all_games 
   SET 
   home_goals = " .$hg. ", 
   away_goals = " .$ag. ", 
   attendance = " . $att . " 
   WHERE 
   all_games_id = '".$thismatch."' 
  ") or die(mysql_error());  
header("Location: matchupdate.php?thedate=".$_POST['date']."");

}

Link to comment
Share on other sites

I think I see what was causing the problem with the code I posted. The quotes in the query string around $hg cause the value to be interpreted as a string, so it was attempting to set the field to the string literal 'NULL'.

 

So if I used your code with home_goals = $hg in the query string it should work? It is late now and am off to bed but will give that a go in the morning.

 

Thanks

Steve

Link to comment
Share on other sites

I tried it again and it threw up a MySQL error on that part, so I tried again but wrapped the NULL in the code you provided in single quotes

$hg = isset($_POST['homegoals']) && ctype_digit($_POST['homegoals'])  ? (int) $_POST['homegoals'] : 'NULL';

 

Now it works perfectly. All that is left is for me to understand what it is doing! My assumption is....

 

$hg = isset($_POST['homegoals']) && ctype_digit($_POST['homegoals'])

Checks if the field is set and if it is set is it a number?

 

? (int) $_POST['homegoals']

If it is a number, insert the field

 

: 'NULL';

If not set or not a number, enter NULL

 

Thanks for the help.

Steve

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.