0

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.

Thamilhan
  • 13,040
  • 5
  • 37
  • 59

2 Answers2

0

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_*.
Logan Wayne
  • 6,001
  • 16
  • 31
  • 49
  • I've tried your way, and it works. But the problem now is, the data that are supposed to be on the textfields (extracted from the database) didn't show. How can i fix that? – Aini Ramlan May 30 '16 at 09:01
  • @AiniRamlan - are you sure there is a `GET` request in your URL? If it does, have you tried running the query in your `phpMyAdmin` with the same parameter? – Logan Wayne May 31 '16 at 01:18
0

You can check that whether the variable is coming as you thinks or not . You can dump all variables sent using POST method with the help of var_dump() or print_r() like this -

<?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'];
     ...

?>

and you can check the key in $_POST and do the required changes .

Deepak Dixit
  • 1,510
  • 15
  • 24