Fatal Error in PHP coding

gazzieh

New Member
I am encountering an error with the following section of PHP and cannot see the reason. The error is being trapped with the line

Code:
$validator->validateTextOnlyNoSpaces($HTTP_POST_VARS['name'],'section name');

The error message is:
Fatal error: Call to a member function validateTextOnlyNoSpaces() on a non-object in C:\Program Files\xampp\htdocs\newsite2\cmsadmin\newSection.php on line 33

Now, this implies that the $HTTP_POST_VARS field of 'name' is not in existence and yet I have declared it clearly within the form at the bottom of the code. The submit is set correctly.

Anyone see what I am missing?

Code:
<?php
	// Call the classes
	require_once('../includes/DbConnector.php');
	require_once('../includes/Validator.php');
	
	// Create instances
	$connector = new DbConnector();
	$theValidator = new Validator();	
	
	// DELETE SECTION ////////////////////////////////////////////////////////////////////
	if  ($HTTP_GET_VARS['action'] == 'delete')
		{
    	// Store the ID of the section to be deleted in a variable
    	$sectionID = $HTTP_GET_VARS['id'];

    	// Validate the section ID, and if it's ok then delete the section
    	if ( $validator->validateNumber($sectionID,'Section ID') )
			{
        	// The validator returned true, so go ahead and delete the section
        	$connector->query('DELETE FROM cmssections WHERE ID = '.$sectionID);
        	echo 'Section Deleted <br><br>';
		    }
		else
			{
        	// The validator returned false, meaning there was a problem
        	echo "Couldn't delete. There was a problem with: ".$validator->listErrors();
    		}
		}
		
	// ADD SECTION ////////////////////////////////////////////////////////////////////
	if ($HTTP_GET_VARS['action'] == 'add')
		{
		$validator->validateTextOnlyNoSpaces($HTTP_POST_VARS['name'],'section name');
		$validator->validateNumber($HTTP_POST_VARS['parent'],'parent section');
	
		if (!$validator->foundErrors())
			{
			$connector->query('INSERT INTO cmssections (name,parentid) VALUES ("'.$HTTP_POST_VARS['name'].'",'.$HTTP_POST_VARS['parent'].')');	
			}
		else
			{
			echo '<b>Please correct '.$validator->listErrors().'</b><br><br>';
			}
		}

	// LIST SECTIONS /////////////////////////////////////////////////////////////////////
	$result = $connector->query('SELECT ID,name,parentid FROM cmssections');

	// Get an array containing the results.
	// Loop for each item in that array
	while ($row = $connector->fetchArray($result))
		{
		echo $row['name'].' - &nbsp;&nbsp; '; // Show the name of section
		echo '<a href="newSection.php?action=delete&id='.$row['ID'].'"> Delete </a>'; // Show the delete link 
		echo '<br>'; // Show a carriage return
		}

?>

	<form name="form1" method="post" action="newSection.php?action=add">
  		<p>Create a Section:</p>
  		<p>&nbsp;Name:
      		<input name="name" type="text" id="name">
  		</p>
  		<p>&nbsp;Parent:
    		<select name="parent">
				<option value="0">None</option>
      			<?PHP
            		// Generate a drop-down list of sections.
					// NOTE : Requires database modifications in article 4
					$result = $connector->query('SELECT ID,name FROM cmssections ORDER BY name');

					// Get an array containing the results.
					// Loop for each item in that array
					while ($row = $connector->fetchArray($result))
						{
						echo '<option value="'.$row['ID'].'">'.$row['name'].'</option>';
						}
			  	?>
    	    </select>
  		</p>
  		<p align="left">
    		<input type="submit" name="Submit" value="Create">
  		</p>
	</form>
 

logoarena

New Member
I should see the Validator.php class code, anyway I believe the problem is here:

you make a new Validator object doing this:

$theValidator = new Validator();

then you call a method (validateTextOnlyNoSpaces) of that class doing this:

$validator->validateTextOnlyNoSpaces($HTTP_POST_VARS['name'],'section name');

but I believe you should call it in $theValidator object, not in $validator, which doesn't exist.

try changing $theValidator = new Validator(); into:

$validator = new Validator();
 
Top