[SOLVED] Passing variables to other pages in a link

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
PHP_Ste
Forum Newbie
Posts: 19
Joined: Thu Mar 10, 2005 8:42 am

[SOLVED] Passing variables to other pages in a link

Post 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;
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Passing variables to other pages in a link

Post 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'];
PHP_Ste
Forum Newbie
Posts: 19
Joined: Thu Mar 10, 2005 8:42 am

Post by PHP_Ste »

Thanks for the help
Post Reply