Notice: Undefined index: SESS_NAME in student_vote.php on line 9 - php

<?php
include "connection.php";
if(!isset($_SESSION))
{
session_start();
}
$cand1 = $_POST['cand1'];
$cand2 = $_POST['vice1'];
$sess = $_SESSION['SESS_NAME'];
if(!$cand1){
$error="<center><h4><font color='#FF0000'>Please fill empty fields</h4></center></font>";
include"student.php";
exit();
}
$cand1 = addslashes($cand1);
$cand1 = mysqli_real_escape_string($con,$cand1);
$sql = 'SELECT * FROM student WHERE username="'.$_SESSION['SESS_NAME'].'" AND status="VOTED"';
$result = mysqli_query($con,$sql);
if (mysqli_num_rows($result)==1){
$msg="<center><h4><font color='#FF0000'>You have already been voted, No need to vote again</h4></center></font>";
include 'student.php';
exit();
}
else{
$sql = 'UPDATE candidate SET votecount = votecount + 1 WHERE cand_id = "'.$_POST['cand1'].'" OR cand_id = "'.$_POST['vice1'].'"';
$sql2 = 'UPDATE student SET status="VOTED" WHERE username="'.$_SESSION['SESS_NAME'].'"';
$result = mysqli_query($con,$sql);
$result2 = mysqli_query($con,$sql2);
if(!$result && !$result2){
die("Error on mysql query".mysqli_error());
}
else{
$msg="<center><h4><font color='#FF0000'>Congratulation, you have made your vote.</h4></center></font>";
include 'student.php';
exit();
}
}
?>
the errors are at code including
$sess= $_SESSION['SESS_NAME']; and at username="'.$_SESSION['SESS_NAME'].'"
i have tried every possibility so can you check my code?
mainly the error is undefined index at $session[session_name]?

Are you sure the session 'SESS_NAME' is getting set? In the code above you are only trying to access its value. Undefined index is the error thrown when there is no session with that name.

Looks like to me you haven't declared the variable $_SESSION['SESS_NAME']. Try doing the following
var_dump($_SESSION);
This will show you all the defined variables in the session, if SESS_NAME isn't there then it hasn't been defined. Maybe you have failed to define it somewhere else?

Related

Notice: Undefined index while deleting the item

I am trying to delete an entry, But constantly getting these errors:
Notice: Undefined index: wineTypeID in
/storage/ssd5/765/3453765/public_html/wine_action.php on line 33
Notice: Undefined index: wineName in
/storage/ssd5/765/3453765/public_html/wine_action.php on line 34
Notice: Undefined index: wineSize in
/storage/ssd5/765/3453765/public_html/wine_action.php on line 35
Notice: Undefined index: winePrice in
/storage/ssd5/765/3453765/public_html/wine_action.php on line 36
my code looks like this
<?php
// include the connection stream include 'connectYes.php';
// here are the two hiddent fields
// they tell the script is this is an add, update or delete
// and for an update and delete, they pass the id
$action = $_REQUEST["action"];
$id = $_REQUEST["id"];
$wineTypeID = $_REQUEST["wineTypeID"];
$wineName = $_REQUEST["wineName"];
$wineSize = $_REQUEST["wineSize"];
$winePrice = $_REQUEST["winePrice"];
if ($action == 'a') {
$query = "insert into WINE values (
null,
'$wineTypeID',
'$wineName',
'$wineSize',
'$winePrice'
)";
mysqli_query($conn,$query)
or die (mysqli_error());
print "<h3>The Item $wineName is added to the List</h3>";
" </h3>";
}
if ($action == 'u') {
$query = "update WINE
set wineTypeID = '$wineTypeID',
wineName = '$wineName',
wineSize = '$wineSize',
winePrice = '$winePrice'
where wineID = '$id'";
mysqli_query($conn, $query)
or die(mysqli_error());
print "<h3>Update Successful</h3>";
} // end u
if ($action == 'd') {
$query = "delete from WINE
where wineID = '$id'";
mysqli_query($conn, $query)
or die("query failed:" . mysqli_error());
print "<h3>Delete Successful</h3>";
}
?>}
Links to my page :- my page
Link to my files :- Php Files
mysql structure Image :-Mysql structure
Please check you request parameters whether "wineTypeID" paremeter is passing or not.

PHP NOTICE:Undefined offset 1

