如何在php中修复未定义的变量

时间:2016-05-30 06:33:31

标签: php mysql undefined-index

我正在尝试在连接了mysql数据库的php中执行更新功能。我将更新代码放在名为parcelEdit.php的文件中。这是我的parcelEdit.php代码

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Updating Parcel Details</title>
<link rel="stylesheet" href="css/style.css" />

</head>

<?php 
include('db.php');

if(isset($_POST['update']))
{   
$parcelID = $_POST['parcelID'];
$owner = $_POST['owner'];
$rcv_date = $_POST['rcv_date'];
$pck_date = $_POST['pck_date'];
$status = $_POST['status'];

// checking empty fields
if (empty($parcelID) || empty($owner) || empty($rcv_date)||
empty($pck_date)|| empty($status)) {    

if(empty($parcelID)) {
echo "<font color='red'>Parcel ID field is empty.</font><br/>";}
if(empty($owner)) {
echo "<font color='red'>Owner Name field is empty.</font><br/>";}
if(empty($rcv_date)) {
echo "<font color='red'>Received Date field is empty.</font><br/>";}
if(empty($pck_date)) {
echo "<font color='red'>Picked Up Date field is empty.</font><br/>";}
if(empty($status)) {
echo "<font color='red'>Parcel Status field is empty.</font><br/>";}    
} else {    

//updating the table
$result = mysql_query("UPDATE parcel SET parcelOwner = '$owner',
dateReceived = '$rcv_date', datePickup = '$pck_date', parcelStatus =
'$status' WHERE parcelID='$parcelID'");

//redirectig to the display page. In our case, it is index.php
header("Location: parcelView.php");
}
}
?>
<?php

//getting id from url
if(isset($_GET['parcelID'])){
$parcelID = $_GET['parcelID'];
}
//selecting data associated with this particular id
if(isset($parcelID)){
$result = mysql_query("SELECT * FROM parcel  WHERE parcelID='$parcelID'");

while($res = mysql_fetch_array($result))
{
//$mem_id= $res['mem_id'];
$parcelID= $res['parcelID'];
$owner= $res['parcelOwner'];
$rcv_date= $res['dateReceived'];
$pck_date= $res['datePickup'];
$status= $res['parcelStatus'];
}}

?>
<body>
    <body style='background: url(mailbox.jpg)'>
    <div align="center">
    <h1>Update Parcel Details</h1>
    <form method="post" enctype="multipart/form-data">
        <table>
        <tr>
            <Td> PARCEL ID : </td>
            <td><input name="parcelID" type="text" id="parcelID" value=<?php
echo $parcelID;?>></td>
        </tr>
        <tr>
            <Td> OWNER : </td>
            <td><input name="owner" type="text" id="owner" value=<?php echo
$owner;?>></td>
        </tr>
        <tr>
            <Td> DATE RECEIVED : </td>
            <td><input name="rcv_date" type="text" id="rcv_date" value=<?php
echo $rcv_date;?>></td>
        </tr>
        <tr>
            <Td> DATE PICKED UP : </td>
            <td><input name="pck_date" type="text" id="pck_date" value=<?php
echo $pck_date;?>></td>
        </tr>
        <tr>
            <Td> STATUS : </td>
            <td><input name="status" type="text" id="status" value=<?php
echo $status;?>></td>
        </tr>
        <tr>
            <Td colspan="2" align="center">
            <input type="submit" value="Update Records" name="update"/>
            </Td>
        </tr>
        </table>
    </form>
    </div>

</body>

</html>

我得到了这些错误

  

注意:未定义的变量:parcelID in   第77行的C:\ xampp \ htdocs \ psmtest1 \ parcelEdit.php

     

注意:未定义的变量:所有者   第81行的C:\ xampp \ htdocs \ psmtest1 \ parcelEdit.php

     

注意:未定义的变量:rcv_date in   第85行的C:\ xampp \ htdocs \ psmtest1 \ parcelEdit.php

     

注意:未定义的变量:pck_date in   第89行的C:\ xampp \ htdocs \ psmtest1 \ parcelEdit.php

     

注意:未定义的变量:状态   第93行的C:\ xampp \ htdocs \ psmtest1 \ parcelEdit.php

老实说,即使参考了不同的代码示例,我也找不到解决这个问题的方法。

2 个答案:

答案 0 :(得分:0)

尝试在else条件

之后添加if(isset($parcelID))
if(isset($_GET["parcelID"])){

    $parcelID = mysql_real_escape_string($_GET["parcelID"]);

    $result = mysql_query("SELECT * FROM parcel WHERE parcelID = '$parcelID'");

    while($res = mysql_fetch_array($result))
    {
        //$mem_id = $res['mem_id'];
        $parcelID = $res['parcelID'];
        $owner = $res['parcelOwner'];
        $rcv_date = $res['dateReceived'];
        $pck_date = $res['datePickup'];
        $status = $res['parcelStatus'];
    }

} else {

        $parcelID = '';
        $owner = '';
        $rcv_date = '';
        $pck_date = '';
        $status = '';

}
  • 我认为可能会减少太多isset()条件,因此我删除了第一个if(isset($_GET["parcelID"]))条件,然后直接用if(isset($parcelID))替换它。
  • 使用mysqli_*扩展名代替deprecated mysql_*

答案 1 :(得分:0)

您可以根据自己的想法检查变量是否即将到来。您可以在 var_dump() print_r()的帮助下转储使用POST方法发送的所有变量 -

<?php 
include('db.php');

if(isset($_POST['update']))
{    
     echo '<pre>';
     print_r($_POST);
     echo '</pre>';

     $parcelID = $_POST['parcelID'];
     $owner = $_POST['owner'];
     $rcv_date = $_POST['rcv_date'];
     $pck_date = $_POST['pck_date'];
     $status = $_POST['status'];
     ...

?>

您可以查看$ _POST中的并执行所需的更改。