2013-04-26 110 views
0

我有一個獲取患者數據並將其發送到數據庫的表單;但是,點擊提交按鈕時會發生錯誤;錯誤:注意:未定義的索引

Notice: Undefined index: $patient_lastname line 14 

任何人都可以告訴我爲什麼會出現這個錯誤嗎?

insert.php

<!doctype html> 
<html lang="en"> 
<head> 
    <title>Form Input Data</title> 
    <script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script> 
</head> 
<body> 
    <form method="post" action="insert_ac.php"> 
    <br /> 
    <table border="1" bgcolor="lightblue"> 
     <tr> 
      <th> 
       Nurse Information 
      </th> 
     </tr> 
     <tr> 
      <td> 
       <label style="color: Red"> 
        Please select your name</label> 
      </td> 
     </tr> 
     <tr> 
      <td> 
       Fullname 
      </td> 
      <td> 
       <select name="valuelist"> 
        <option value="valuelist" name="nurse_name" value='<?php echo $nurse_name; ?>'></option> 
        <?php 
         $value=$_POST ["valuelist"]; 
         $con = mysql_connect("localhost","root","") or die('Could not connect:'.mysql_error()); 
         mysql_select_db("a&e", $con) or die('Could not select database.'); 

         $fetch_nurse_name = mysql_query("SELECT DISTINCT Fullname FROM nurse"); 

         while($throw_nurse_name = mysql_fetch_array($fetch_nurse_name)) { 
          echo '<option value=\"'.$throw_nurse_name[0].'">'.$throw_nurse_name[0].'</option>'; 
         } 
         echo "</select>"; 
        ?> 
      </td> 
     </tr> 
     <tr> 
      <td> 
       <label style="color: Red"> 
        Register Patient</label> 
      </td> 
     </tr> 
     <tr> 
      <th> 
       Patient Information 
      </th> 
     </tr> 
     <tr> 
      <td> 
       Forename 
      </td> 
      <td> 
       <input type="text" name="patient_name" size="30" required="true" /> 
      </td> 
     </tr> 
     <tr> 
      <td> 
       Surname 
      </td> 
      <td> 
       <input type="text" name="patient_lastname" size="30" required="true" /> 
      </td> 
     </tr> 
     <tr> 
      <td> 
       Gender 
      </td> 
      <td> 
       <select name="gender"> 
        <option value="M">Male</option> 
        <option value="F">Female</option> 
       </select> 
      </td> 
     </tr> 
     <tr> 
      <td> 
       Date_Of_Birth 
      </td> 
      <td> 
       <select name="dateOfBirth"> 
        <option value="">---select date---</option> 
        <?php for ($i = 1; $i <= 31; $i++) : ?> 
        <option value="<?php echo ($i < 10) ? '0'.$i : $i; ?>"><?php echo $i; ?></option> 
        <?php endfor; ?> 
       </select> 
       <select name="monthOfBirth"> 
        <option value="">---select month---</option> 
        <?php for ($i = 1; $i <= 12; $i++) : ?> 
        <option value="<?php echo ($i < 10) ? '0'.$i : $i; ?>"><?php echo $i; ?></option> 
        <?php endfor; ?> 
       </select> 
       <select name="yearOfBirth"> 
        <option value="">---Select year---</option> 
        <?php for ($i = 1945; $i < date('Y'); $i++) : ?> 
        <option value="<?php echo $i; ?>"><?php echo $i; ?></option> 
        <?php endfor; ?> 
       </select> 
      </td> 
     </tr> 
     <tr> 
      <td> 
       Address 
      </td> 
      <td> 
       <input type="text" name="address" size="54" required="true" /> 
      </td> 
     </tr> 
     <tr> 
      <td> 
       Patient_History 
      </td> 
      <td> 
       <textarea rows="5" cols="20" name="history" wrap="physical">Enter patient's history</textarea>:<br /> 
      </td> 
     </tr> 
     <tr> 
      <td> 
       Illness 
      </td> 
      <td> 
       <select name="illness"> 
        <option value="Life Threatening">Critical</option> 
        <option value="Immediate">Immediate</option> 
        <option value="Urgent">Urgent</option> 
        <option value="semi-urgent">Semi-Urgent</option> 
        <option value="non-urgent">Non-urgent</option> 
       </select> 
      </td> 
     </tr> 
     <tr> 
      <td> 
       Priority 
      </td> 
      <td> 
       <select name="priority"> 
        <option value="high">High Priority</option> 
        <option value="moderate">Moderate Priority</option> 
        <option value="low">Low Priority</option> 
       </select> 
      </td> 
      <br /> 
     </tr> 
     <tr> 
      <th> 
       Assessment 
      </th> 
     </tr> 
     <tr> 
      <td> 
       Symptoms 
      </td> 
      <td> 
       <input type="text" name="symptoms" size="30" required="true"/> 
      </td> 
     </tr> 
     <tr> 
      <td> 
       Diagnosis 
      </td> 
      <td> 
       <input type="text" name="diagnosis" size="30" required="true"/> 
      </td> 
     </tr> 
     <tr> 
      <td> 
       Treatment 
      </td> 
      <td> 
       <input type="text" name="treatment" size="30" required="true"/> 
      </td> 
     </tr> 
     <tr> 
      <td> 
       <label style="color: Red"> 
        Please assign a doctor</label> 
      </td> 
     </tr> 
     <tr> 
      <td> 
       Doctor 
      </td> 
      <td> 
       <select name="valuelist"> 
        <option value="valuelist" name="doc_name" value='<?php echo $doc_name; ?>'></option> 
        <?php 
        $value=$_POST ["valuelist"]; 
        $con = mysql_connect("localhost","root","") or die('Could not connect:'.mysql_error()); 
        mysql_select_db("a&e", $con) or die('Could not select database.'); 

        $fetch_doc_name = mysql_query("SELECT DISTINCT Name FROM doctor"); 

        while($throw_doc_name = mysql_fetch_array($fetch_doc_name)) { 
        echo '<option value=\"'.$throw_doc_name[0].'">'.$throw_doc_name[0].'</option>'; 
        } 
        echo "</select>"; 

        ?> 
      </td> 
     </tr> 
     <tr> 
      <td align="right"> 
       <input type="submit" name="submit" value="submit" required="true"/> 
      </td> 
     </tr> 
    </table> 
    </form> 