I am facing a problem that undefined offset :1. I can't understand that what type of error it is. Can anyone tell me that why such error occurs in php
<?php
$host="localhost";
$dbusername="root";
$dbpassword="";
$dbname="school";
$conn=mysql_connect($host,$dbusername,$dbpassword) or die(mysql_error());
mysql_select_db($dbname) or die(mysql_error());
if(isset($_POST['submit']))
{
$cls=$_POST['hhcls'];
{
{
{
$query=mysql_query("select * from stu_class where class='$cls'",$conn);
while($data=mysql_fetch_row($query))
{
$r1=$data[0];
$r=$_POST[$r1]; //THIS LNE ERROR
echo $r;
$query1="update stu_class set rollno=".$r." where ad_no=".$r;
$sql_query1=mysql_query($query1) or die(mysql_error());
if($sql_query1)
{
echo "<script type='text/javascript'>alert('Students Details Updated Successfully');</script>";
}
else
{
echo "<script type='text/javascript'>alert('Updation Failed');</script>";
}
}
}
}
}
}
?>
I am getting error while passing data to another query
Of course )
Look:
$query = mysql_query("select * from stu_class where class='$cls'", $conn);
// Now we got not query, but result of this query in $query
while ($data = mysql_fetch_row($query)) {
// Every iteration we will have in $data array (not associated)
// of fields (next row from database)
$r1 = $data[0];
// After this line we have value of first column of current line in $r1
// usually this is integer ID field and if first ID in database is 1,
// then $r1 = 1
$r = $_POST[$r1]; //THIS LNE ERROR
// here you trying to set $r = $_POST[1] because $r1 = 1
// but you have no any POST variable with key 1.
When you trying to get some array member that doesn't exist, you will get notice "Undefined offset %key%". In strict mode you will get exception.

Unable to get session variable to save into my database

Can anyone help me in getting the group_id from a session and save into the database, it seems not to working, been working on it for a while now. The error i am getting is Notice: Undefined index: group_name
This is my script
include('db.php')
//Get User Info
if(isset($_SESSION['username'])){
$LoggedUser = $_SESSION['username'];
if($GetUser = $mysqli->query("SELECT * FROM users WHERE username='$LoggedUser'")){
$UserInfo = mysqli_fetch_array($GetUser);
$LoggedUsername = strtolower($UserInfo['username']);
$LoggedUserLink = preg_replace("![^a-z0-9]+!i", "-", $LoggedUsername);
$LoggedUserLink = strtolower($LoggedUserLink);
$UserId = $UserInfo['user_id'];
$GetUser->close();
}else{
printf("Error: %s\n", $mysqli->error);
}
}
//Get Group info
if(isset($_SESSION['group_name'])){
$LoggedGroup = $_SESSION['group_name'];
if($GetGroup = $mysqli->query("SELECT * FROM groups WHERE group_name='$LoggedGroup'")){
$GroupInfo = mysqli_fetch_array($GetGroup);
$LoggedGroupname = strtolower($GroupInfo['group_name']);
$LoggedGroupLink = preg_replace("![^a-z0-9]+!i", "-", $LoggedGroupname);
$LoggedGroupLink = strtolower($LoggedGroupLink);
$GroupId = $GroupInfo['group_id'];
$GetGroup->close();
}else{
printf("Error: %s\n", $mysqli->error);
}
}
//getting variables and inserting into a database
if($_POST)
{
$User = $UserId;
$Group = $GroupId;
$mysqli->query("INSERT INTO tb_name(group_id_fk, user_id_fk) VALUES ('$Group', '$User')");
die('<div class="alert alert-success" role="alert">You have been added successfully to the group.</div>');
}else{
die (mysqli_error());
}
?>
Thanks
A session is started with the session_start() function.
Session variables are set with the PHP global variable: $_SESSION.
<?php
// Start the session
session_start();
// Set session variables
if(isset($_SESSION['username'])) {
---- Your Statements -----
}
?>
you need to start a session first by session_start(); before using $_SESSION global

Undefine index: session error

Whenever someone login $_SESSION['semail']; and $_SESSION['username2'] are set. Whenever I go to profile page then I set session into a variable like $email = $_SESSION['semail'];
Then in error log an error is recorded as:
Undefined index: semail in /home/twekr/public_html/profile.php on line 11
My session start like this at top of page:
session_name('twekr');
session_start();
ob_start();
Here is the small part of script which is creating error:
<?php
session_name('twekr');
session_start();
ob_start();
if(!isset($_SESSION['semail']))
{
header("Location:https://www.twekr.com/");
}
if(isset($_GET['user']))
{
$id = $_GET['user'];
$user = $_SESSION['semail'];
$userid1 = $_SESSION['username2'];
include_once('config/db.php');
include_once('frnds/functions.php');
require('config/dbsettings.php');
$userquery= "SELECT * FROM users WHERE email='$user'";
$userquery=$sp->query($userquery) or die($sp->error);
$info=$userquery->fetch_object();
$userr = $info->username;
This is the small part of login script where I set session:
$_SESSION['username2'] = $get['username'];
$_SESSION['semail'] = $uemail;
$get is performed here below:
$check_login = mysqli_query($con, "SELECT username FROM users WHERE
email ='$uemail' AND password ='$upass'");
if(mysqli_num_rows($check_login) ==1) {
$get = mysqli_fetch_array($check_login);
$_SESSION['username2'] = $get['username'];

Undefined property: stdClass

I'm getting the following error on these lines
Notice: Undefined property: stdClass::$id on line 36
Notice: Undefined property: stdClass::$id on line 40
Notice: Undefined property: stdClass::$screen_name on line 40
Line 36:
$vOAuth1 = mysql_query("SELECT * FROM `atwitter_oauth_users` WHERE `oauth_provider` = 'twitter' AND `oauth_uid` = '{$oUserInfo->id}'"); // searching for user in database
Line 40:
mysql_query("INSERT INTO `atwitter_oauth_users` (`oauth_provider`, `oauth_uid`, `username`, `oauth_token`, `oauth_secret`) VALUES ('twitter', {$oUserInfo->id}, '{$oUserInfo->screen_name}', '{$aAccessToken['oauth_token']}', '{$aAccessToken['oauth_token_secret']}')");
This is the whole code:
<?php
ini_set('display_startup_errors',1);
ini_set('display_errors',1);
error_reporting(-1);
require_once('inc/header.php');
require_once('inc/twitteroauth.php');
global $sConsumerKey, $sConsumerSecret;
session_start();
if (! empty($_GET['oauth_verifier']) && ! empty($_SESSION['oauth_token']) && ! empty($_SESSION['oauth_token_secret'])) {
} else { // some params missed, back to login page
header('Location: http://www.domain.com/livetweeter/tw_login.php');
echo "parameters missed";
}
$oTwitterOauth = new TwitterOAuth($sConsumerKey, $sConsumerSecret, $_SESSION['oauth_token'], $_SESSION['oauth_token_secret']);
$aAccessToken = $oTwitterOauth->getAccessToken($_GET['oauth_verifier']); // get access tokens
$_SESSION['access_token'] = $aAccessToken; // saving access token to sessions
$oUserInfo = $oTwitterOauth->get('account/verify_credentials'); // get account details
if(isset($oUserInfo->error)){
header('Location: http://www.domain.com/livetweeter/tw_login.php'); // in case of any errors - back to login page
echo "error";
} else {
global $sDbName, $sDbUserName, $sDbUserPass;
$vLink = mysql_connect($sDbHost, $sDbUserName, $sDbUserPass);
mysql_select_db($sDbName);
$vOAuth1 = mysql_query("SELECT * FROM `atwitter_oauth_users` WHERE `oauth_provider` = 'twitter' AND `oauth_uid` = '{$oUserInfo->id}'"); // searching for user in database
$aOauthUserInfo = mysql_fetch_array($vOAuth1);
if (empty($aOauthUserInfo)) { // if user info not present - add them into database
mysql_query("INSERT INTO `atwitter_oauth_users` (`oauth_provider`, `oauth_uid`, `username`, `oauth_token`, `oauth_secret`) VALUES ('twitter', {$oUserInfo->id}, '{$oUserInfo->screen_name}', '{$aAccessToken['oauth_token']}', '{$aAccessToken['oauth_token_secret']}')");
$vOAuth2 = mysql_query("SELECT * FROM `atwitter_oauth_users` WHERE `id` = '" . mysql_insert_id() . "'");
$aOauthUserInfo = mysql_fetch_array($vOAuth2);
} else {
mysql_query("UPDATE `atwitter_oauth_users` SET `oauth_token` = '{$aAccessToken['oauth_token']}', `oauth_secret` = '{$aAccessToken['oauth_token_secret']}' WHERE `oauth_provider` = 'twitter' AND `oauth_uid` = '{$oUserInfo->id}'"); // update tokens
}
$_SESSION['oauth_id'] = $aOauthUserInfo['id'];
$_SESSION['oauth_username'] = $aOauthUserInfo['username'];
$_SESSION['oauth_uid'] = $aOauthUserInfo['oauth_uid'];
$_SESSION['oauth_provider'] = $aOauthUserInfo['oauth_provider'];
$_SESSION['oauth_token'] = $aOauthUserInfo['oauth_token'];
$_SESSION['oauth_secret'] = $aOauthUserInfo['oauth_secret'];
mysql_close($vLink);
header('Location: http://www.domain.com/livetweeter/index.php');
echo "we've got an error";
}
if(!empty($_SESSION['oauth_username'])){
header('Location: http://www.domain.com/livetweeter/index.php'); // already logged, back to our main page
echo "this is an error";
}
?>
Note: I know I shouldn't be using mysql_query - I'll sort that once I have to script working.

Categories