带有PDO的未定义变量下拉列表


Undefined Variable with PDO drop down list

>我有一个动态定义的下拉列表,定义如下:

  $sql = "SELECT cat_id, disease_main FROM lists ORDER BY cat_id ASC";
                foreach ($db->query($sql) as $row){//Array or records stored in $row
                if($row['cat_id']==0.01){ // 0.01 = "---- Please Select ----" (empty value)
                $dis_cat_main = "<option value='" . $row['cat_id'] . "'>" . 
                }else{
                $dis_cat_main =  "<option value='" . $row['cat_id'] . "'>" . 
                /* Option values are added by looping through the array */
                    }
                }

我已将此代码放在主页.php文件中:如下所示:

require('../../includes/config.inc.php');
require('../../includes/db_connection.php');
require('../../includes/functions.php');
// Second --> Let's 'Check whether user has the rights to see current page or not
if(!isLoggedIn()) //"isLoggedIn" is a pre-specified function in functions.php file
    {
    header('Location: ../index.php');
    die();
    }

// Define Page Title:
$page_title = 'Kardia: Add New Patient';
// $base_url= '';
include('../elements/layouts/header.html');
/*
Setup some variables/arrays: First we are creating a blank array called action and then setting an array value of result.
Result is going to hold a value of either success or error. Next we create another blank array called text.
This is going to hold any text we want to show the user during the signup.
*/
$action = array();
$action['result'] = null;
$text = array();
// Check if the form has been submitted:
 if (($_SERVER['REQUEST_METHOD'] == 'POST') && isset($_POST['enroll'])) {

     //let's define the variables involved, starting with the fields coming from the Demographics form
     $pid        = null; // this can be anything (MySQL will overwrite this value in any case)
     $addmod_ts =   date('Y-m-d H:i:s');
     $address   =  $_POST['address'];
     $age       = isset($_POST['age']) ? $_POST['age'] : "999"; // ternary operator, in case Age has not been defined, default value will be 999
     $censor_d  = $_POST['censor_d'];
     $city  =   $_POST['city'];
     $clinic    =   $_POST['clinic'];
     $death =   isset($_POST['death']) ? $_POST['death'] : "0"; // ternary operator, assign deafult value of 0 ("No") to $death
     $dis_cat_main  =   $_POST['dis_cat_main'];
     $dis_cat_spec  =   $_POST['dis_cat_spec'];
     $disease_1 =   $_POST['disease_1'];
     $disease_2 =   $_POST['disease_2'];
     $disease_3 =   $_POST['disease_3'];
     $disease_4 =   $_POST['disease_4'];
     $dob       =   $_POST['dob'];
     $email_1   =   $_POST['email_1'];
     $email_2   =   $_POST['email_2'];
     $firstname =   $_POST['firstname'];
     $fup_months    =  isset($_POST['fup_months']) ? $_POST['fup_months'] : "999"; // ternary operator (deafult value: 999)
     $fup_years =   isset($_POST['fup_years']) ? $_POST['fup_years'] : "999"; // ternary operator (deafult value: 999)
     $institution   = $_POST['institution'];
     $lastname  =   $_POST['lastname'];
     $locked    =   $_POST['locked'];
     $notes =   $_POST['notes'];
     $phone_1   =   $_POST['phone_1'];
     $phone_2   =   $_POST['phone_2'];
     $phone_3   =   $_POST['phone_3'];
     $physician =   $_POST['physician'];
     $province  =   $_POST['province'];
     $pt_department =   $_POST['pt_department'];
     $pt_location   =   $_POST['pt_location'];
     $recruit_ts    =   date('Y-m-d H:i:s');
     $region    =   $_POST['region'];
     $research  =   isset($_POST['research']) ? $_POST['research'] : "0"; // ternary Operator, defualt value: 0 ("No")
     $saved =   $_POST['saved'];
     $sex   =   $_POST['sex'];
     $specdis_1a    =   $_POST['specdis_1a'];
     $specdis_1b    =   $_POST['specdis_1b'];
     $ssn   =   $_POST['ssn'];
     $study =   $_POST['study'];
     $zip   =   $_POST['zip'];
     // In this case, let us start some basic, server-side, validation:
     //Let us start basic validation: make sure everything required has been inserted
     if (empty($lastname)){
         $action['result'] = 'error'; array_push($text,'Please insert patient last name');
     }
     if (empty($firstname)){
         $action['result'] = 'error'; array_push($text,'Please insert patient first name ');
     }
     if (!is_numeric ($sex)) {
         $action['result'] = 'error'; array_push($text,'Please insert patient gender'); // SEX is a Number so must be treated accordingly (if empty does not work here)
     }
     if (empty($disease_1)){
         $action['result'] = 'error'; array_push($text,'Please insert at least the first medical issue'); // Disease_1 is a Number so must be treated accordingly (if empty does not work here)
     }
     if (empty($address)){
         $action['result'] = 'error'; array_push($text,'Please insert patient Address');
     }
     if (empty($city)){
         $action['result'] = 'error'; array_push($text,'Please insert city name');
     }
     if (empty ($phone_1)){
         $action['result'] = 'error'; array_push($text,'Please insert at least one valid phone number ');
     }
     if (empty($email_1)){
         $action['result'] = 'error'; array_push($text,'Please insert at least one valid e-mail address');
     }
     // then let us define and validate DOB and put the date in SQL format
     // Validate the Date of Birth (DOB)
     if (empty($dob)){
         $action['result'] = 'error'; array_push($text,'Please insert date of birth for patient');
     }

// If all required data are present , in the correct format, and there are no errors, we can go ahead and enroll the patient:
    if($action['result'] != 'error'){
        // let's start a try/catch loop and submit the query via PDO prepared statement, using named placeholders
        try  {
            // Finally, we can go ahead with the SQL INSERT query
            $sql = 'INSERT INTO `demographics` (    PID,
                                                    ADDMOD_TS,
                                                    ADDRESS,
                                                    AGE,
                                                    CENSOR_D,
                                                    CITY,
                                                    CLINIC,
                                                    DEATH,
                                                    DIS_CAT_MAIN,
                                                    DIS_CAT_SPEC,
                                                    DISEASE_1,
                                                    DISEASE_2,
                                                    DISEASE_3,
                                                    DISEASE_4,
                                                    DOB,
                                                    EMAIL_1,
                                                    EMAIL_2,
                                                    FIRSTNAME,
                                                    FUP_MONTHS,
                                                    FUP_YEARS,
                                                    INSTITUTION,
                                                    LASTNAME,
                                                    LOCKED,
                                                    NOTES,
                                                    PHONE_1,
                                                    PHONE_2,
                                                    PHONE_3,
                                                    PHYSICIAN,
                                                    PROVINCE,
                                                    PT_DEPARTMENT,
                                                    PT_LOCATION,
                                                    RECRUIT_TS,
                                                    REGION,
                                                    RESEARCH,
                                                    SAVED,
                                                    SEX,
                                                    SPECDIS_1A,
                                                    SPECDIS_1B,
                                                    SSN,
                                                    STUDY,
                                                    ZIP
                                                              )
                                    VALUES (            :pid,
                                                        NOW(),
                                                        :address,
                                                        :age,
                                                        :censor_d,
                                                        :city,
                                                        :clinic,
                                                        :death,
                                                        :dis_cat_main,
                                                        :dis_cat_spec,
                                                        :disease_1,
                                                        :disease_2,
                                                        :disease_3,
                                                        :disease_4,
                                                        :dob,
                                                        :email_1,
                                                        :email_2,
                                                        :firstname,
                                                        :fup_months,
                                                        :fup_years,
                                                        :institution,
                                                        :lastname,
                                                        :locked,
                                                        :notes,
                                                        :phone_1,
                                                        :phone_2,
                                                        :phone_3,
                                                        :physician,
                                                        :province,
                                                        :pt_department,
                                                        :pt_location,
                                                        NOW(),
                                                        :region,
                                                        :research,
                                                        :saved,
                                                        :sex,
                                                        :specdis_1a,
                                                        :specdis_1b,
                                                        :ssn,
                                                        :study,
                                                        :zip
                                                )';
            // a. Prepare the statement
            $stmt = $db->prepare($sql);
            // b. Bind values and variables
            $stmt->bindParam(':pid' , $pid, PDO::PARAM_INT);
            // ADDMOD_TS here, not to be bound (it is the NOW() MySQL function)
            $stmt->bindParam(':address' , $address, PDO::PARAM_STR);
            $stmt->bindParam(':age' , $age, PDO::PARAM_INT);
            $stmt->bindParam(':censor_d' , $censor_d, PDO::PARAM_STR);
            $stmt->bindParam(':city' , $city, PDO::PARAM_STR);
            $stmt->bindParam(':clinic' , $clinic, PDO::PARAM_STR);
            $stmt->bindParam(':death' , $death, PDO::PARAM_INT);
            $stmt->bindParam(':dis_cat_main' , $dis_cat_main, PDO::PARAM_STR);
            $stmt->bindParam(':dis_cat_spec' , $dis_cat_spec, PDO::PARAM_STR);
            $stmt->bindParam(':disease_1' , $disease_1, PDO::PARAM_STR);
            $stmt->bindParam(':disease_2' , $disease_2, PDO::PARAM_STR);
            $stmt->bindParam(':disease_3' , $disease_3, PDO::PARAM_STR);
            $stmt->bindParam(':disease_4' , $disease_4, PDO::PARAM_STR);
            $stmt->bindParam(':dob' , $dob, PDO::PARAM_STR);
            $stmt->bindParam(':email_1' , $email_1, PDO::PARAM_STR);
            $stmt->bindParam(':email_2' , $email_2, PDO::PARAM_STR);
            $stmt->bindParam(':firstname' , $firstname, PDO::PARAM_STR);
            $stmt->bindParam(':fup_months' , $fup_months, PDO::PARAM_INT);
            $stmt->bindParam(':fup_years' , $fup_years, PDO::PARAM_INT);
            $stmt->bindParam(':institution' , $institution, PDO::PARAM_STR);
            $stmt->bindParam(':lastname' , $lastname, PDO::PARAM_STR);
            $stmt->bindParam(':locked' , $locked, PDO::PARAM_INT);
            $stmt->bindParam(':notes' , $notes, PDO::PARAM_STR);
            $stmt->bindParam(':phone_1' , $phone_1, PDO::PARAM_STR);
            $stmt->bindParam(':phone_2' , $phone_2, PDO::PARAM_STR);
            $stmt->bindParam(':phone_3' , $phone_3, PDO::PARAM_STR);
            $stmt->bindParam(':physician' , $physician, PDO::PARAM_STR);
            $stmt->bindParam(':province' , $province, PDO::PARAM_STR);
            $stmt->bindParam(':pt_department' , $pt_department, PDO::PARAM_STR);
            $stmt->bindParam(':pt_location' , $pt_location, PDO::PARAM_STR);
            // RECRUIT_TS parameter here, not to be bound (it is the NOW() MySQL function
            $stmt->bindParam(':region' , $region, PDO::PARAM_INT);  // Numbered DropDown List
            $stmt->bindParam(':research' , $research, PDO::PARAM_INT);
            $stmt->bindParam(':saved' , $saved, PDO::PARAM_INT);
            $stmt->bindParam(':sex' , $sex, PDO::PARAM_INT);
            $stmt->bindParam(':specdis_1a' , $specdis_1a, PDO::PARAM_STR);
            $stmt->bindParam(':specdis_1b' , $specdis_1b, PDO::PARAM_STR);
            $stmt->bindParam(':ssn' , $ssn, PDO::PARAM_STR);
            $stmt->bindParam(':study' , $study, PDO::PARAM_STR);
            $stmt->bindParam(':zip' , $zip, PDO::PARAM_STR);
            // Kick in the prepared statement
            $stmt->execute();

        } catch (PDOException $e) {
            echo '<p class="error"> An Error Occurred: ' . $e->getMessage() . '</p>'; // Report the Error
              }
        // Tell the user we have done successfully
        $action['result'] = 'success';
        array_push($text,'Patient is on Kardia now');
    }

