Hi everyone,

I'd be grateful for some help please.

My problem is this - I am trying to find a way to pass the content of a db search from one page to another page, if that's clear.

Each file opens with

session_start();

This is the database search:

$result = mysql_query("SELECT word,sentence1 FROM pro_words WHERE word LIKE '%$search%'");

Any of the results of the db search can be accessed in more detail via a new page;

while ($r=mysql_fetch_array($result, MYSQLI_ASSOC))

echo '<tr><td align="left"><a href="word.php?w=' . $r['word'] . '">' . $r['word'] . '</a></td></tr>';

In essence, I'm looking to reproduce 'word' and 'sentence1' in the word.php file.

I have tried variations around $_SESSION, $_POST and $_GET without success - neither of my reference books provide guidance.

I'd be really grateful if someone could help me with a solution please. Something is eluding me, perhaps the problem is more complex than I think?

If you need any further info then do please say, but in meantime thanks in advance for any help.

Recommended Answers

All 6 Replies

If I understand this correctly, when you click on search, you want a new page to open with the search result?

we may need a bit more details like error messages or how you're trying to display the data and more code so we can get the clearer picture etc.

Thanks for the reply

This is the search script

<?php
  session_start();
    // check session variable

  if (isset($_SESSION['first_name']))
  {
include ('includes/header_loggedin.html');
  
  //connect to mysql
$link = mysql_connect('xxxx', 'yyyyy', 'zzzzzz');
if (!$link) {
    die('Not connected : ' . mysql_error());
}

// access the required db

$db_selected = mysql_select_db('proguide', $link);
if (!$db_selected) {
    die ('Can\'t use proguide : ' . mysql_error());
}

//specify type of search

$search=$_POST["search"];

//get the mysql and store them in $result

$result = mysql_query("SELECT word,sentence1 FROM pro_words WHERE word LIKE '%$search%'");
			        
//get the db content that is specified above

if (mysql_num_rows($result) < 1) {

echo "<br><br><h2>Oh dear. We didn't find anything. Sorry - we did look though.</h2>";

}else {
         
echo '<table align="center" cellspacing="8" cellpadding="8" width="85%"><tr><td align="left"><b>Word</b></td></tr>';

echo "<br><br><h3>This is what we found:</h3><br>";

while ($r=mysql_fetch_array($result, MYSQLI_ASSOC))

echo '<tr><td align="left"><a href="word.php?w=' . $r['word'] . '">' . $r['word'] . '</a></td></tr>';

}

// session variable for word.php
$_SESSION["word"]=$r['word'];

echo "Click on any word to use it.";

echo '</table>'; // Close the table.      

   }

	else
 
   {
    echo '<h1><p>You are not logged in.</p></h1>';
    echo '<h2><p>If you want to see this page, please register or log-in.</p></h2>';
    echo '<a href="index.php">Take Me Back</a><br>';
    echo '<a href="register.php">Register</a><br>';
   }
  
 
    echo '<br><a href="search.php">Search again?</a><br>';
    
?>

So,

- the search is made
- any results are returned
- each result is displayed as a link to a new page that displays further information on the chosen search result. The bit in bold is what I can't get working

e.g. you might search on the word 'able' and you get that word returned when you click on the link the search returns, you are passed to a new page that shows the word in its various uses.

From what I have read I ought to be able to link content between the two pages with a session variable, say;

$_SESSION[word"]=$r['word'];

The session variable is echoed in word.php

echo $_SESSION["word"];

I get an undefined variable message for $r, to wit;

Notice: Undefined variable: r in /var/www/bazzaah/searchdb.php on line 49

But $r is defined, albeit within a while loop.

But it is defined as the search works, so I suppose that either there is some basic error somewhere, whether syntax or in approach to the problem.

From what little I know I don't see why a session variable shouldn't work.

Hi, can't you just use $_GET instead of $_SESSION["word"]; in word.php?

Yep, that does work and thanks.

The only problem I have is that there are other entries from the database that I would like to include in word.php, up to 3 sentences and all of which link to audio material as well.

I was hoping there was an effective way to pull all those entries into word.php via searchdb.php. Can I put more than one array into the url constructed in searchdb.php?

Thanks in advance, I appreciate your help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.