Jump to content

variable in url


Chris.P

Recommended Posts

I'm trying to get a variable from one page to another using variables in the URL. I have this working fine from some pages but not others.

 

So starting on one page I have this section of code.

 

else {
  header ("Location: user.php?band=$bandid");
       }

 

The variables work fine and it takes me to the user.php page.

 

On user.php I am using the following to get the variable in the URL to a variable in the page which also works fine because I can echo the values out just fine.

 

$bandid = $_GET['band'];
echo $bandid;

 

My problem comes when I try use the variable on user.php in a link on that page like this.

 

<a href="image_upload.php?band=$bandid">Upload Image</a><br />

 

Instead of the link showing the contents of the variable it shows me the variable name. Any help I'm baffled.  ???

 

Link to comment
https://forums.phpfreaks.com/topic/51112-variable-in-url/
Share on other sites

Post the PHP block of code here.

 

I have a feeling you have it like this...

 

<?php

 

code

code

code

echo ' some code some code

      <a href="image_upload.php?band=$bandid">Upload Image</a><br />

    some code';

 

?>

 

If you have it in a single quote string, it is a literal string and it will print out as $bandid not the value you have set for bandid.

 

Link to comment
https://forums.phpfreaks.com/topic/51112-variable-in-url/#findComment-251691
Share on other sites

Post the PHP block of code here.

 

Ok in my user.php page I have the following section of PHP.

 

<?php
include_once  'functions.php'; //This includes some common functions
$bandid = $_GET['band'];
echo $bandid;
showLogin(); //This is a function in 'functions.php'
?>

Heres the functions.php file that conatins the showlogin() function that called form the user.php file above and contains the link concerned.

 

<?php
//login details
function loginDetails(){
    //db access details 
    $hostname = "xxxxx";
    $username = "xxxxx";
    $password = "xxxxx";
    $dbname = "xxxxx";

   // (2) Open the database connection 
   $connection = mysql_connect($hostname, $username, $password) or die ("Unable to connect to database!");; 

   // (3) Select your database 
   mysql_select_db($dbname) or die ("Unable to select database!");
   }

   //login script
   function showLogin()
   { 
  // Check if we have established an authenticated session
     if (isset($_SESSION["authenticatedUser"]))
     {
       $_SESSION["message"] = "You are logged in as ". $_SESSION['authenticatedUser'];
       echo '<h2><a href="user.php">Control Panel</a><br />
   			<a href="update_details.php">Update details</a><br />
			<a href="image_upload.php?bandid=".$bandid."">Upload Image</a><br />
                 <a href="logout.php">Logout</a></h2>';
     }
    else
     {
  	echo '<h1>Login</h1>
      	<form name="form1" id="form1" method="post" action="loginaction.php">
      	<h2>Username:
     	<input name="loginUser" type="text" id="loginUser" size="8" />
       	<br />
       	Password:
       	<input name="loginPass" type="password" id="loginPass" size="8"/>
      	<br />
      	<input type="submit" name="Submit" value="Go!" class="button"/>
        </h2>
      	</form>
      	<h2><a href="register_details.php">Register</a><br />
	<a href="recoverpass.php">Forgotten password?</a></h2>';
     }
 echo "<h2>". $_SESSION["message"]. "</h2>";
} //End showLogin
?>

Link to comment
https://forums.phpfreaks.com/topic/51112-variable-in-url/#findComment-251920
Share on other sites

<?php
//login details
function loginDetails(){
    //db access details 
    $hostname = "xxxxx";
    $username = "xxxxx";
    $password = "xxxxx";
    $dbname = "xxxxx";

   // (2) Open the database connection 
   $connection = mysql_connect($hostname, $username, $password) or die ("Unable to connect to database!");; 

   // (3) Select your database 
   mysql_select_db($dbname) or die ("Unable to select database!");
   }

   //login script
   function showLogin()
   { 
  // Check if we have established an authenticated session
     if (isset($_SESSION["authenticatedUser"]))
     {
       $_SESSION["message"] = "You are logged in as ". $_SESSION['authenticatedUser'];
       echo "<h2><a href=\"user.php\">Control Panel</a><br />
   			<a href=\"update_details.php\">Update details</a><br />
			<a href=\"image_upload.php?bandid=$bandid\">Upload Image</a><br />
                 <a href=\"logout.php\">Logout</a></h2>";
     }
    else
     {
  	echo '<h1>Login</h1>
      	<form name="form1" id="form1" method="post" action="loginaction.php">
      	<h2>Username:
     	<input name="loginUser" type="text" id="loginUser" size="8" />
       	<br />
       	Password:
       	<input name="loginPass" type="password" id="loginPass" size="8"/>
      	<br />
      	<input type="submit" name="Submit" value="Go!" class="button"/>
        </h2>
      	</form>
      	<h2><a href="register_details.php">Register</a><br />
	<a href="recoverpass.php">Forgotten password?</a></h2>';
     }
 echo "<h2>". $_SESSION["message"]. "</h2>";
} //End showLogin
?>

 

Try that

Link to comment
https://forums.phpfreaks.com/topic/51112-variable-in-url/#findComment-251923
Share on other sites

<?php
//login details
function loginDetails(){
    //db access details 
    $hostname = "xxxxx";
    $username = "xxxxx";
    $password = "xxxxx";
    $dbname = "xxxxx";

   // (2) Open the database connection 
   $connection = mysql_connect($hostname, $username, $password) or die ("Unable to connect to database!");; 

   // (3) Select your database 
   mysql_select_db($dbname) or die ("Unable to select database!");
   }

   //login script
   function showLogin()
   { 
  // Check if we have established an authenticated session
     if (isset($_SESSION["authenticatedUser"]))
     {
       $_SESSION["message"] = "You are logged in as ". $_SESSION['authenticatedUser'];
       ?>
   <h2><a href="user.php">Control Panel</a><br />
   			<a href="update_details.php">Update details</a><br />
			<a href="image_upload.php?bandid=<?=$bandid ?>">Upload Image</a><br />
                 <a href="logout.php">Logout</a></h2>
<?php
     }
    else
     {
?>       <h1>Login</h1>
      	<form name="form1" id="form1" method="post" action="loginaction.php">
      	<h2>Username:
     	<input name="loginUser" type="text" id="loginUser" size="8" />
       	<br />
       	Password:
       	<input name="loginPass" type="password" id="loginPass" size="8"/>
      	<br />
      	<input type="submit" name="Submit" value="Go!" class="button"/>
        </h2>
      	</form>
      	<h2><a href="register_details.php">Register</a><br />
	<a href="recoverpass.php">Forgotten password?</a></h2>
<?php
     }
 echo "<h2>". $_SESSION["message"]. "</h2>";
} //End showLogin
?>


 

Try that. I am very much against echo'ing large chunks of HTML. It makes the code more difficult to find errors and such.

 

If you can echo the $bandid variable then that should work. If it shows up blank, then make sure that variable is set with data

Link to comment
https://forums.phpfreaks.com/topic/51112-variable-in-url/#findComment-252149
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.