dekon Posted April 17, 2013 Share Posted April 17, 2013 i can't save data to my database and keep getting an error saying thisNotice: Undefined variable: _SESSION in /home/sn027/public_html/final project/createcat.php on line 10could 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'; ?> Quote Link to comment Share on other sites More sharing options...
lemmin Posted April 17, 2013 Share Posted April 17, 2013 Try adding session_start() to the beginning of your file Quote Link to comment Share on other sites More sharing options...
dekon Posted April 17, 2013 Author Share Posted April 17, 2013 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"> Quote Link to comment Share on other sites More sharing options...
lemmin Posted April 17, 2013 Share Posted April 17, 2013 Are you sure you are including your header in createcat.php? Quote Link to comment Share on other sites More sharing options...
dekon Posted April 17, 2013 Author Share Posted April 17, 2013 yes include header is near the bottom of the code in createcat.php Quote Link to comment Share on other sites More sharing options...
Solution lemmin Posted April 17, 2013 Solution Share Posted April 17, 2013 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. Quote Link to comment Share on other sites More sharing options...
dekon Posted April 17, 2013 Author Share Posted April 17, 2013 thank you it worked Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.