Session variable cannot be passed into query - php

page1.php has this code
<?php
session_start();
$_SESSION['supp_id'] = 4;
?>
page2.php has this code
<?php
session_start();
include "db2.php";
$supp_id = $_SESSION['supp_id'];
var_dump($supp_id);
if(isset($_POST['accept']))
{
$cust_id = $_POST['cust_id'];
$cartype = $_POST['cartype'];
$q=mysqli_query($conn,"INSERT INTO `availablesupp` (`Cust_ID`,`Supp_ID`,`CarType`) VALUES ('$cust_id','$supp_id','$cartype')");
if($q)
echo "ok";
else
echo "error";
}
?>
$supp_id did not get passed into the query, and the database is updated with Supp_ID = 0;
However changing $supp_id = $_SESSION['supp_id'] to $supp_id = 4 works fine. var_dump produced identical results in both cases, int(4).
How do I solve this?

Related

Store array in session variable and post the session variable into another page

This is my code in 1st page,
<?php
session_start();
require 'dataconnection.php';
$res = mysql_query("select * from questions where category_id=1 LIMIT 20") or die(mysql_error());
$rows = mysql_num_rows($res);
echo $rows;
while ($result=mysql_fetch_assoc($res)) {
for($i=1;$i<=$rows;$i++)
{
$_SESSION['questions']=$result;
}
echo implode("",$_SESSION['questions']);
}
?>
In next page my code
<?php
session_start();
echo implode(",",$_SESSION['questions']);
?>
This part of the code doesn't add to an array, but overwrite the value in it. It simply sets $_SESSION['questions'] to the same value as $result x times, where x is the number of rows.
while ($result=mysql_fetch_assoc($res)) {
for($i=1;$i<=$rows;$i++)
{
$_SESSION['questions']=$result;
}
}
Try changing it to:
while ($result=mysql_fetch_assoc($res)) {
$_SESSION['questions'][]=$result;
}

PHP/MYSQL - How do i assign data fetched from the database(stored in variables) to a session variable?

I am having a bit of an issue passing array variables that are fetched from the database and stored in variables and passing them into session variables to use all over the site.
Code:
$rid=$_SESSION['SESS_MEMBER_ID'] ;
$recquery = 'SELECT * FROM clinic_receptionist,clinic_table where clinic_receptionist.recep_clinic_id = clinic_table.ID AND recep_id ='.$rid;
$recresult = mysqli_query($conn,$recquery);
if(mysqli_num_rows($recresult)>0)
{
while($row = mysqli_fetch_assoc($recresult))
{
$cid = $row['clinic_id'];
$cname = $row['clinic_name'];
$name = $row['recep_full_name'];
$phone = $row['recep_mobile'];
$email = $row['recep_email'];
$address = $row['recep_address'];
$gender = explode(",",$row['recep_gender']);
$dob = $row['recep_dob'];
$doj = $row['recep_doj'];
}
}
?>
The above code is where the select query is happening. I wanted to know how do I assign $cid and $cname to a $_SESSION variable in different pages. Any help would be much appreciated. Thanks
CODE:
<?php session_start();
include('header.php');
include('menu.php');
include('config.php');
$appquery = 'SELECT * FROM appointment_table,clinic_table where clinic_table.ID = appointment_table.clinic_id';
$appresult = mysqli_query($conn,$appquery);
if(mysqli_num_rows($appresult)>0)
{
while($row = mysqli_fetch_array($appresult))
{
$_SESSION['cid'] = $row['ID'];
$_SESSION['cname'] = $row['clinic_name'];
$ptdate = $row['date'];
$pttime = $row['time'];
$ptname = $row['pt_name'];
$ptphone = $row['pt_ph_num'];
$ptemail = $row['pt_email_id'];
}
}
?>
The above code is the place from where I want to receive the id and clinic name.
<?php
// Start the session
session_start();
//Assigning the values to $_SESSION
$_SESSION['cid'] = $row['clinic_id'];
$_SESSION['cname'] = $row['clinic_name'];;
?>
session_start
You need to call session_start() on each page to initialize the session, of course; after that you can just
$_SESSION['cid'] = $cid;
$_SESSION['cname'] = $cname;
to store those values in $_SESSION.
By the way, someone's going to tell you to use a prepared statement for that query, so it might as well be me.
Perhaps an illustration is in order.
page1.php:
<?php
session_start(); // find session, or create new one
include('header.php');
include('menu.php');
include('config.php');
$appquery = 'SELECT * FROM appointment_table,clinic_table where clinic_table.ID = appointment_table.clinic_id';
$appresult = $conn->query($appquery);
if(($rowcount = $appresult->num_rows()) > 0)
{
// create arrays to pass data
$cids = array($rowcount);
$cnames = array($rowcount);
$i = 0;
while($row = $appresult->fetch_array())
{
// add values to arrays
$cids[$i] = $row['ID'];
$cnames[$i] = $row['clinic_name'];
$i++;
}
// add arrays to session
$_SESSION['cids'] = $cids;
$_SESSION['cnames'] = $cnames;
}
page2.php:
<?php
session_start(); // find session, or create new one
include('header.php');
include('menu.php');
include('config.php');
// retrieve arrays from session
$cnames = $_SESSION['cnames'];
$cids = $_SESSION['cids'];
// ... and print them
$count = count($cids);
for ($i = 0; $i < $count; $i++)
{
echo "cid = " . $cid[$i] . "; cname = " . $cname[$i] . "<br>";
}
page1.php performs the query and stores cid and cname values in arrays which are then stored in $_SESSION. page2.php retrieves the arrays from $_SESSION and echoes them out. In production, of course, it would be considered more efficient to run the query on the page where it is displayed, and only pass key values between pages.
I've also used object syntax instead of procedural syntax for mysqli, just to show what it looks like. As usual, I leave error handling as an exercise for the reader.