但是当我尝试在 home 中定义以下字段(下拉列表)时.html:

<label for="dis_cat_main" </label>
<select name="dis_cat_main" id="dis_cat_main" title="disease Category">
<?php echo $disease_cat_main; ?>
</select>

我收到"未定义的变量dis_cat_main"错误消息。

谁能帮我了解如何解决这个问题?

多谢

将脚本放在此行的位置下方:<?php echo $disease_cat_main; ?>

 sql = "SELECT cat_id, disease_main FROM lists ORDER BY cat_id ASC";
 foreach($db - > query($sql) as $row) { //Array or records stored in $row
     if ($row['cat_id'] == 0.01) { // 0.01 = "---- Please Select ----" (empty value)
         echo "<option value='".$row['cat_id']."'>";
     } else {
         echo "<option value='".$row['cat_id']."'>";
         /* Option values are added by looping through the array */
     }
 }

您的评论的解决方案:

 sql = "SELECT cat_id, disease_main FROM lists ORDER BY cat_id ASC";
 $dis_cat_main = "";
 foreach($db - > query($sql) as $row) { //Array or records stored in $row
     if ($row['cat_id'] == 0.01) { // 0.01 = "---- Please Select ----" (empty value)
         $dis_cat_main .= "<option value='".$row['cat_id']."'>";
     } else {
         $dis_cat_main .= "<option value='".$row['cat_id']."'>";
         /* Option values are added by looping through the array */
     }
 }

然后在您的 HTML 中:

<?php echo $dis_cat_main; ?>

只要两个PHP脚本在同一页面上,这应该可以工作。