Re: Program Dies

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 




On Oct 14, 2012, at 4:57 PM, Ethan Rosenberg, PhD wrote:

Dear List -

Thank you ever so much for all your help.

I apologize in advance for dumping all this code on you. I cannot get any debugger to work for me [see my separate email on debuggers]. I placed numerous echo and print_r statements in the code, and could not find the error.

The program worked perfectly previously, and now it dies.

It mimic a doctor's office where a study is being performed on obesity. The parameter is a calculated value called BMI.

In the initial part of the program, a search is performed to obtain the medical record . This works perfectly. The second step is to obtain data at the patient visit, which is triggered by a button "Do you wish to enter visit data?" If that button is clicked, the program returns to the original welcome screen.

Advice and help, please.

Here is the code:

================
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<html>
<head>
<title>Search of Initial Input Data</title>

<style type="text/css">
table {
border-collapse: collapse;
}
td {
border-top: "1px solid #000";
border-bottom: "1px solid #000";
border-left:"1px solid #000";
border-right: "1px solid #000";
}
tr {
border-top: "1px solid #000";
border-bottom: "1px solid #000";
border-left:"1px solid #000";
border-right: "1px solid #000";
}

#more{
margin-top: 90%;
}

#vs{
   position: absolute;
   left: 46%;
}
#vs2{
   position: absolute;
   left: 44%;
}


</style>

