0

I am trying to add a new row to a table in my database, there is only two columns in the table and i am using a form to get one of the fields. here is the form i am using

<form action="add_list.php" method="post">
Name: <input type="text" name="listname">
<input type="submit" value="Add List">
</form>

and here is add_list.php

<?php
/* Execute a prepared statement by binding PHP variables */
$username = "mydb";
$password = "mydb"; 
$member = $_SESSION['SESS_MEMBER_ID'];
$listname = $_POST["listname"];
try {
    $dbh = new PDO('mysql:;dbname=mydb', $username, $password);
    $sth = $dbh->prepare('INSERT INTO lists VALUES (:member_id, :listname)');
    $sth->execute(array(':member_id' => $member, ':listname' => $listname));

    } catch(PDOException $e) {
    echo 'ERROR: ' . $e->getMessage();}
?>

I am currently getting an error on line five

Undefined variable: _SESSION in /home/danu2_cj3/public_html/add_list.php on line 5

how to I identify the session variable?

Here is the part of the login code i have set the session in

if($result) {
        if(mysql_num_rows($result) == 1) {
            //Login Successful
            session_regenerate_id();
            $member = mysql_fetch_assoc($result);
            $_SESSION['SESS_MEMBER_ID'] = $member['member_id'];
            $_SESSION['SESS_FIRST_NAME'] = $member['firstname'];
            $_SESSION['SESS_LAST_NAME'] = $member['lastname'];
            session_write_close();
            header("location: lists.php");
            exit();
        }else {
            //Login failed
            header("location: login-failed.php");
            exit();
        }
Johnnerz
  • 29
  • 1
  • 6

1 Answers1

2

Messages in the form of:

Undefined variable: _SESSION ...

are often caused by the lack of a session_start call at the beginning of the script (before the headers are sent and before any attempt to access a $_SESSION key is done).


I have tried that but it just give me a blank page and does not add it to the table.

Well, that means that you have at least solved the above problem. For the blank page you should search for white screen of death, which is well documented here as one of the most common problems in the PHP environment.

Community
  • 1
  • 1
Shoe
  • 74,840
  • 36
  • 166
  • 272