Jump to content

Need help in my IF condition


bugzy

Recommended Posts

I have this if statement with validations and mysql query

 

<?php
if(isset($_POST['submit']))
{
	$errors = array();

	$required_fields = array('menu_name', 'position', 'visible');

	foreach($required_fields as $fieldname)
		{
			if(!isset($_POST[$fieldname]) || (empty($_POST[$fieldname]) && $_POST[$fieldname] != 0))
				{
					$errors[] = $fieldname;
				}
		}

	$fields_with_length = array('menu_name' => 30);

	foreach($fields_with_length as $fieldname => $maxlength)
		{
			if (strlen(trim(mysql_prep($_POST[$fieldname]))) > $maxlength)
				{
					$errors[] = $fieldname;
				}
		}	


		if(empty($errors))
			{
				//Perform Update

					$id = mysql_prep($_GET['subj']);
					$menu_name = mysql_prep($_POST['menu_name']);
					$position = mysql_prep($_POST['position']);
					$visible = mysql_prep($_POST['visible']);


					$query = "UPDATE subjects SET menu_name = '{$menu_name}', position = {$position}, visible = {$visible} WHERE id = {$id}";

					$result = mysql_query($query, $connection);
					if(mysql_affected_rows() == 1)
					{
						//success
						$message = "Record has been successfuly updated!";
					}
					else
					{
						//failed
						$message = "The subject update failed";
						$message .= "<br>". mysql_error();
					}

			}
			else
			{
				//Error Occured
				$message = "There were ". count($errors) . " error in the form";
			}


}

?>

 

 

 

I'm having problem on this line

 

<?php
if(!isset($_POST[$fieldname]) || (empty($_POST[$fieldname]) && $_POST[$fieldname] != 0))
?>

 

 

this line is for validation of my radio button which has a 0 and 1 value.

 

&& $_POST[$fieldname] != 0

 

 

 

Eventhough the textbox is not set and is empty, it's still bypassing my if statement and still executing the update query...

 

 

Link to comment
Share on other sites

empty($_POST[$fieldname]) && $_POST[$fieldname] != 0

 

Will never be true.

 

Anything that would resolve to 0 would also resolve TRUE with empty.

 

You could try using !== '0'. $_POST values will always be strings, and the !== forces it to equal in value and type.

Link to comment
Share on other sites

empty($_POST[$fieldname]) && $_POST[$fieldname] != 0

 

Will never be true.

 

Anything that would resolve to 0 would also resolve TRUE with empty.

 

You could try using !== '0'. $_POST values will always be strings, and the !== forces it to equal in value and type.

 

Thank you very much!

 

:)

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.