Re: [PHP-DB] global not solving my problem

[email protected] (Micah Gersten)
Newsgroups php.db
Message-ID <[email protected]>
Fred Silsbee wrote:
> problem:
>
> I have resolved all problems in the PHP program below except one.
>
> Yes I know the program is in a mess but I will clean it up.
>
> This is a learning exercise!
>
> Basically there is one function per page.
>
> When I make a selection on a page, a function is executed.
>
> I need to pass the value of $rowInsert from one page to another.
>
> $rowInsert is altered when I enter the program (at the top).
>
> To verify the program works (outside this problem), I set the value = 525.
>
> Global does not persist the value between pages(functions).
>
> I can't use _POST because the value of $rowInsert is not associated with an entry slot(edit control).
>
> I could make an entry slot(edit control) just for this purpose but that is not good!
>
>
>
>
>
>
>
> <?php                                          
> /*                                             
>  drop sequence log_id;                         
>  create sequence log_id                        
>  increment by 1                                
>  start with 1;                                 
>
> drop table log_book_id;
>
> create table log_book_id ( log_id number primary key, fdate date, actype varchar2(16), acid varchar(16), nlandings number, nhoursnumber);
>
> insert into log_book_id values (logid.nextval, TO_DATE('08/12/1973','MM/dd/YYYY'),'C150','N5787G',1,1.8);
>
> insert into log_book_id values (logid.nextval, TO_DATE('08/17/1973','MM/dd/YYYY'),'C150','N5787G',3,1.5);
>
> insert into log_book_id values (logid.nextval, TO_DATE('08/26/1973','MM/dd/YYYY'),'C150','N5787G',10,1.8);
>
> insert into log_book_id values (logid.nextval, TO_DATE('09/01/1973','MM/dd/YYYY'),'C150','N5293S',9,1.7);
> */                                                                                                       
> print("........................................ENTER................");                                  
> $rowInsert = 525;          // temporary                                                                              
> $db = "(DESCRIPTION =                                                                                    
>    (ADDRESS = (PROTOCOL = TCP)(HOST = 127.0.0.1)(PORT = 1521))                                           
>    (CONNECT_DATA =                                                                                       
>      (SERVER = DEDICATED)                                                                                
>      (SID = LMKIIIGDNSID)                                                                                
>    )                                                                                                     
>  )";                                                                                                     
> if ($conn=oci_connect('landon', 'rumprocella',$db))                                                      
> {                                                                                                        
>         echo "Successfully connected to Oracle.\n";                                                      
> }                                                                                                        
> else                                                                                                     
> {                                                                                                        
>  $err = OCIError();                                                                                      
>  echo "Oracle Connect Error " . $err['message'];                                                         
> }                                                                                                        
>
> //construct a global variable for the form profile
> $fields = array("fdate", "actype", "acid", "nlandings", "nhours");
> ///                                                               
>
> // If the submit button has been pressed
> if (isset($_POST['select'])){           
>     displayDeleteForm();                
> }elseif(isset($_POST['delete'])){       
>     deleteRecords();                    
> } elseif(isset($_POST['insert'])){      
>     insertRecord();                     
> } elseif (isset($_POST['new'])){        
>     displayEntryForm();                 
> } elseif (isset($_POST['update'])){     
>     showRecord();                       
> } else {                                
>     //default action                    
>     displayEntryForm();                 
> }                                       
>
> //...........................................................................function displayDeleteForm()
>
> function displayDeleteForm(){
>     //get $fields into the function namespace
>     global $fields;                          
>         global $conn;                        
> /* Create and execute query. */              
> // Loop through each row, outputting the actype and name
>      echo <<<HTML                                       
>     Pilots Logbook entries stored in Oracle Relational Database
> under Redhat Fedora 8 Linux                                    
>     <form action="{$_SERVER['PHP_SELF']}" method="post">       
>     <table width="50%" align="center" border="1">              
>     <tr>                                                       
>         <th>checked</th>                                       
>         <th>rowID</th>                                         
>         <th>fdate</th>                                         
>         <th>actype</th>                                        
>         <th>acid</th>                                          
>         <th>nlandings</th>                                     
>         <th>nhours</th>                                        
>     </tr>                                                      
> HTML;                                                          
>
>     //get log_book_id table information
>     $user = "select * from log_book_id";
>     $result = oci_parse($conn, $user);  
>         $r = oci_execute($result);      
>
>     //display log_book_id information
>
>     $count = 0;
>     while ($newArray = oci_fetch_assoc($result)) {
>         if ($count == 0)        print_r($newArray);
>     $count = 1;                                    
>         foreach ($fields as $field){               
> $fieldx = strtoupper($field);                      
>             ${$field} = $newArray[$fieldx];        
>                 $count = $count +1;                
> //              print($field);                     
> //              print_r($newArray[$field]);        
>         }                                          
>         $rowID = $newArray['LOG_ID'];              
>
>         //note that we do not rely on the checkbox value as not all browsers submit it
>         //instead we rely on the name of the checkbox.                                
>         echo <<<HTML                                                                  
>     <TR>                                                                              
>         <td>                                                                          
>             <input type="checkbox" name="checkbox[$rowID]" value="$rowID">Check to delete record
>         </td>                                                                                   
>         <TD>$rowID</TD>                                                                         
>         <TD>$fdate</TD>                                                                         
>         <TD>$actype</TD>                                                                        
>         <TD>$acid</TD>                                                                          
>         <TD>$nlandings</TD>                                                                     
>         <TD>$nhours</TD>                                                                        
>     </TR>                                                                                       
>
> HTML;
>
>     }    // while
>     echo <<<HTML 
>     <tr>         
>         <td colspan="7">
>             <input type="submit" name="delete" value="delete checked items"/>&nbsp;
>             <input type="submit" name="update" value="update checked items"/>&nbsp;
>             <input type="submit" name="new" value="New Log Entry" />&nbsp;         
>             <input type="reset" name="reset" value="Reset Form" />                 
>         </td>                                                                      
>     </tr>                                                                          
>     </table>                                                                       
>     </form>                                                                        
>                                                                                    
> HTML;                                                                              
> }    //close function                                                              
>
>
> // ................................................................. function deleteRecords()
> //                                                                                           
>
> function deleteRecords()
> {                       
>
> $db = "(DESCRIPTION =
>    (ADDRESS = (PROTOCOL = TCP)(HOST = 127.0.0.1)(PORT = 1521))
>    (CONNECT_DATA =                                            
>      (SERVER = DEDICATED)                                     
>      (SID = LMKIIIGDNSID)                                     
>    )                                                          
>  )";                                                          
>
> // Loop through each log_book_id with an enabled checkbox
>     if (!isset($_POST['checkbox'])){                     
>         return true;                                     
>     }                                                    
>     //else                                               
>                                                          
>     foreach($_POST['checkbox'] as $key=>$val){           
>         $toDelete[] = $key;                              
>     }                                                    
>     //expand the array for an IN query                   
>     $where = implode(',', $toDelete);                    
>                                                          
> if ($conn=oci_connect('landon', 'rumprocella',$db))      
> {                                                        
>  echo "Successfully connected to Oracle D.\n";           
> }                                                        
> else                                                     
> {                                                        
>  $err = OCIError();                                      
>  echo "Oracle Connect Error " . $err['message'];         
> }                                                        
>
>     $query = "DELETE FROM log_book_id WHERE log_id IN ($where)";
> echo($query);                                                   
>     $result = oci_parse($conn, $query);                         
>         $r = oci_execute($result);                              
> // Commit transaction                                           
>         $committed = oci_commit($conn);                         
> // Test whether commit was successful. If error occurred, return error message
> if (!$committed) {                                                            
>     $error = oci_error($conn);                                                
>     echo 'Commit failed. Oracle reports: ' . $error['message'];               
> }                                                                             
>     // Should have one affected row                                           
>     if ((oci_num_rows($result)== 0) ) {                                       
>         echo "<p>There was a problem deleting some of the selected items.</p><p>".oci_error().'</p>';
> //        exit();                                                                                    
>     } else {                                                                                         
>         echo "<p>The selected items were successfully deleted.</p>";                                 
>     }                                                                                                
>     //direct the user back to the delete form                                                        
>     displayDeleteForm();                                                                             
> } //end function                                                                                     
>
> //...........................................................................function insertRecord()
>
> function insertRecord()
> {                      
> global $rowInsert;     
> $db = "(DESCRIPTION =  
>    (ADDRESS = (PROTOCOL = TCP)(HOST = 127.0.0.1)(PORT = 1521))
>    (CONNECT_DATA =                                            
>      (SERVER = DEDICATED)                                     
>      (SID = LMKIIIGDNSID)                                     
>    )                                                          
>  )";                                                          
> if ($conn=oci_connect('landon', 'rumprocella',$db))           
> {                                                             
>  echo "Successfully connected to Oracle.\n";                  
> }                                                             
> else                                                          
> {                                                             
>  $err = OCIError();                                           
>  echo "Oracle Connect Error " . $err['message'];              
> }                                                             
>     // Retrieve the posted log book information.              
>     global $fields;                                           
>     //add some very crude validation                          
>     foreach ($fields as $field){                              
>         if (empty($_POST[$field])){                           
>             $messages[] = "You must complete the field $field \r\n";
>         } else {                                                    
>             $dFields[$field] = mysql_real_escape_string(trim($_POST[$field]));
>         }                                                                     
>     }                                                                         
>     if (count($messages)>0) {                                                 
>         displayEntryForm($messages, $dFields);                                
>         exit();                                                               
>     }                                                                         
>     //end validation                                                          
>                                                                               
>     //get variables into the namespace                                        
>     extract($dFields);                                                        
>     // Insert the log book information into the log book table                
> print("insertRecord".$rowInsert);                                             
>         if($rowInsert)                                                        
>         {                                                                     
>                 $query = "UPDATE log_book_id set fdate = '$fdate', actype='$actype', acid='$acid', nlandings='$nlandings', nhours='$nhours'  where log_id='$rowInsert'";
>         }                                                                                                                                                               
>         else                                                                                                                                                            
>         {                                                                                                                                                               
>                 $query = "INSERT INTO log_book_id (log_id , fdate, actype, acid, nlandings, nhours) values (logid.nextval,'$fdate','$actype','$acid','$nlandings','$nhours')";
>         }                                                                                                                                                                     
>
>         $rowInsert = 0;
> // echo ($query);      
>     $result = oci_parse($conn, $query);
>         $r = oci_execute($result);     
> // Display an appropriate message      
>     if ($r) {                          
>         echo "<p>Product successfully inserted!</p>";
>     }else {                                          
>         echo "<p>There was a problem inserting the log book!</p>";
>     $error = oci_error($conn);                                    
>     echo 'Insert failed. Oracle reports: ' . $error['message'];   
>
>     }
>     //direct the user back to the entry form
>                                             
> // Commit transaction                       
>         $committed = oci_commit($conn);     
> // Test whether commit was successful. If error occurred, return error message
> if (!$committed) {                                                            
>     $error = oci_error($conn);                                                
>     echo 'Commit failed. Oracle reports: ' . $error['message'];               
> }                                                                             
>     // Should have one affected row                                           
>     if ((oci_num_rows($result)== 0) ) {                                       
>         echo "<p>There was a problem inserting some of the selected items.</p><p>".oci_error().'</p>';
> //        exit();                                                                                     
>     } else {                                                                                          
>         echo "<p>The selected items were successfully inserted.</p>";                                 
>     }                                                                                                 
>         displayDeleteForm();                                                                          
> }                                                                                                     
>
>
> //...........................................................................function showRecord()
>
> function showRecord() {
>
>     global $fields;
>    global $dFields; 
>         global $rowInsert;
> // Loop through each log_book_id with an enabled checkbox
>     if (!isset($_POST['checkbox'])){                     
>         return true;                                     
>     }                                                    
>     //else                                               
>                                                          
>     foreach($_POST['checkbox'] as $key=>$val){           
>         $toDelete[] = $key;                              
>     }                                                    
>                                                          
>
> $db = "(DESCRIPTION =
>    (ADDRESS = (PROTOCOL = TCP)(HOST = 127.0.0.1)(PORT = 1521))
>    (CONNECT_DATA =                                            
>      (SERVER = DEDICATED)                                     
>      (SID = LMKIIIGDNSID)                                     
>    )                                                          
>  )";                                                          
> if ($conn=oci_connect('landon', 'rumprocella',$db))           
> {                                                             
>         echo "Successfully connected to Oracle.\n";           
> }                                                             
> else                                                          
> {                                                             
>  $err = OCIError();                                           
>  echo "Oracle Connect Error " . $err['message'];              
> }                                                             
>
> // errors
>     //get log_book_id table information
>     $user = "select * from log_book_id where log_id = $toDelete[0]";
>     $result = oci_parse($conn, $user);                              
>         $r = oci_execute($result);                                  
>
>     //display log_book_id information
>
>     $count = 0;
>     $newArray = oci_fetch_assoc($result);
>         if ($count == 0)        print_r($newArray);
>     $count = 1;                                    
>         foreach ($fields as $field){               
>                 $fieldx = strtoupper($field);      
>                 ${$field} = $newArray[$fieldx];    
>                 $dFields[$field] = ${$field};      
>         }                                          
>         $rowID = $newArray['LOG_ID'];              
>         $rowInsert = $rowID;                       
> print_r($dFields);                                 
> print ($rowInsert);                                
>         //note that we do not rely on the checkbox value as not all browsers submit it
>         //instead we rely on the name of the checkbox.                                
>
>     //  insert the row information in the entry form
>     global $fields;                                 
>     //end validation                                
>     displayEntryForm();                             
> }                                                   
>
> //...........................................................................function displayEntryForm()
> function displayEntryForm($errorMessages=null, $dFields=null){                                          
>     if (!empty($errorMessages)){                                                                        
>         echo "<ul><li>".explode('</li><li>', $errorMessages) . "</li></ul>";                            
>     }                                                                                                   
>                                                                                                         
>     global $dFields;                                                                                    
>     //sort out field values
>     global $fields;
>     foreach ($fields as $field){
>         if (isset($dFields[$field])){
>             ${$field} = $dFields[$field];
>         } else {
>             ${$field} = '';
>         }
>     }
>
>     echo <<<HTML
> <form action="{$_SERVER['PHP_SELF']}" method="post">
> <p>
> Flight Date:<br />
> <input type="text" size="20" maxlength="40" name="fdate"
> value="$fdate" />
> </p>
> <p>
> Aircraft Type:<br />
> <input type="text" size="20" maxlength="40" name="actype"
> value="$actype" />
> </p>
> <p>
> Aircraft ID:<br />
> <input type="text" size="20" maxlength="40" name="acid"
> value="$acid" />
> </p>
> <p>
> Number Landings:<br />
> <input type="text" size="20" maxlength="40" name="nlandings"
> value="$nlandings" />
> </p>
> <p>
> Number Hours:<br />
> <input type="text" size="20" maxlength="40" name="nhours"
> value="$nhours" />
> </p>
> <button type="submit" name = "insert" value="Insert Row!"
> style="color:maroon font:18pt Courier; font-weight:bold ">Insert Row
> </button>
> <button type="submit" name = "select" value="Show All!"
> style="color:red font:18pt Courier; font-weight:bold ">Show All
> </button>
> <button type="submit" name = "delete" value="Delete Row!"
> style="color:red font:18pt Courier; font-weight:bold ">Delete Row!
> </button>
> <button type="submit" name = "update" value="Update Row!"
> style="color:red font:18pt Courier; font-weight:bold ">Update Row!
> </button>
> </form>
> HTML;
> } //end display function
> ?>
>
>
>
>       
>
>
>   

Have you tried the $_SESSION variable?
http://us.php.net/manual/en/reserved.variables.session.php

Thank you,
Micah Gersten
onShore Networks
Internal Developer
http://www.onshore.com
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.