Victor Ngure Victor Ngure - 27 days ago 9
HTML Question

Passing values from page1.php to page2.php using sessions

I would like to pass values from page1.php to page2.php using session variable. However, i am yet to be successfull. Instead of displaying only one name (the clicked one) on page2.php, all the names from page1.php are displayed. Here is my code,

page1.php

<?php
session_start();
$name_data = array();
?>
<?php
include('dbase.php');

// get the records from the database
if ($result = $mysqli->query("SELECT * FROM notes WHERE categoryname='Humanities' ORDER BY noteid"))
{
// display records if there are records to display
if ($result->num_rows > 0)
{
while ($row = $result->fetch_object())
{
?>
<div class="vdo-list">

<div class="vdo-title"><a href="preview.php">
<?php
$name_data[] = $row->notename;
echo "<h3>" .$row->notename. "</h3>";
?>
</div></a>
<?
$_SESSION['uname'] = $name_data;
?>


page2.php

<?php
session_start();
?>
<div class="heading6">
<h1>
<?php
if(count($_SESSION['uname'])>0)
{
foreach ($_SESSION['uname'] as $uname)
{
echo $uname;
}
}
?>
</h1></div>


Could anyone please point out what i am doing wrong? Any efforts will be highly appreciated.

Answer

Finally, here is an answer that works perfectly,

page1.php

 <?php
$_SESSION['uname'] = $row->notename;
echo '<h3><a href="preview.php?name='.$row->notename.'">'.$row->notename.'<h3></a>';
?>

page2.php

 <?php
include ('dbase.php');
       if(count($_SESSION['uname'])>0)
    {
        if($uname = $mysqli->query("SELECT * FROM `notes` WHERE `notename`='".$_GET['name']."'"))
        {
        $row=mysqli_fetch_array($uname,MYSQLI_ASSOC);
   // $result=$uname->fetchAll(PDO::FETCH_ASSOC);
    //foreach ($result as $row)
    //{
    echo $row['notename'];
    //}
    }    
    } 
    ?>
Comments