Using PHP to run different queries within one page depending on a variable value

Hey guys I am creating a user account page where the user can access their account details from the users table, I want to have a few links on the account page that will allow them to do certain things with their information, such as view, change, edit password. So I was trying to make these options links that returned the same page but clicking a link sets a php variable to say 1, 2 or 3 and I have 3 queries written out and and if statement setting a condition if variable = 1 then run and display this query, or another etc…

I was doing this by creating a link like this:

<li><a href='myaccount.php?userinfo=1'>View account info</a></li>

At the top of the page I have the variable set to 0 $userinfo = 0;

I cannot get this to work, can anyone tell me what I am doing wrong? The idea is that I can return the information from the table depending on what link the user clicks on.

I would appreciate any assistance with this.

Many thanks.

Your idea is OK, and so is the code youve posted, so I suspect the problem will be with your filering code that delivers the required content

It should look something like


$choice = $_GET['userinfo'];

if ($choice == 1){
// your code 
}

if ($choice == 2){
// your code
}

if ($choice == 3){
// your code
}

Thanks for your advice, it makes sense, I cannot figure out how t use your advice. Here is my code, can you see where I am masking my mistake?


<?php	
$userinfo = 0;

$choice = $_GET['userinfo'];


	if (isset($_SESSION['firstname'])){
									



echo'hello ' . $user_name;}

else{

echo 'You have logged out';}


echo "<ul class='sublink'>
				<li><a href='myaccount.php?choice=1'>Shirts</a></li>
				<li><a href='myaccount.php?choice=0'>Jackets</a></li>
				<li><a href='products.php?category=men&range=shorts'>Shorts</a></li>
				<li><a href='products.php?category=men&range=hat'>Hats</a></li>
			</ul>";


			
			
if ($choice == 1){
			
	$query = "select * from cryptuser WHERE userID ='" . $userid ."'";						
										
		$result2 = sqlsrv_query( $conn, $query, array(), array( "Scrollable" => SQLSRV_CURSOR_KEYSET ));

		if( $result2 === false)
		{
			 echo "Error in query preparation/execution.\
";
			 die( print_r( sqlsrv_errors(), true));
		}

			 while( $row = sqlsrv_fetch_array ( $result2, SQLSRV_FETCH_ASSOC))
		{
			echo '<p id="productsContent">'.$row['firstname'];
			echo '<br/>'.$row['surname'];
			echo '<br/>'.$row['email'];
			echo '<br/>'.$row['address'];
			echo '<br/><br/><a href="products.php?productID='.$row['productID'].'"><img id="searchimage" src="' . $row['picture'] .'" /></a>';
			echo '<br/><br/><p/>';
		}



}
			


?>

Many thanks!

Put this into myaccount.php and run it, hopefully you’ll see how it works


<?php 
$choice = $_GET['userinfo'];
?>
<ul class='sublink'>
 <li><a href='myaccount.php?userinfo=0'>Shirts</a></li>
 <li><a href='myaccount.php?userinfo=1'>Jackets</a></li>
 <li><a href='myaccount.php?userinfo=2'>Shorts</a></li>
 <li><a href='myaccount.php?userinfo=3'>Hats</a></li>
</ul>

   
<?php   
if ($choice == 0){
 echo '<h1> You chose Shirts </h1>';
}
if ($choice == 1){
 echo '<h1> You chose Jackets</h1>';
}
if ($choice == 2){
 echo '<h1> You chose Shorts</h1>';
}
if ($choice == 3){
 echo '<h1> You chose Hats</h1>';
}
   

?>


Mandes, that worked perfectly, so I implemented it into my code and it didn’t work, I tried forever and couldn’t get additional PHP code to fit in with it. Here is my complete script, can you see how this doesn’t work at all?

<?php
$choice = $_GET[‘userinfo’];
?>

<?php
if (isset($_SESSION[‘firstname’])){

		echo'hello ' . $user_name;}

		else{

		echo 'You have logged out';}

?>

		&lt;ul class='sublink'&gt;
			&lt;li&gt;&lt;a href='myaccount.php?choice=0'&gt;one&lt;/a&gt;&lt;/li&gt;
			&lt;li&gt;&lt;a href='myaccount.php?choice=1'&gt;two&lt;/a&gt;&lt;/li&gt;
			&lt;li&gt;&lt;a href='products.php?choice=2'&gt;three&lt;/a&gt;&lt;/li&gt;
			&lt;li&gt;&lt;a href='products.php?choice=3'&gt;four&lt;/a&gt;&lt;/li&gt;
		&lt;/ul&gt;"

<?php
if ($choice == 0){
echo ‘<h1> You chose one </h1>’;

$query = "select * from cryptuser WHERE userID ='" . $userid ."'";						
									
	$result2 = sqlsrv_query( $conn, $query, array(), array( "Scrollable" =&gt; SQLSRV_CURSOR_KEYSET ));

	if( $result2 === false)
	{
		 echo "Error in query preparation/execution.\

";
die( print_r( sqlsrv_errors(), true));
}

		 while( $row = sqlsrv_fetch_array ( $result2, SQLSRV_FETCH_ASSOC))
	{
		echo '&lt;p id="productsContent"&gt;'.$row['firstname'];
		echo '&lt;br/&gt;'.$row['surname'];
		echo '&lt;br/&gt;'.$row['email'];
		echo '&lt;br/&gt;'.$row['address'];
		echo '&lt;br/&gt;&lt;br/&gt;&lt;a href="products.php?productID='.$row['productID'].'"&gt;&lt;img id="searchimage" src="' . $row['picture'] .'" /&gt;&lt;/a&gt;';
		echo '&lt;br/&gt;&lt;br/&gt;&lt;p/&gt;';
	}

}

if ($choice == 1){

echo ‘<h1> You chose two </h1>’;
}

?>

Are you sure that youre using MS SQL Server ? or are you using mySQL ?

I am using SQL Server 2008 R2

Locally using ISS and windows 7.

Sorry, I was doing too many things first time round

Youve changed my script from

<ul class='sublink'>
 <li><a href='myaccount.php?userinfo=0'>Shirts</a></li>
 <li><a href='myaccount.php?userinfo=1'>Jackets</a></li>
 <li><a href='myaccount.php?userinfo=2'>Shorts</a></li>
 <li><a href='myaccount.php?userinfo=3'>Hats</a></li>
</ul>


to

<ul class='sublink'>
<li><a href='myaccount.php?choice=0'>one</a></li>
<li><a href='myaccount.php?choice=1'>two</a></li>
<li><a href='products.php?choice=2'>three</a></li>
<li><a href='products.php?choice=3'>four</a></li>
</ul>"


I was seting a URL variable called ‘userinfo’, you are calling it ‘choice’

Mandes, I am sorry, I have to take a break, I made a mistake. Your code works fine, thank you for all your help I appreciate it. I am making silly mistakes now :frowning:

You solved my problem!

:slight_smile:

Cheers!

no problem