PHP Selectbox and Session Variables

Hey guys,

[php]<?php
session_start();
include(“db_connect.php”);
if(($_SESSION[‘user_id’]) && ($_SESSION[‘access_level’] == 1 ))
{
?>

Teaching Int Limited - Edit Database <?php include("header.php"); ?>
  • View and/or Edit Person <?php $ops = ''; $sql_select = "select person_id, first_name, middle_name, last_name from `person` order by `last_name` asc "; $retval_selectperson = mysql_query( $sql_select, $conn ); if(! $retval_selectperson ) { die('Could not select data: ' . mysql_error()); } while($row = mysql_fetch_assoc($retval_selectperson)) { $ops .= "{$row['first_name']} {$row['middle_name']} {$row['last_name']}"; } ?>
    Select a Person --- Select a Person --- <?php echo $ops;?>
  • View and/or Edit Company <?php $ops = ''; $sql_select = "select * from `company` order by `company_name` asc "; $retval_selectcompany = mysql_query( $sql_select, $conn ); if(! $retval_selectcompany ) { die('Could not select data: ' . mysql_error()); } while($row = mysql_fetch_assoc($retval_selectcompany)) { $ops .= "{$row['company_name']}"; } ?>
    Select a Company --- Select a Company --- <?php echo $ops;?>
  • View and/or Edit Category <?php $ops = ''; $sql_select = "select * from `category` order by `category` asc "; $retval_selectcategory = mysql_query( $sql_select, $conn ); if(! $retval_selectcategory ) { die('Could not select data: ' . mysql_error()); } while($row = mysql_fetch_assoc($retval_selectcategory)) { $ops .= "{$row['category']}"; } ?>
    Select a Category --- Select a Category --- <?php echo $ops;?>
    <?php $_SESSION['edit_category'] = $_POST['category_id'] ?>
  • View and/or Edit Campus <?php $ops = ''; $sql_select = "select * from `campus` order by `campus_name` asc "; $retval_selectcampus = mysql_query( $sql_select, $conn ); if(! $retval_selectcampus ) { die('Could not select data: ' . mysql_error()); } while($row = mysql_fetch_assoc($retval_selectcampus)) { $ops .= "{$row['campus_name']}"; } ?>
    Select a Campus --- Select a Campus --- <?php echo $ops;?>
    <?php $_SESSION['edit_campus'] = $_POST['campus_id'] ?>
  • View and/or Edit Documents for a Person <?php $ops = ''; $sql_select = "select person_id, first_name, middle_name, last_name from `person` order by `last_name` asc "; $retval_selectperson = mysql_query( $sql_select, $conn ); if(! $retval_selectperson ) { die('Could not select data: ' . mysql_error()); } while($row = mysql_fetch_assoc($retval_selectperson)) { $ops .= "{$row['first_name']} {$row['middle_name']} {$row['last_name']}"; } ?>
    Select a Person --- Select a Person --- <?php echo $ops;?>
    <?php $_SESSION['edit_documents'] = $_POST['person_id'] ?>
  • View and/or Edit Education for a Person <?php $ops = ''; $sql_select = "select person_id, first_name, middle_name, last_name from `person` order by `last_name` asc "; $retval_selectperson = mysql_query( $sql_select, $conn ); if(! $retval_selectperson ) { die('Could not select data: ' . mysql_error()); } while($row = mysql_fetch_assoc($retval_selectperson)) { $ops .= "{$row['first_name']} {$row['middle_name']} {$row['last_name']}"; } ?>
    Select a Person --- Select a Person --- <?php echo $ops;?>
    <?php $_SESSION['edit_education'] = $_POST['person_id'] ?>
  • View and/or Edit Employment for a Person <?php $ops = ''; $sql_select = "select person_id, first_name, middle_name, last_name from `person` order by `last_name` asc "; $retval_selectperson = mysql_query( $sql_select, $conn ); if(! $retval_selectperson ) { die('Could not select data: ' . mysql_error()); } while($row = mysql_fetch_assoc($retval_selectperson)) { $ops .= "{$row['first_name']} {$row['middle_name']} {$row['last_name']}"; } ?>
    Select a Person --- Select a Person --- <?php echo $ops;?>
    <?php $_SESSION['edit_employment'] = $_POST['person_id'] ?>
  • View and/or Edit Intake for a Person <?php $ops = ''; $sql_select = "select person_id, first_name, middle_name, last_name from `person` order by `last_name` asc "; $retval_selectperson = mysql_query( $sql_select, $conn ); if(! $retval_selectperson ) { die('Could not select data: ' . mysql_error()); } while($row = mysql_fetch_assoc($retval_selectperson)) { $ops .= "{$row['first_name']} {$row['middle_name']} {$row['last_name']}"; } ?>
    Select a Person --- Select a Person --- <?php echo $ops;?>
    <?php $_SESSION['edit_intake'] = $_POST['person_id'] ?>
<?php } else { echo ""; }[/php]

That is the code for the form page. Now, when I select an option and click the button, I need the relevant page to open. I would need to code a select query using the session variable in the sql statement to get the relevant record. Any ideas on how this is done?

Listen,

I am not where I can check out your code on a server right now; but, does your form even work? (at first glance I wouldn’t think so)

If I understand what you are trying to do you want to select some records from the database, display the relevant records and then have the user select one of them for further manipulation. The easiets way to do this is comething like this:

First, have a unique ID in the database for each record, retrieve the database quiery results (as $records in my case) then create a form like this one I am using in an app, stepping through your records with a foreach loop:
[php]

    <thead>
         <tr>
            <th>Select</th>
            <th>First name</th>
            <th>Last Name</th>
            <th>Phone</th>
            <th>Email</th>
         </tr>                   
    </thead>
    <tbody>
                  <?php
                  
           foreach($records as $r){  
          ?>
           
           
        <tr>
    
            <td><input type='radio' name='RKEY' <?php echo $checked ?>" align="center" value=<?php echo $r->idCustomer ?> /></td>
            <?php
                $checked='';
            ?>
            <td><?php echo $r->FirstName ?></td>
            <td><?php echo $r->LastName ?></td>
            <td><?php echo $r->Phone ?></td>
            <td><?php echo $r->EmailAddress ?></td>
            
        </tr>
        
       
          <?php
           }
           
          ?>
  </tbody>       
 
    </table>
    </div>
    
    <input type="hidden" name="token" value ="<?php echo token::generate() ;?>" id="hidden">
    <input type="submit" name="select" value ="Select">
    <input type="submit" name="cancel" value ="<?php echo $target ;?>">
    </form>

    [/php]

The token class is a security addition I have implemented to makesure the post is coming from my PHP page.

Hope this helps.

And as a warning, I’m a novice PHP coder. I make no warranties :wink:

Sponsor our Newsletter | Privacy Policy | Terms of Service