Quantcast
Channel: PHP Freaks: PHP Help
Viewing all articles
Browse latest Browse all 13200

can't save data to database because of session error

$
0
0

i can't save data to my database and keep getting an error saying this

Notice: Undefined variable: _SESSION in /home/sn027/public_html/final project/createcat.php on line 10

could someone help me with this please thanks i want it so that only administrators with user_level of 1 are only able to create a category 


 

<?php  
include 'mysql.php';  

	//the user has admin rights
$name = isset($_POST['name']) ? trim($_POST['name']) : '';
$description = isset($_POST['description']) ? trim($_POST['description']) : '';

//Create variable to hold error message
$errorMsg = '';
if($_SESSION['loggedIn'] == false | $_SESSION['user_level'] != 1 )
{
	//the user is not an admin
	echo 'Sorry, you do not have sufficient rights to access this page.';
}
else
{
//Check if form was posted
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
    //Create array to hold errors
    $errors = array();

	 if(empty($name))  
    {
        $errors[] = 'The catergory name field must not be empty.';  
    }
 if(!ctype_alpha($name))
    {
        $errors[] = 'The catergory only contain letters.';  
    }  
    if(strlen($name) > 30)  
    {  
        $errors[] = 'catergory cannot be longer than 30 characters.';  
    }  
	if(empty($description))  
    {
        $errors[] = 'The catergory description field must not be empty.';  
    }
 //if(($description))
 //   {
  //      $errors[] = 'The catergory description must only contain letters.';  
  //  }  
    if(strlen($description) > 250)  
    {  
        $errors[] = 'catergory cannot be longer than 250 characters.';  
    }  
if(!empty($errors))
    {
        $errorMsg .= "fields are not filled in correctly...<br>\n";
        $errorMsg .= "<ul>\n";
        foreach($errors as $err)
        {
            $errorMsg .= "<li>{$err}</li>\n";
        }
        $errorMsg .= "</ul>\n";
    }
    else
    {
        //No errors attempt to create record

    //the form has been posted, so save it 
	 $sql = sprintf("INSERT INTO 
                    catagories(name, description) 
                VALUES('%s', '%s')",
					 mysql_real_escape_string($name),		
                    mysql_real_escape_string($description));
              
        $result = mysql_query($sql);  
        if(!$result)  
        {  
            echo 'Error. Please try again later'. mysql_error(); 
			exit();
        } 
        else 
        { 
            echo 'New category successfully added.'; 
			exit();
        } 
} 
}
}
?>
<?php include 'header.php'; ?>
<?php echo $errorMsg; ?>
<h3>Create Catergory</h3>
<form method="post" action="">
		   Catergory Name: <input type="text" name="name" value="<?php echo $name; ?>" /></p>
		   Catergory Description: </p><textarea name="description" /></textarea><?php echo $description; ?>
            <input type="submit" value="Add category" />
</form>
<?php include 'footer.php';  ?>

Viewing all articles
Browse latest Browse all 13200

Trending Articles