Page 1 of 1

[SOLVED] Passing variables to other pages in a link

Posted: Mon Apr 04, 2005 12:17 pm
by PHP_Ste
I want to put band names out of my database inside links and loop it through to display a set of links of all the bands in my database, this bit works no problem.

I then want to be able to click those links, go to a seperate page and on that page get up the band details, to do this I need to pass the band name to the page the link is going to. I thought I could do this using $_GET but it hasn't worked, the code is below.

Here is the code ont he first page.

Code: Select all

$query="SELECT * FROM band";
		$result=mysql_query($query);
		$num=mysql_numrows($result);
		$i=0;
		while ($i < $num) 
		{
			
			$band=mysql_result($result,$i,"band_name");
			
//Link here, shows up no problem

echo'<a href="bandview.php?band=$band">'.$band; echo'</a>';
		
			i++;
		
		}
Here is the script for the second page, I'm just echoing out the variable to test see if it works, once I've done this I can put it into a select query and get all the information I need:

Code: Select all

include('connect.inc');
		$band = $_GET['bandname'];
		echo .$band;

Re: Passing variables to other pages in a link

Posted: Mon Apr 04, 2005 12:25 pm
by John Cartwright

Code: Select all

$query=&quote;SELECT * FROM band&quote;;

//added error reporting - always recommended
$result=mysql_query($query) or die(mysql_error());
//no need for mysql_num_rows

     //you were never actualy acquiring your query 
     //should use mysql_fetch_assoc() or mysql_fetch_array()
     while ($row = mysql_fetch_assoc($result)) 
     {
          //assuming the column holding the bands name is 'bandname'
          //note you must escape variables in single quotes for the to be parsed
          echo '&lt;a href=&quote;bandview.php?bandname='.$row&#1111;'bandname'].'&quote;&gt;'.$row&#1111;'bandname'].'&lt;/a&gt;';
		
     }
Note my comments.

Code: Select all

include('connect.inc');

//previous link was bandview.php?band=
//note its changed to bandview.php?bandname= singe you were searching for $_GET&#1111;'bandname']
$band = $_GET&#1111;'bandname'];

Posted: Mon Apr 04, 2005 12:39 pm
by PHP_Ste
Thanks for the help