Jump to content

[SOLVED] Isset or empty or what?


Siggles

Recommended Posts

Hi,

 

I have a form and if the user does not fill out one of the fields it is still changing that field to a 0 and I want it to stay as NULL. Here is my code chopped down (the php bit)...

 

switch($_POST['opt2']) {
case "editprediction":  //SQL FOR EDITING PREDICTION IN DBASE

		$score=$_POST['score'];

		if (isset($_POST['score'])){
		mysql_query("UPDATE predictions SET score = '$score' WHERE predictionid = '$j'");
		}
		break;
}
?>

 

I understand that isset only checks that a varible exists which is why it is setting it to 0. There may be times in the form however when I actually want the user to put 0 in the form field. So I only want it to do the query when the form field has something in it (0 or any number) but not when it is left blank. Any suggestions?

Link to comment
Share on other sites

try this:

<?php
switch($_POST['opt2']) {
case "editprediction":  //SQL FOR EDITING PREDICTION IN DBASE

		$score=$_POST['score'];

		if (isset($_POST['score']) && !empty($_POST['score']) && is_numeric($_POST['score'])){
		mysql_query("UPDATE predictions SET score = '$score' WHERE predictionid = '$j'");
		}
		break;
}
?>

 

Regards ACE

Link to comment
Share on other sites

The problem with using "empty()" on a field is that it returns "true" if the field contains a "0" (zero). I always use

<?php
if (strlen(trim(stripslashes($_POST['fld']))) > 0) {
?>

 

to see if a field is not empty.

 

Ken

Link to comment
Share on other sites

try this:

<?php
switch($_POST['opt2']) {
case "editprediction":  //SQL FOR EDITING PREDICTION IN DBASE

		$score=$_POST['score'];

		if (isset($_POST['score']) && !empty($_POST['score']) && is_numeric($_POST['score'])){
		mysql_query("UPDATE predictions SET score = '$score' WHERE predictionid = '$j'");
		}
		break;
}
?>

 

Regards ACE

 

Thank you for your help, only it wont accept a 0 in the form and leaves it null. It works in that it leaves field null thogh when field is empty. halfway there! :-)

Link to comment
Share on other sites

The problem with using "empty()" on a field is that it returns "true" if the field contains a "0" (zero). I always use

<?php
if (strlen(trim(stripslashes($_POST['fld']))) > 0) {
?>

 

to see if a field is not empty.

 

Ken

 

Great, that works! Thanks :-)

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.