I have a page on my site that I am making that is to upload comments to a community section from the users profile page.
The page was working fine but I needed to add a 'username' foreign key to the table to identify who made the post, and so I am trying to use the session function to do this.
In the code below, the three functions I added (before which the page was working correctly) are the $date, $time, and $_SESSION['username']=$_POST['username'].
The $date and $time variables are for timestamping the post, and along with the $_SESSION['username']=$_POST['username'], are going to be the primary key for the table.
Since I added those three variables, (yes, they are added in the table) I am getting an error:
Notice: Undefined index: username in C:\xampp\htdocs\soiree'_2\create_post_script.php on line 12
Notice: Undefined index: username in C:\xampp\htdocs\soiree'_2\create_post_script.php on line 22 failure
It seems to have a problem with the $_SESSION variables. So with that being said, is my implementation of the session variable incorrect?
As you can see below the page begins with a 'start_session();' function, so does my log in script. The way I understand this to work is when my user logs on, the $_POST['username'] is assigned to $_SESSION['username'], so that on the subsequent pages, like when they go to make their post or comment, $_SESSION['username'] captures the credentials to insert into the column of the table for 'username'.
Perhaps obvious this is my first time using session variables/functions. Below is the code for the page in question (the comment page) and the script for the login below. Thanks.
<?php
session_start();
include('db.php');
$event_name='';
$place='';
$t='';
$d='';
$description='';
$_SESSION['username'];
$time='';
$date='';
$event_name=strip_tags($_POST['event_name']);
$place=strip_tags($_POST['place']);
$t=strip_tags($_POST['time']);
$d=strip_tags($_POST['date']);
$description=strip_tags($_POST['event_description']);
$_SESSION['username']=strip_tags($_POST['username']);
$time=time();
$date=date("y-m-d");
if(isset($_POST['submit']))
{
$query = "INSERT INTO user_posts (date, time, username, title, location, t, d, description)";
$query .= "VALUES ('$date','$time','$_SESSION["username"]','$event_name','$place','$t','$d','$description')";
if (mysqli_query($connection, $query)) {
echo "<h2> your post has been submitted </h2>";
}
else {
die('failure');
}
}
and the following is from the login script;
<?php
session_start();
include("db.php");
if(isset($_POST['submit'])) {
$username = $_POST["username"];
$password = $_POST["password"];
$u_check= mysqli_query($connection, "SELECT username AND password FROM registration WHERE username='$username' AND password='$password'");
$check=mysqli_num_rows($u_check);
if($check==1) {
$_SESSION['auth'] = "yes";
$_SESSION['username'] = $_POST['username'];
header("location: home.php");//echo "succesful login";
}
else {
echo "invalid login info";
$result=mysqli_query($connection, $query);
if(!$result){
die('failure');
}
}
}