-3

This issue must have been asked a thousand times, but here's mine: I want to update an entry from another page, so I passed the id via the URL. The problem is: when I go and hit "edit" a new page will come up with the Undefined variable: row in lines 93, 96 and 99 (it's the

<form action="" class="w3-container" method="post">
    <input type="hidden" name="id" value="<?php echo $id;?>">
    <p>
    <input class="w3-input" type="text" name="itemno" value="<?php print $row['item_no'];?>"required>
    <label class="w3-label w3-validate">Item Number</label></p>
    <p>
    <input class="w3-input" type="text" name="itemname" value="<?php print $row['item_name'];?>"required>
    <label class="w3-label w3-validate">Item Name</label></p>
    <p>
    <input class="w3-input" type="text" name="desc" value="<?php print $row['item_desc'];?>" required>
    <label class="w3-label w3-validate">Description</label></p>

    <p>
    <button class="w3-btn w3-blue-grey w3-hover-teal" name="update">Update</button>
    <button class="w3-btn w3-blue-grey w3-hover-light-blue" name="reset" type="reset">Clear fields</button></p>
    </form>

<?php $id = $_GET['id']; ?>

BTW, here's what I use when getting the ID ^^^^

  • 3
    Possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – Epodax Aug 16 '16 at 09:32
  • 1
    It's because you are trying to fetch the ID via `$_GET` when you post it with `$_POST`. – Epodax Aug 16 '16 at 09:32
  • Where are you defining `$row`? – Styphon Aug 16 '16 at 09:33
  • 1
    What have you tried? What errors did you get? If no errors displayed please post your PHP error Log. Thank you – Mark Twigg Aug 16 '16 at 09:33

2 Answers2

0
<form action="" class="w3-container" method="post">
    <input type="hidden" name="id" value="<?php echo $id;?>">
    <p></p>
    .
    .
</form>

Since, method='POST' used.

Change

$id = $_GET['id'];

To

$id = $_POST['id'];

Nana Partykar
  • 10,556
  • 10
  • 48
  • 77
0

I found a turn around. After posting this. Sorry to bother you guys, but if you have any suggestions, I'd gladly accept it! Thanks!!!

The code I added, didn't do the $_POST yet:

<?php
$id = $_GET['id'];

$result = mysqli_query($conn,"SELECT * FROM tbl_items WHERE item_id='$id'");
if (!$result) {
echo 'Could not run query: ' . mysql_error();
exit;
}
$row = mysqli_fetch_row($result);
?>

The ones with $row which I modified from $row['item_no'] to $row[1] is in the form.

<p>
    <input class="w3-input" type="text" name="itemno" value="<?php print $row[1];?>"required>
    <label class="w3-label w3-validate">Item Number</label></p>