错误:注意:未定义的索引

时间:2013-04-26 04:05:45

标签: php variables error-handling insert

我有一个获取患者数据并将其发送到数据库的表单;但是,单击提交按钮时会发生错误;

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 个答案:

答案 0 :(得分: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 :(得分:0)

你的错误是自我定义的..它说你在尝试获取值的数组中没有这样的键

在$ _POST

中的值前面删除'$'

替换

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

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

在你的代码中还有其他一些像这样。

请避免使用mysql_ *切换到PDO或mysqli _ *