I am trying to send two array over session and then combined them to display together but nothing displayed.This is my code--->
<?php
session_start();
function tee($uid){
include 'connect.php';
$uid=$uid;
$parent=array();
$child=array();
$_SESSION["child"]=array();
$_SESSION["parent"]=array();
$sql="select id from relation2 where parentID=$uid";
$result=mysql_query($sql,$link);
while($row=mysql_fetch_assoc($result))
{
$parent[]=$uid;
$child[]=$row["id"];
$_SESSION["child"][]=$child;
$_SESSION["parent"][]=$parent;
tee($row["id"]);
}
}
tee(2);
foreach(array_combine($_SESSION["child"],$_SESSION["parent"]) as $child=>$parent)
{
echo $child.'----------->'.$parent;
echo '<br>';
}
?>
I replaced the the query output with something custom and this code worked for me
<?php
session_start();
function tee($uid){
include 'connect.php';
$parent=array();
$child=array();
$_SESSION["child"]=array();
$_SESSION["parent"]=array();
$sql="select id from relation2 where parentID=$uid";
$result=mysqli_query($sql,$link);
while($row=mysqli_fetch_assoc($result))
{
$_SESSION["child"][]=$row["id"];
$_SESSION["parent"][]=$uid;
}
tee(2);
foreach(array_combine($_SESSION["child"],$_SESSION["parent"]) as $child=>$parent)
{
echo $child.'----------->'.$parent;
echo '<br>';
}
?>
Also check the extract() function
If you pass $uid from user input use PDO
Related
The page is unable to see the login form whose code is written under the isset function statement. I have written the code correctly and have executed it many times , but now the code written inside the isset statement does not works. here is the code:-
<?php
session_start();
echo "<p style=\"font-color: #ff0000;\"> Catogoies </p>";
echo '<link href="var/www/html/sample.css" rel="stylesheet">';
require_once('../html/conn.php');
$query = "select * from catogories";
mysqli_select_db($dbc, 'odit');
$retrieve = mysqli_query($dbc, $query);
if(!$retrieve)
{
die(mysqli_error($query));
}
while($row=mysqli_fetch_array($retrieve, MYSQL_ASSOC)){
echo "<p style=\"font-color: #ff0000;\">".''.$row["Name"].''."</p>";
$_SESSION['cat']=$row["Name"];
}
if(!($_SESSION)) {
session_start();
}if(isset($_SESSION['lgout']))//the variable logout intialization line
{
if($_SESSION['lgout']!=1||$_SESSION['signup']){
echo "Hello : ".''.$_SESSION['unme'].''; echo "<br><br>";
echo '<a href="logout.php">'."Logout";}
else {
include 'lform.php'; echo "<br><br>";
echo '<a href="Sign_up.php">'."Sign up"."<br>";
} }
mysqli_close($dbc);
//include 'lform.php';
?>
<br>
<a href = 'adding_catogory.php'>Create a New Catogory</a><br><br>
<a href = 'Log_in.php'></a>
<?php
$db = #mysqli_connect("localhost", "oddittor", "Odit#123", "odit");
if(isset($_POST['login'])){
$username=mysqli_real_escape_string($db, $_POST['l_id']);
$password=mysqli_real_escape_string($db, $_POST['pswd']);
$sql="SELECT * from users where usrName='$username' and pswrd = '$password'";
$result = mysqli_query($db, $sql) or die(mysqli_error($db));
$count=mysqli_num_rows($result) or die(mysqli_error($db));
if($count>0) {
$_SESSION['unme']=$username; //This is the global session variable...used for storing the variables across the pages.
$_SESSION['lgout']=0;
header('Location : session.php'.$_SESSION['unme']);
header("Location : Homepage.php".$_SESSION['unme'].$_SESSION['lgout']); header( "refresh:0;url=Homepage.php" );
$_SESSION['unme']=$username;
}
else {
$error = "Invalid Details! Please Renter them"; }
}
?>
Here the problem is in the
if(isset($_SESSION['lgout']))
line if, I remove this line i can see the login page form but by doing so, I get the error of undefined variable logout whenever, I open the page for the first time.
here is the logout script
<html>
<?php
session_start();
$_SESSION['lgout']=1;
$_SESSION['signup']=0;
echo ' You have been successfully logged out';
header('Location : Homepage.php'.$_SESSION['lgout']);header( "refresh:0;url=Homepage.php" );
?>
</html>
You need to put your
session_start();
globally on the start of page. As it's not able to get $_SESSION object.
Just remove
session_destroy();
As you can access all $_SESSION values.
Your queries not secured. Use Prepared Statements instead of your all queries.
http://php.net/manual/en/mysqli.quickstart.prepared-statements.php
This is my PHP code page
<?php
session_start();
//include database connection
include_once("dbconn.php");
//afetr click the submit button the below code will be impelemnted
if(isset($_POST['id'])){
$id=$_POST['id'];
$Type=$_POST['Type'];
$Avalibility=$_POST['Avalibility'];
# insert data into MySQL database
$sql1 =" update $Type set Reserve=1 where APP_ID=$id ";
$sql2="INSERT INTO appointment_summary
SELECT Fname,Dep_Name,".$_SESSION['login_user'] .",D_ID,Hospital_Name,Hospital_Type,'".$_SESSION['Reason']."',APP_ID,Avalibility
FROM $Type
where APP_ID=$id";
$sql3="select * from appointment_summary where P_ID=".$_SESSION['login_user'] ." and Availability ='$Avalibility' ";
$result=$conn->query($sql3);
if ($result->num_rows != 1) {
$conn->query($sql2);
$conn->query($sql1);
echo "<script>
alert('Your appointment has been succssfuly add');
window.location.href='CheckAppoinmnets.php';
</script>";
} else {
$Invalid="<p style=\"color:red;\"> You alrredy have an appointment in this time</p>";
$_SESSION['Invalid']=$Invalid;
}
}
//enter code here
$conn->close();
This code which I used in HTML page
<?php
if(isset($_POST['id'])){
echo $_SESSION['Invalid'];
}
?>
try the following code
<?php
session_start();
if(isset($_REQUEST['id'])){
echo isset($_SESSION['Invalid'])?$_SESSION['Invalid']:'';
}
?>
it should print if you have value in ID and also in SESSION
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;
}
system/article.php
<?php
$sql = "SELECT articleTitle, articleSummary, articleContent FROM articles";
$result = $dbconnect->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo $row["articleTitle"];
echo $row["articleSummary"];
echo $row["articleContent"];
}
} else {
echo "0 results";
}
include 'template/homepage.php';
retrieves articles from the article table.
I have included the homepage.php which is supposed to act as a template.
template/homepage.php
<?php include 'template/common/header.php'; ?>
<h1>Article Title here</h1>
<p>articleSummary</p>
<?php include 'template/common/footer.php'; ?>
How do I now pass the retrieved data to the homepage.php to display it on the browser ?
Edit
smarber pointed me to
In the first file:
global $variable;
$variable = "apple";
include('second.php');
In the second file:
echo $variable;
which works. But how do I implement the same with my problem up top?
You may do that via GET, Session or Post; But why don't you simply and efficiently define a function and pass those variables to it, just for example:
function displayArticle($title, $summary, $content) {
displayHeader(); // maybe some concepts you've used in template/common/header.php
echo "<h1>$title</h1><p>$summary</p><div>$content</div>";
displayFooter(); // again, what you've provided in footer.php
}
Well then, you may do the following:
change the template/homepage.php file to:
<?php
include 'template/common/header.php';
echo "<h1>$articleName</h1>";
echo "<p>$articleSummary</p>";
include 'template/common/footer.php';
?>
and change the system/article.php to:
<?php
global $articleName;
global $articleSummary;
global $articleContents;
$sql = "SELECT articleTitle, articleSummary, articleContent FROM articles";
$result = $dbconnect->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$articleName = $row["articleTitle"];
$articleSummary = $row["articleSummary"];
$articleContents = $row["articleContent"];
include 'template/homepage.php';
}
} else {
echo "0 results";
}
However, It's so better to create a cleaner and more reusable code using some facilities you have in the programming language, like using functions and classes :)
I got two files name func.inc.php and profile.php
I'm trying to fetch the data from the mysql by creating a function get_data($id) in func.inc.php and display in the profile page(profile.php).But the data is not displaying.
<-- func.inc.php file -->
<?php session_start();
function get_data($id){
$query_in="SELECT * FROM user WHERE id ='$id'";
$query=mysql_query($query_in);
while($row=mysql_fetch_assoc($query)){
$name=$row['name'];
$book=$row['book'];
$mobile=$row['mobile'];
$computer=$row['computer'];
}
}
?>
<-- profile.php -->
<?
include'func.inc.php';
echo "Name: ".$name;
echo "Book: ".$book;?>
The function should return some value then only the values from the function will be got
function get_data($id){
$query_in= sprintf("SELECT * FROM user WHERE id ='%d'", mysql_real_escape_string($id));
$query=mysql_query($query_in);
$result = array();
while($row=mysql_fetch_assoc($query)){
$result[]['name'] = $row['name'];
}
return $result;
}
<-- profile.php -->
$values = get_data($id);
print_R($values);
You should pass the id from the function call that is: here you should call like this:
$values = get_data($id);
while($row=mysql_fetch_array($values)){
echo $name=$row['name']."<br>";
echo $book=$row['book']."<br>";
echo $mobile=$row['mobile']."<br>";
echo $computer=$row['computer']."<br>";
}
in func.inc.php use this code:
function get_data($id){
$query_in="SELECT * FROM user WHERE id ='$id'";
$query=mysql_query($query_in);
return $query;
}