gpt4 book ai didi

php - 如何修复php中 undefined variable

转载 作者:行者123 更新时间:2023-11-29 20:57:24 24 4
gpt4 key购买 nike

我正在尝试在连接 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>

我遇到了这些错误

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

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

最佳答案

你可以检查变量是否如你所想的那样到来。您可以借助 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 中的 key 并进行所需的更改。

关于php - 如何修复php中 undefined variable ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37519049/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com