douheng8629 2016-05-30 06:33 采纳率: 100%
浏览 48

如何修复php中的未定义变量[重复]

I'm trying to do the update function in php with a mysql database connected. I put the codes for update in a file called parcelEdit.php. Here's my code for 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>

And i got these errors

Notice: Undefined variable: parcelID in C:\xampp\htdocs\psmtest1\parcelEdit.php on line 77

Notice: Undefined variable: owner in C:\xampp\htdocs\psmtest1\parcelEdit.php on line 81

Notice: Undefined variable: rcv_date in C:\xampp\htdocs\psmtest1\parcelEdit.php on line 85

Notice: Undefined variable: pck_date in C:\xampp\htdocs\psmtest1\parcelEdit.php on line 89

Notice: Undefined variable: status in C:\xampp\htdocs\psmtest1\parcelEdit.php on line 93

I honestly can't find ways to solve this even after referring to different code examples.

</div>
  • 写回答

2条回答 默认 最新

  • douchao5864 2016-05-30 06:39
    关注

    Try adding an else after your if(isset($parcelID)) condition

    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 = '';
    
    }
    
    • Too many isset() condition which I think could be lessen, so I removed the first if(isset($_GET["parcelID"])) condition and just go straight and replace the if(isset($parcelID)) with it.
    • Use mysqli_* extension instead of deprecated mysql_*.
    评论

报告相同问题?

悬赏问题

  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据