Passing variables from 1 php to another php page

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
Rmias
Forum Newbie
Posts: 24
Joined: Wed Nov 26, 2003 11:02 am

Passing variables from 1 php to another php page

Post by Rmias »

Hi,

Can someone help me on this problem please, I have the following php page which displays users from the database. Users are displayed in a table that containes radio buttons for each displayed users. if i select one user using the radio button and click on view or edit or delete button it should open e.g view.php with the selected user. In order to do this I tried to pass the variables using sessions but still have problems,

Can you help me please, here are displayuser.php and viewuser,php
displayuser.php

Code: Select all

<?php
<?php
print "<form action='viewuser.php' method='POST'>";

	mysql_connect("localhost") or die ("Unable to connect to databse.");

	mysql_select_db("users") or die ("Unable to select database.");

	$resultID = mysql_query("SELECT user_id, full_name, login_name, email, disabled FROM users");

	          $_SESSION['user_id'];
	
print "<table border=1><tr><td>edit/delete</th><th>User ID</th>";
print "<th>Full Name</th><th>Login Name</th><th>Email Address</th><th>Is user disabled ?</th>";

while ($row = mysql_fetch_row($resultID))
{
   print "<tr>";
   // the user-id is the first field?
   echo  '<td><input type="radio" name="user_id" value="', $row[0], '"/></td>';
   foreach ($row as $field)
   {
      print "<td>$field</td>";
   }
   print "</tr>";
}
print "</table>";

  ?>
<?
print "<table>";
print "<tr>";
print "<td>";
print "<INPUT type='submit' name='View User' value='view_user'> </td>
";
print "</form>";
 ?>
</body>
</html>

//viewuser.php
<?php
session_start();
header("Cache-control: private"); //IE 6 Fix
?>
<?php

   
    // Open the database connection
	$dbc = mysql_connect("localhost") or die ("Unable to connect to databse.");
    //select database
	$dbselect = mysql_select_db("Users",$dbc) or die ("Unable to select database.");
      
     if (!$_SESSION['user_id']) {
     	echo "No user selected please make a selection";
     }
     else {
     	$query = mysql_query("SELECT * FROM users WHERE user_id = $user_id");

        print "<h1>'$fullname' &nbsp; Details" ;
        }
        ?>
        </body>
        </html>
?>
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

You're already passing the user_id in a POST var via the form, so no need to use sessions here.

Just do
if (empty($_POST['user_id'])){
echo "No user selected please make a selection";
} else {
$query = mysql_query("SELECT * FROM users WHERE user_id = '{$_POST['user_id']}'");
....

and remove the $_SESSION['user_id']; line from displayuser.php as it's not needed (and badly formed anyway ;))
Rmias
Forum Newbie
Posts: 24
Joined: Wed Nov 26, 2003 11:02 am

Post by Rmias »

Thank you markI999 for the quick reply and I will try it
Post Reply