Jump to content

can't save data to database because of session error


dekon
Go to solution Solved by lemmin,

Recommended Posts

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';  ?>
Link to comment
Share on other sites

i have used session start in my header which is 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">  
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="nl" lang="nl">  
<head>  
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />  
    <meta name="description" content="A short description." />  
    <meta name="keywords" content="put, keywords, here" />  
    <title>Forum</title>  
    <link rel="stylesheet" href="style.css" type="text/css">  
<body>
<center><h1>SE Connect</h1><center>
<div id="wrapper">
<div id="menu">
<a class="item" href="home.php">Home<a/> -
<a class="item" href="events.php">Events<a/> -
<a class="item" href="forum.php">Forum<a/> -
<a class="item" href="createtop.php">Create Topic<a/> -
<a class="item" href="createcat.php">Create Catergory<a/> -

<div id="userbar">  
<?php  
session_start();
    if ($_SESSION['loggedIn'])  
		{
			echo 'Hello <b>' . htmlentities($_SESSION['username']) . '</b>. Not you? <a class="item" href="signout.php">Sign out</a>';
		}
		else
		{
			echo '<a class="item" href="signin.php">Sign in</a> or <a class="item" href="signup.php">create an account</a>';
		}
		?>
	</div>
<div id="content">
Link to comment
Share on other sites

  • Solution

Includes are done in-line linearly. This means that session_start() isn't being called until AFTER your first access of $_SESSION. In order for the $_SESSION variable to exist, session_start() must be called BEFORE it is accessed.

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.