Set session in foreach loop is not working - php

Am trying to set a session in foreach loop when users post a form so when they refresh the page the post values won't lost, but the session is not saving.
My Code
<?php session_start();
if(isset($_POST['EventLookUp'])){
/* city state zipcode country*/
$EventPostArgs = filter_input_array(INPUT_POST);
foreach($EventPostArgs as $pname => $pval){
$_SESSION[$pname] = htmlentities($pval);
}
echo $_SESSION['country'];
?>

Related

How to store a value in a session?

I am displaying the user names as links on a php page like this which on clicking navigates on particular user home page:
$msql=$db->prepare("SELECT * from users where id=? order by id desc");
$msql->bind_param("i",$user_id);
$msql->execute();
$msql = $msql->get_result();
$msql = $msql->num_rows;
while($usercount=$msql->fetch_assoc())
{
$Email = $usercount['email'];
$FirstName = $usercount['first_name'];
$LastName = $usercount['last_name'];
?>
<strong><?php echo $FirstName.' '.$LastName;?></strong>
<?php
}
?>
And for the navigation to the user home page on clicking the link, I am using like this :
<?php
if(isset($_GET['navigate']) && $_GET['navigate'] == "true"){
$_SESSION['email'] = $Email;
header('location: nextpage.php');
}
?>
So my page looks like this as link
user1
user2
user3
.
.
.
usern
My issue is whenever I click on any of the links it always stores the first email in the session variable.
So, if I output the SESSION in nextpage.php
echo $_SESSION['email'];
it always echoes the first link email.
My guess for this because of the while loop I am using it always picks up the first link data and stays with it, but my question is how do i get the data for others as well. I want to navigate to that user page for which the link is clicked and that can only be done if I get the correct email on clicking the link.
As you said id is not primary key, and i assume that your same id will contain different emails, you have to do below changes:-
<?php
$msql=$db->prepare("SELECT * from users where id=? order by id desc");
$msql->bind_param("i",$user_id);
$result = $msql->execute(); // assign to a variable
//$msql = $msql->get_result(); //you are over-writing to one variable which is not correct
//$msql = $msql->num_rows; //you are over-writing to one variable which is not correct
while($usercount=$result->fetch_assoc()){
$Email = $usercount['email'];
$FirstName = $usercount['first_name'];
$LastName = $usercount['last_name'];
?>
<strong><?php echo $FirstName.' '.$LastName;?></strong> <!-- send mail id to the url too otherwise how's you will get the email id to save it into SESSION-->
<?php } ?>
AND
<?php
if(isset($_GET['navigate']) && $_GET['navigate'] == "true"){
$_SESSION['email'][] = $_GET['email']; // assign each mail to SESSION ARRAY not SESSION variable
header('location: nextpage.php');
}
?>
AND
echo "<pre/>";print_r($_SESSION['email']); //to see all emails.
Note:- You have to write session_start(); on top of your php page just after <?php if you want to work with SESSION on that page.

php - unwanted session results

i am developing a web application,which has 3 pages.
first is index.php
which has search bar on which user searches.
second is search.php
which displays the search results like result_1,result_2,result_3 with info(title,description,url) when user click on any result it send the user to final page i.e show.php
and third page is show.php
on which info is displayed for the result which user has clicked.
for eg(corresponding url content will be displayed using iframe)
i tried using two dimensional session array,which is working not correctly.
when user click on any result,some other result's info is displaying on show.php
i check the session array content by print_r,it had unnecessary content.
someone help me in this i am sharing my code snippet.
search.php
<?php
session_start();
$id = 1;
while($row = mysqli_fetch_array($sql))
{
$title = $row['title'];
$description = $row['description'];
$url = $row['content_url'];
$icon = $row['thumb_icon_url'];
$_SESSION['result'][] = Array('title' => $title,'description'=> $description,'content_url' => $url,'icon' => $icon,'id'=> $id);
?>
<li name="id" >View doc</li>
<?php
$id++;
?>
show.php
<?php
if(isset($_GET['id']))
{
$id = $_GET['id'];
?>
<div>
<iframe class="embed-responsive-item item" src="<?php echo $_SESSION['result'][$id]['content_url'];?>"></iframe>
</div>
when i tried to check $_SESSION['result'] i got this
this array should have contain only query results.Help me to fix it
you're not setting the key of your array:
$_SESSION['result'] = Array();
$_SESSION['result'][$id] = Array('title' => $title,'description'=> $description,'content_url' => $url,'icon' => $icon,'id'=> $id);
add session_start(); in your show.php
If I understand correctly, your problem is that you have unwanted results.
This is basically because your are always adding results to the $_SESSION['results'] across all your queries.
Every time that you run search.php you will keep adding items to $_SESSION['result'], breaking the correspondence of the $id and the position of the array.
I would initialize the $_SESSION['result']
<?php
session_start();
$id = 1;
$_SESSION['results'] = [];
while($row = mysqli_fetch_array($sql)){
//...
}

Remove item from session array

I am having trouble removing an item from the itinerary/array upon clicking a 'Remove location' link.
The value of the location to be removed is passed into the 'remove_loc.php' file as its unique 'loc_id' and assigned to a new variable. Upon doing this I then try to use 'unset()' to delete it from the session array but it doesn't seem to be working.
The value of the location is definitely being passed through to the 'remove_loc.php' file ok as I have run an echo statement at other end to make sure, and am connecting ok to db etc, so I believe the error is in my remove_loc.php file.
Code for itinerary.php (where locations are displayed).Note 'Remove Location' href link:
<?php
session_start();
include ("includes/db.php");
include("functions/functions.php");
include("includes/head.html");
include("includes/search_box.html");
include("includes/left_sidebar.html");
//If user not already logged in then redirect to the login page
if (!isset($_SESSION['user_id'])){
load_page();
}//if
//Display contents of itinerary
if(!empty($_SESSION['itinerary'])){
//Retrieve details of each location in array from database
$query = "SELECT * FROM locations WHERE loc_id IN (";
foreach ($_SESSION['itinerary'] as $loc_id=>$value)
{$query.=$loc_id.',';}
$query = substr($query, 0, -1).')ORDER BY loc_id ASC';
$result = mysqli_query($db, $query);
echo'<form action="itinerary.php" method="POST"><table><tr><th colspan="5">Locations in your itinerary<br></br></th></tr><tr>';
//Display locations in array
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
$loc_id = $row['loc_id'];
echo"<tr><td>{$row['loc_name']}<br></br></td>
<td>{$row['loc_desc']}</td>
<td><a href=remove_loc.php?value=$loc_id>Remove location</a><br></br></td>
";
}//while
mysqli_close($db);
}//if
else {echo '<p>Your itinerary is empty.<br></br></p>';}
echo '<p>
<a href=submit.php?submit=$submission>Save itinerary</a><br></br>
Your details<br></br>
Logout<br></br>
</p>';
?>
Code for 'remove_loc.php' (for removing location from session array):
<?php
session_start();
include ("includes/db.php");
include("functions/functions.php");
include("includes/head.html");
include("includes/search_box.html");
include("includes/left_sidebar.html");
?>
<html>
<h1>REMOVE LOCATION</h1>
<br></br>
<?php
//If user not already logged in then redirect to the login page
if (!isset($_SESSION['user_id'])){
load_page();
}//if
//Assign ID of location which has been passed to new variable
if(isset($_GET['value']))
$loc_id = $_GET['value'];
//Remove location from sess array?
if(isset($_SESSION['itinerary'][$loc_id])) unset($_SESSION['itinerary'][$loc_id]);
echo 'Removal successful!';
print_r($_SESSION);
?>
</html>

Storing array in session and retrienving desired value

The code below stores array of post id's into session and using foreach I iterate over post id and call data(title,body) related to that post id. It works fine.
index.php
$result = $query->fetchAll(PDO::FETCH_COLUMN, 0);
$_SESSION['postid']=$result;
foreach($_SESSION['postid'] as $key=>$postid){
$sql = "SELECT * FROM posts WHERE postid=:postid";
$query = $db->prepare($sql);
$query->execute(array(':postid'=>$postid));
$row = $query->fetch(PDO::FETCH_ASSOC);
<?php echo $row['title']; ?>
<p><?php echo row['body'] ?></p>
}
Now I want to get directed to post page by clicking on title link from index page, and call that specific post id from session array whose title was clicked. Using $postid = $_SESSION['postid']; on post page gives the whole array of post id's . Is there any way by which I can start session on post page for only that specific post id whose title was clicked on index page.
In index.php page, modify the anchor tag like this :
<?php echo $row['title']; ?>
then on post page use this code to set postid in session
<?php
$_SESSION['pid'] = isset($_GET['pistid'])$_GET['pistid']:null?;
?>

How to create session for sql query result in php

I want to create session for the record tag_id here is my sql code please help me how i create session for this value..
<?php
$sql_tagid=mysql_query("SELECT * FROM tag_new WHERE EMAIL_ID='$sessionemail' AND CUST_ID='$id'") or die(mysql_error());
while($tagid=mysql_fetch_array($sql_tagid)){
echo $tagid['tag_id'];
}
?>
First you need to start session
session_start();
after that add variable to session
$_SESSION['tag'] = $tag['tag_id']
after that you can check by print session
print_r($_SESSION); you value in session or not
$_SESSION['tag'] = $tagid['tag_id']
Try this, if you have mutliple tag id then use it $_SESSION['tag_id'][], if you have only one $_SESSION['tag_id']
session_start();
while($tagid=mysql_fetch_array($sql_tagid)){
$_SESSION['tag_id'][] = $tagid['tag_id'];
}
var_dump($_SESSION['tag_id']);
<?php
session_start(); // To assign a session variable you have to start the session
$sql_tagid=mysql_query("SELECT * FROM tag_new WHERE EMAIL_ID='$sessionemail' AND CUST_ID='$id'") or die(mysql_error());
while($tagid=mysql_fetch_array($sql_tagid))
{
$_SESSION['tag_id'] = $tagid['tag_id']; // assign tag_id value to session variable
echo $tagid['tag_id'];
}
?>
For getting the value of session on another page say page1.php then you have to do :-
page1.php
<?php
session_start();
echo $_SESSION['tag_id'];
?>

Categories