</head>
<body>
<strong><center>Search of Input Data</strong></center>
<pre>
<?php
   session_start();
   ini_set('display_errors', 'On');
   ini_set('display_startup_errors', 'On');
   error_reporting(E_ALL);
   $fptr2=fopen("/var/www/scratch.txt", "r+");
   require '/var/www/pass.inc';

   $db = "hospital2";

   $cxn = mysqli_connect($host,$user,$password,$db);
       if (!$cxn)
       {
           die('Connect Error (' . mysqli_connect_errno() . ') '
           . mysqli_connect_error());
       }
       mysqli_select_db($cxn, "hospital2");
       global $n1;
       $n1 = 1;
       $flag =1;
       global $Mdr;
       global $cxn;
       global $Date;
       global $MedRec;
       global $medrec;
       global $result;
       global $result1;
       global $result2;
       global $result43;
       global $result5;
       global $result6;
       global $result7;
       global $result8;
       global $query;
       global $MedRec2;
       global $row1;
       global $row2;
       global $row3;
       global $row4;
       global $row5;
       global $row6;
       static $_GLOBALS;
       global $query;
       global $not_yet;
       $not_yet = 1;
       global $setter;
       $setter = 0;
       global $errors_array;

   if(!isset($_REQUEST["welcome_already_seen"]))
     {
       $_POST['Site']   = '';
       $_POST['MedRec'] = '';
       $_POST['Fname']  = '';
       $_POST['Lname']  = '';
       $_POST['Phone']  = '';
       $_POST['Sex']    = '';
       $_POST['Height'] = '';
     }



     $errors_array = array();


       if($_REQUEST["welcome_already_seen"]== "already_seen")
       {
           check_data();
              if(count($errors_array) != 0)
              {
                  echo "start #1";
               show_errors();
               show_welcome();
           }
           else
           {
               show_welcome();
               handle_data($cxn);
           }
       }
       if(!isset($_REQUEST["welcome_already_seen"]))
             show_welcome();



       global $errors;
       $errors = 0;
       function check_data()
         {
           global $errors_array;
           global $errors;

           if(!empty($_POST['Fname']))
           if(preg_match('#^[a-zA-Z]\w#', $_POST['Fname'])== 0)
           {
$errors_array[] = "<center><b><font color='red'>First Name must be Text</font></center></b>";
               $errors = 1;
           }

           if(!empty($_POST['Lname']))
           if(preg_match('#^[a-zA-Z]\w#', $_POST['Lname'])== 0)

           {
$errors_array[] = "<center><b><font color='red'>Last Name must be Text</font></center></b>";
               $errors = 1;
           }

           if(!empty($_POST['Site']))
           if(preg_match('#^[A-Z]\w{1}#', $_POST['Site'])== 0)

           {
$errors_array[] = "<center><b><font color='red'>Site must be TWO CAPITAL LETTERS</font></center></b>";
               $errors = 1;
           }

           if(!empty($_POST['MedRec']))
           if(preg_match('#^\d{5}$#', $_POST['MedRec']) == 0)
           {
$errors_array[] = "<center><b><font color='red'>Medical Record must be FIVE NUMBERS</font></center></b>";
               $errors = 1;
           }

           if(!empty($_POST['Height']))
if(preg_match('#^[5-6]{1}$|^[5-6]{1}[0-9]{1}$#', $_POST['Height']) == 0)
           {
$errors_array[] = "<center><b><font color='red'>Height must be between 50 and 69</font></center></b>";
                  $errors = 1;
           }

           if(!empty($_POST['Phone']))
// ^\(?(\d{0,3})\)?(\.|\/)|\s|\-)?(\d{3})(\.| \/)|\s|\-)?(\d{4})\s?(x|ext)?(\d*)$ if(preg_match('#^\d{3}-\d{3}-\d{4}$#', $_POST['Phone']) == 0)
           {
$errors_array[] = "<center><b><font color='red'>Phone Format xxx-xxx-xxxx</font></center></b>";
               $errors = 1;
           }

           if(!empty($_POST['Bday']))
// ^\(?(\d{0,3})\)?(\.|\/)|\s|\-)?(\d{3})(\.| \/)|\s|\-)?(\d{4})\s?(x|ext)?(\d*)$ if(preg_match('#^\d{4}-\d{2}-\d{2}$#', $_POST['Bday']) == 0)
           {
$errors_array[] = "<center><b><font color='red'>Birthday Format yyyy-mm-dd</span></center></b>";
               $errors = 1;
           }

       }



        function show_errors()
       {
           global $errors_array;

           foreach ($errors_array as $err)
           {
             echo $err, "<br>";
           }
     }


       function handle_data($cxn)
       {
       global $cxn;


           if(isset($_Request['Sex'])&& trim($_POST['Sex']) != '' )
           {
               if ($_REQUEST['Sex'] === "0")
               {
                   $sex = 'Male';
               }
           else
               {
                   $sex = 'Female';
               }

            }



       $allowed_fields = array
( 'Site' => 's', 'MedRec' => 'i', 'Fname' => 's', 'Lname' => 's','Phone'=> 's', 'Height' => 'i', 'Sex' => 's',
           'Hx' => 's','Bday' => 's',  'Age' => 'i'
           );

       if(empty($allowed_fields))
       {
            echo "ouch";
       }
// Configure the query and the acceptable params to put into the WHERE clause
   $sql12 = 'SELECT * FROM Intake3 WHERE 1';
  // Magically put everything together
   $types = '';
   $args = array();
   foreach( $allowed_fields as $k => $type )
   {
    if( !array_key_exists( $k, $allowed_fields ) )
           continue;
       else
       {
           if( ($_POST[$k]) != '')
           {
$args[] = &$_POST[$k]; // Note the addition of the ampersand here
               $types .= $type;
               $sql12 .= " AND ($k = ?)";
           }
       }
   }
   // For debugging and demonstration
   echo 'Query: ' . $sql12 . PHP_EOL;
   echo 'Bind types: ' . $types . PHP_EOL;
   echo 'Arguments:' . PHP_EOL;
   echo "arguments<br />";
   print_r($args);

   $stmt = mysqli_stmt_init($cxn);
   mysqli_stmt_prepare( $stmt, $sql12 );
   if( !$stmt )
       throw new Exception( 'Error preparing statement' );

// Put the statement and types variables at the front of the params to pass to mysqli_stmt_bind_param() array_unshift( $args, $stmt, $types ); // Note that I've moved this call. Apparently it doesn't pass back the result. I guess sometimes I just forget these things.

   // mysqli_stmt_bind_param()
   if( !call_user_func_array( 'mysqli_stmt_bind_param', $args ) )
       throw new Exception( 'Failed calling mysqli_stmt_bind_param' );

   if( !mysqli_stmt_execute( $stmt ) )
       throw new Exception( 'Error while executing statement' );
mysqli_stmt_bind_result( $stmt, $Site, $MedRec, $Fname, $Lname, $Phone, $Height, $Sex, $Hx, $Bday, $Age);

      $test = mysqli_stmt_fetch($stmt);

   if(($test) == 0)
   {
   ?>
<br /><br /><center><b><p style="color: red; font-size:14pt;" >No Records Retrieved</center></b></style></p>

<?php
       exit();
   }
mysqli_stmt_bind_result( $stmt, $Site, $MedRec, $Fname, $Lname, $Phone, $Height, $Sex, $Hx, $Bday, $Age);


   if(count($errors_array) == 0)
   {
?>
<center><b>Search Results</b></center><br />
<center>
<table border="4" cellpadding="5" cellspacing="55" rules="all" frame="box">
<tr class="heading">
<th>Site</th>
<th>Medical Record</th>
<th>First Name</th>
<th>Last Name</th>
<th>Phone</td>
<th>Height</td>
<th>Sex</td>
<th>History</td>
<th>Birthday</td>
<th>Age</td>
</tr>
<?php

   do
   {

       {
           global $result1;
           echo "<tr>\n";
           $_SESSION['exe'] = 2;
           echo "<td> $Site </td>\n";
           echo "<td> $MedRec </td>\n";
           echo "<td> $Fname </td>\n";
           echo "<td> $Lname </td>\n";
           echo "<td> $Phone </td>\n";
           echo "<td> $Height </td>\n";
           echo "<td> $Sex </td>\n";
           echo "<td> $Hx </td>\n";
           echo "<td> $Bday </td>\n";
           echo "<td> $Age </td>";
       }
       echo "</center>";
       echo "</form>";
   } while (mysqli_stmt_fetch($stmt));


   $Mdr  = $_POST['MedRec'];

   global $Mdr;

   $flagMDR = 0;

    if ($flagMDR == 0)
$query2 = "select * from Visit3 where 1 AND (Site = 'AA') AND (MedRec = $Mdr)";
   if ($flagMDR == 1)
$query2 = "select * from Visit3 where 1 AND (Site = 'AA') order by MedRec";
   $result2 = mysqli_query($cxn, $query2);
   } // count errors = 0

global $finished;
$finished = 0;

   switch ( $_POST['next_step'] )
   {

       case "step10":
       {
           echo "step10 $_REQUEST[welcome_already_seen]";
           global $row;
           global $row4;
           global $Mdr;
           $_SESSION['exe'] = 1;
           echo "step10 <br />";
           print_r($_SESSION);
           echo "step10 decission<br />";
           echo $_POST['decision'] ;
           if (!isset($_POST['decision']))
           {
               $_POST['decision'] = 5;
           }

           if ($_POST['decision'] == 0)
           {
               exit();
           }
           if ($_POST['decision'] == 1)
           {



       echo "<form method=\"post\" action=\"\">";
           echo "<center>" ;
echo "<input type=\"hidden\" name=\"next_step\" value= \"step4\" />";
           echo "<enter><br />";
echo "Medical Record: &nbsp<input type=\"text\" name= \"MedRec\" value = $_SESSION[MedR]></input>"; echo "&nbsp&nbsp Weight: <input type=\"decimal\" name= \"Weight\" ></input><br /><br /><br /><br /> &nbsp&nbsp&nbsp&nbsp ";
           echo "Notes: <br /> ";
echo "<textarea style=\"overflow: scroll\"; cols=\"60\" rows=\"10\" wrap=\"hard\" name=\"Notes\" ></textarea> </input><p />"; echo "<input type=\"submit\" value = \"Submit\"/><br / ><br />"; echo " <input type=hidden name=\"datain\" value='already'>";

           echo "<input type = \"reset\" /></center>";
       echo "</form>";


       } //decission = 1


   } // step 10



   case "step4":
   {
       if (!isset($_POST['datain']))
       {
           $_POST['datain'] = 5;
       }
$sql10 = "select * from Intake3 where MedRec= $_SESSION[MedR]";
           $result10 = mysqli_query($cxn, $sql10);

?>

<center><b>Search Results 3b</b></center><br />

<center><table border="4" cellpadding="5" cellspacing="55" rules="all" frame="box">
<tr class=\"heading\">
<th>Site</th>
<th>Medical Record</th>
<th>First Name</th>
<th>Last Name</th>
</tr>


<?php

   while($row10 = mysqli_fetch_row($result10))
   {
       echo "<tr>\n";
       echo "<td> $row10[0] </td>\n";
       echo "<td> $row10[1] </td>\n";
       echo "<td> $row10[2] </td>\n";
       echo "<td> $row10[3] </td>\n";
       echo "<td> $row10[4] </td>\n";
       echo "<td> $row10[5] </td>\n";
       echo "<td> $row10[6] </td>\n";
       echo "<td> $row10[7] </td>\n";
       echo "<td> $row10[8] </td>\n";
       echo "<td> $row10[9] </td>\n";


       echo "</tr>\n";
       echo "<br /><br /><br />";


       echo "</table>";

       $_REQUEST['MDR'] = $row10[1];


       if( $_POST['datain'] == "already")
       {
           global $row1;
           global $row4;
           global $medrec;

           date_default_timezone_set('America/New_York');


            $Date  = date('Ymd');
            $Date2 = date('d M Y');


           echo $row10[5];
           $Weight = $_POST['Weight'];
           $Notes  = $_POST['Notes'];
           $Height = $_SESSION['Height'];
           $BMI = ($Weight*703)/($Height*$Height);
           $BMI = round($BMI,1);
           $_POST['BMI'] = $BMI;

           $fptr1 = fopen("/home/ethan/PHP/HRecnumSite", "r+");
           fscanf($fptr1,"%d %s",$Num, $Site);

            $stmt = mysqli_stmt_init($cxn);
           $sql3 = "select max(Indx) from Visit3";
           $result7 = mysqli_query($cxn, $sql3);
           $row7 = mysqli_fetch_array($result7);

           $Indx = $row7[0];
$sql2 = "INSERT INTO Visit3(Indx, Site, MedRec, Notes, Weight, BMI, Date) VALUES(?, ?, ?, ?, ?, ?, ? )";

               mysqli_stmt_prepare( $stmt, $sql2 );
               echo "statement";
               print_r($stmt);
               $Indx = $Indx + 1;
               $_POST['Indx'] = $Indx;
               $_POST['Date'] = $Date;

               $In = (int)$Indx;
               $St = (string)$Site;
               $MR = $_SESSION['MedR'];
               $Nt = (string)$Notes;
               $Wt = (int)$Weight;
               $Bm = (int)$BMI;
               $Dt = (string)$Date;

if( !mysqli_stmt_bind_param($stmt, 'isisiis', $Indx, $Site, $MR, $Notes, $Weight, $BMI, $Date))
               {
echo "Error in mysqli_stmt_bind_param ".mysqli_stmt_error($stmt);
                    exit();
               }
               print_r($_POST);
                 if(!mysqli_execute($stmt))
                 {
echo "Error in mysqli_execute. ".mysqli_stmt_error($stmt);
                   exit();
               }
?>


<center>
<table border="4" cellpadding="5" cellspacing="55" rules="all" frame="box">
<tr class=\"heading\">
<th>Index</th>
<th>Site</th>
<th>Medical Record</th>
<th>Notes</th>
<th>Weight</th>
<th>BMI</td>
<th>Date</td>
</tr>

<?php


       echo "<tr>\n";
       echo "<td> $Indx </td>\n";
       echo "<td> $Site </td>\n";
       echo "<td> $_REQUEST[MedRec] </td>\n";
       echo "<td> $Notes </td>\n";
       echo "<td> $Weight </td>\n";
       echo "<td> $BMI </td>\n";
       echo "<td> $Date2 </td>\n";
       echo "</tr>\n";
       echo "</table>";


   } //datain
   } //fetch row
   } // Step4
   case "step5":
   {
       if ($_POST['next_step']== 'step4')
       {
           echo "<br /><br />Do you wish to enter more data?";
echo <<<FORM
<form name="form2" action="Start-Database.html" method="post">
<input type="submit" name="button" value="YES" >
</form>

<form id="form1" action="exiter.php" method="post" >
<input type="submit" name="button" value="NO" >
</form>

FORM;
       exit;
       } // POST next step == step4
       } //step5

   case "step2":
   {
       global $Mdr;
       global $query;
       $decision = 5;

           if ($_SESSION['exe'] == 1)
           {
               break;
           }

       $_SESSION['exe'] = 2;
           if ($_SESSION['exe'] == 2)
           {


?>
<center>Do you Wish to Enter Visit Data? </center>
<form method="post" action="">
<div id="vs"><input type="radio" name="decision" value="1" checked/ >Yes<input type="radio" name="decision" value="0" />No</div><br / ><br /> <center></center><input type="submit" value="Enter Visit Data" /></ center>
<input type="hidden" name="next_step" value="step10" />
<input type="hidden" name="execute" value="0" />
</form>
<br />
<?php
       }

       if ($_REQUEST['decision']== 1)
       {
           $_SESSION['exe'] = 1;
       }


   }  //step 2
   }
   } //switch