Session variable does not persist

I can't seem to get my session variable to output. It works fine on the other one I'm using:
$_SESSION['status']['register']['username'] = $username;
header('Location: register-form.php');
When you are taken to register-form.php it should output the variable for you:
<?php
if(isset($_SESSION['status']['register']['username'])){
$username = $_SESSION['status']['register']['username'];
} else {
$username = '';
}
?>
Exact same code is used on email_address and works fine.
Here is the same code for the email
$_SESSION['status']['register']['email_address'] = $email_address;
And on the form page:
<?php
if(isset($_SESSION['status']['register']['email_address'])){
$ea_value = $_SESSION['status']['register']['email_address'];
} else {
$ea_value = '';
}
?>
You miss to initialize your session with
session_start();
at top of your register_form.php

Array not displaying, Notice: array to string conversion

I am attempting to display the contents of an array. I have a SELECT query that transfers two rows from the data base with a WHERE statement;
<?php
session_start();
include 'connection.php';
$inspectit = array();
$update1 = $_POST['inspect'];
$query1 = "SELECT title, reason FROM daysoff WHERE DATE(start) = '$update1' AND gighold = 1";
$day = mysql_query($query1);
while($requesting = mysql_fetch_array($day)) {
$inspectit[] = $requesting;
}
$_SESSION['inspect'] = $inspectit;
header('Location: '. $_SERVER['HTTP_REFERER']) ;
?>
I place the session variable into a new variable,$inspect, defined on a different page. I then try to display the array using;
<?php
session_start();
$inspect = $_SESSION['inspect'];
if($inspect == true) {
echo " </br>Name: </br><a> ". implode($inspect) ."</a></br></br>";
unset ($_SESSION['inspect']);
}
?>
If i var_dump($inspect) all the expected data is there, but it looks like an array holding another array. If i execute code as shown above, this gives a notice of array to string conversion. Where am i going wrong?
<?php
session_start();
$inspect = $_SESSION['inspect'];
if($inspect) {
echo " </br>Name: </br><a> ". implode($inspect) ."</a></br></br>";
unset ($_SESSION['inspect']);
}
?>
You are adding an array of query result into an array. try to change:
from:
while($requesting = mysql_fetch_array($day)) {
$inspectit[] = $requesting;
}
to:
$inspectit = mysql_fetch_array($day);
$_SESSION['inspect'] is an array, so you can used check like
$inspect = $_SESSION['inspect'];
if(count($inspect)>0) {
instead of
if($inspect == true) {
try
<?php
session_start();
$inspect = $_SESSION['inspect'];
if($inspect) {
echo " </br>Name: </br><a> ". implode("-",$inspect) ."</a></br></br>";
unset ($_SESSION['inspect']);
}
?>
or you can use empty() function too like
<?php
session_start();
$inspect = $_SESSION['inspect'];
if(!empty($inspect)) {
echo " </br>Name: </br><a> ". implode("-",$inspect) ."</a></br></br>";
unset ($_SESSION['inspect']);
}
?>
With $inspectit = array(); you are setting $inspectit to an array, then you are trying to set the string (or non-array) $_SESSION['inspect'] to equal that array. You can iterate multiple $_SESSION variables to each iteration of $inspectit as follows:
$n=0;
while($requesting = mysql_fetch_array($day)) {
$_SESSION['inspect'+$n] = $requesting;
$n++;
}
$_SESSION['inpect_count']=$n;
Thus you will know how many variables you setup with the $_SESSION['inpect_count']
I tried all above methods to no avail. I did however get it to work by doing;
<?php
session_start();
include 'connection.php';
$inspectname = array();
$inspectreason = array();
$update1 = $_POST['inspect'];
$query1 = "SELECT title, reason FROM daysoff WHERE DATE(start) = '$update1' AND gighold = 1";
$day = mysql_query($query1);
while($requesting = mysql_fetch_array($day)) {
$inspectname[] = $requesting['title'];
$inspectreason[] = $requesting['reason'];
}
$_SESSION['inspectname'] = $inspectname;
$_SESSION['inspectreason'] = $inspectreason;
header('Location: '. $_SERVER['HTTP_REFERER']) ;
?>
then to call and display the arrays;
<?php
if($inspectname == true) {
$size = sizeof($inspectname) - 1;
for($count = 0; $count <= $size; $count++){
echo " </br>Name: </br><a> ". $inspectname[$count] ."</a>";
echo " </br>Details: </br><a> ". $inspectreason[$count] ."</a></br></br>";
unset ($_SESSION['inspectname']);
unset ($_SESSION['inspectreason']);}
}
?>

PHP echo array data

I want to access the data in the php array. I have tried array[0] and array['Name'] but i dont get an output. here is my code:
session_start();
$user = $_SESSION['username'];
$sql_1 = "SELECT UserID FROM users WHERE username=$user";
$result_1 = mysqli_query($sql_1);
$uID = mysqli_fetch_array($result_1);
if ($uID=NULL) {
echo 'null';
} else {
echo $uID[0];
}
Now I am not getting any output from the echo command. So what am i doing wrong here?
The Part if ($uID=NULL) is always true[UPDATED:false], because you're doing an assignment rather than a comparism (that would be if ( $uID == NULL ))

Categories