</body> 
</html> 

insert_ac.php

<?php 
$con = mysql_connect("localhost","root","") or die('Could not connect: ' . mysql_error()); 
mysql_select_db("a&e", $con) or die('Could not select database.'); 



//get Disease values from form 
$symptoms = mysql_real_escape_string($_POST['symptoms']); 
$diagnosis = mysql_real_escape_string($_POST['diagnosis']); 
$treatment = mysql_real_escape_string($_POST['treatment']); 

//Assigning value to variable for Patient 
$patient_name = ($_POST['patient_name']); 
$patient_lastname = ($_POST['$patient_lastname']); 
$gender = ($_POST['$gender']); 
$dateOfBirth = ($_POST['$dateOfBirth']); 
$monthOfBirth = ($_POST['$monthOfBirth']); 
$yearOfBirth = ($_POST['$yearOfBirth']); 
$address = ($_POST['address']); 
$history = ($_POST['history']); 
$illness = ($_POST['illness']); 
$priority = ($_POST['priority']); 

// Validate date of birth 
$date = $dateOfBirth.'-'.$monthOfBirth.'-'.$yearOfBirth; 

//Validate 


$sql ="INSERT INTO Disease(Illness, Symptoms, Diagnosis, Treatment) 
    VALUES('$illness', '$symptoms', '$diagnosis', '$treatment')"; 
mysql_query($sql,$con) or die('Error: ' . mysql_error()); 

$sql ="INSERT INTO Priority(Priority) 
    VALUES('$priority')"; 
mysql_query($sql,$con) or die('Error: ' . mysql_error()); 

$sql="INSERT INTO Patient(Forename, Surname, Gender, Date_Of_Birth, Address, Patient_History, Illness, Priority, Arrival_Time) 
VALUES('$patient_name', '$patient_lastname', '$gender', '$date', '$address', '$history', '$illness', '$priority', NOW())"; 
mysql_query($sql,$con) or die('Error: ' . mysql_error()); 

echo "1 record added"; 
// close connection 
mysql_close($con); 
?> 

回答

2

您錯誤地提交值前加入$

改變這種

$patient_lastname = ($_POST['$patient_lastname']); 
$gender = ($_POST['$gender']); 
$dateOfBirth = ($_POST['$dateOfBirth']); 
$monthOfBirth = ($_POST['$monthOfBirth']); 
$yearOfBirth = ($_POST['$yearOfBirth']); 

$patient_lastname = ($_POST['patient_lastname']); 
$gender = ($_POST['gender']); 
$dateOfBirth = ($_POST['dateOfBirth']); 
$monthOfBirth = ($_POST['monthOfBirth']); 
$yearOfBirth = ($_POST['yearOfBirth']); 

在這裏看到您的帖子輸入值的名稱屬性

<input type="text" name="patient_lastname" size="30" required="true" /> 

同去的其他爲好。

+1

這樣一個愚蠢的錯誤,謝謝 – user2321845 2013-04-26 04:18:03

+0

感謝您的幫助! – user2321845 2013-04-26 04:23:07

0

你的錯誤是自定義..它說有陣沒有這樣的關鍵從中您正試圖獲取價值

在值前刪除「$」 $ _ POST

更換

$patient_lastname = ($_POST['$patient_lastname']);

$patient_lastname = ($_POST['patient_lastname']);

有一些行吟詩人你也是這樣的代碼。

請避免使用mysql_ *切換到PDO或mysqli_ *