I really need someone to help me on the scenario below.

I fetch all the rows in a database table with the mysql_fetch_array() and it was successful. I placed a button on each row in order to click and take the user to another page. On click of the button, i want session to take the values of all columns on that particular row to the other page.

But i discovered that on click on any of the button, session only registered the last row of the table not minding maybe i click on the button of the first row or any other row. I need someone to put me through on this and i will really appreciate all suggestions.

Thanks in advance.

Recommended Answers

All 4 Replies

Let us peep on your code just a little bit. I am sure it is a secret but promise we won't tell anyone.

Below is a copy of my code.

$select = "select * from platform";
		  $result = mysql_query($select) or die(mysql_error());
		  
		  echo "<table border = '1' bordercolor = '#CC6600' cellspacing = '0'>";
echo "<tr><th>Category</th><th>Code</th><th>Game</th><th>Valid Time</th></tr>";

			while($row = mysql_fetch_array($result)){
			
			echo '<form name = "form2" method = "post" action = "predictproc.php">';
			
			echo "<tr><td width = '100'>";
	echo $row['category'];

	echo "</td><td width = '100'>";

	echo $row['competitioncode'];

	echo "</td><td width = '250'>";

	echo $row['teama']; echo '&nbsp;&nbsp;'; echo "vs"; echo '&nbsp;&nbsp;'; echo $row['teamb'];

	echo "</td><td width = '100'>";

	echo $row['kickoff'];

	echo "</td><td>";

	echo '<input type = "Submit" name = "Submit" value =  "Play This" class = "button">';

	echo "</td></tr>";

		$gamecode = $row['competitioncode'];

	
			echo '</form>';
			
		//my problem starts here, I want session variables to be created for each row if I click on the button of that particular row. but no matter what button i click, it retrieves record of only the last row on the other page.  
			
				$predict = "select * from platform where competitioncode = '$gamecode'";

				$predict_result = mysql_query($predict) or die("can't select for predict");

				if($predict_result)
				{
					if(mysql_num_rows($predict_result) == 1)
					{
						$predictrow = mysql_fetch_array($predict_result);

					$_SESSION['category'] = $predictrow['category'];

				$_SESSION['competitioncode'] = $predictrow['competitioncode'];

				$_SESSION['teama'] = $predictrow['teama'];

				$_SESSION['teamb'] = $predictrow['teamb'];

				$_SESSION['kickoff'] = $predictrow['kickoff'];	
					}

				}
				
						
			}		
				echo "</table>";
				
	
				
					
	?>

I still look for solution about this post... please help me out. Or send me your email so that i can screen shot the interface to you in order to have full understanding of my problem.

Thanks.

I am sorry to not get back with help earlier but I haven't noticed that you posted your code sometime ago. Really sory for that.

The problem is that in your while loop you assign a value of a every row from the second query to the same set session variables. In first iteration you create theese session variables but in subsequent sessions you overwrite them with new values. At the end of the loop only the last row is assigned to this set of session variables.

I can see two possible solutions here:

1. Make session variables an array where each element is an array - the setof variables for one row. But I am not sure if this is not good since you will use a lot of session resources.

2. Assign each row a hidden field with the value of $gamecode and execute the second query on another page (predictproc.php) with the value of gamecode in POST. Then assign values from the query to session variables (if needed).

If I find some more time today I can post also some code. Please let me know which of above proposal suits your needs beter (or if you have other requirements).

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.