function show_welcome()
{

   $first_name = isset($_REQUEST["Fname"])  ? $_REQUEST["Fname"] : "";
   $last_name  = isset($_REQUEST["Lname"])  ? $_REQUEST["Lname"] : "";
$medrec = isset($_REQUEST["MedRec"]) ? $_REQUEST["MedRec"] : "";
   $phone      = isset($_REQUEST["Phone"])  ? $_REQUEST["Phone"] : "";
$height = isset($_REQUEST["Height"]) ? $_REQUEST["Height"] : "";
   $birthday   = isset($_REQUEST["Bday"]) ?   $_REQUEST["Bday"] : "";

?>

<?php
   echo "<form  method=\"post\">";
echo " <center>Site: <input type=\"text\" name=\"Site\" value=\"AA \" /></input>"; echo " Record Number: <input type=\"text\" name=\"MedRec\" value='", $medrec, "' /></input>"; echo " First Name: <input type=\"text\" name=\"Fname\" value='", $first_name, "' />"; echo " Last Name: <input type=\"text\" name=\"Lname\" value='", $last_name, "' /></input><br /><br />"; printf("Two Capital Letters\t\t\t Five Numbers\t\t\t\t Text - No Numbers\t\t\t Text - No Numbers\n\n\n"); echo " Phone: <input type=\"text\" name=\"Phone\" value='", $phone, "' /></input>"; echo " Height: <input type=\"decimal\" name=\"Height\" value='", $height, "' /></input><br /><br />";
   printf("XXX-XXX-XXXX\t\t\t    Inches\n\n");
echo "<input type=\"hidden\" name=\"next_step\" value=\"step2\" / >";


echo " <br/ ><br />Birthday: <input type=\"text\" name=\"Bday\" value='", $birthday, "' /></input><br /><br />";
   printf("\t\tYYYY-MM-DD   \n\n");
echo " Male<input type=\"radio\" name=\"Sex\" value = \"Male\" checked>"; echo " Female<input type=\"radio\" name=\"Sex\" value = \"Female \" ><br /><br /><br />";
   echo " <input type=\"submit\"  /><br /><br />";
   echo " <input type=\"reset\" value = \"Clear Form\"  /></center>";
   echo " <input type=hidden name='welcome_already_seen'
             value='already_seen'>";
   if (isset($_REQUEST['MedRec']))
   $setter = 1;

}


?>

</pre></body></html>

================

Ethan
--


--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Ethan,
Try removing functions and test. Create errors yourself. Then place that function back in. Remove the next function... etc. Test with each function and make sure they error out, then when you get to the function that doesn't error and it still doesn't work, there it is!

HTH,
Best,

Karl DeSaulniers
Design Drumm
http://designdrumm.com


--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux