0

I have two pages - 1. products | 2. edit-products.

I have a table on the products page with products listed. I then have an edit button next to each product. When I click on the edit button, it takes the user to the edit-products page. Then I have additional input fields that the user can edit the information.

My issue is I cannot get my UPDATE query to work. It says I have an undefined index for the variables that I am bringing the information over with, but I do not understand that because I use those variables within the input fields.

This is my products page and how I go to the edit page:

$stmt = $dbc->query("SELECT `id`,`first`,`last`,`product` FROM users");
        $stmt->setFetchMode(PDO::FETCH_ASSOC);

        while($row = $stmt->fetch()) {
        ?>
        <form method="POST" action="edit-product">
            <tr>
                <td><?php echo $row['id'];?>"</td>
                <td><?php echo $row['first'];?></td>
                <td><?php echo $row['last'];?></td>
                <td><?php echo $row['product'];?></td>
                <input name="id" type="hidden" value="<?php echo $row['id'];?>" readonly>
                <input name="first" type="hidden" value="<?php echo $row['first'];?>">
                <input name="last" type="hidden" value="<?php echo $row['last'];?>">
                <input name="product" type="hidden" value="<?php echo $row['product'];?>">
                <td><div class="delete-class" name="delete" id="<?php echo $row['id']; ?>">Delete</div></td>
                 <td><input name="edit" type="submit" value="Edit"></td> 
<!-- <td><a href="edit-product">Edit</a></td> -->
            </tr>
        </form>
        <?php } ?>
        </tbody>

Then on the edit page it is basically set up like this:

ini_set('display_errors', 1);
error_reporting(E_ALL);

$id = $_POST['id'];
$first = $_POST['first'];
$last = $_POST['last'];
$product = $_POST['product'];

$newId = filter_var($id, FILTER_SANITIZE_STRING);


try {
    $host = 'localhost';
    $name = '';
    $user = '';
    $password = '';

    $dbc = new PDO("mysql:host=$host;dbname=$name", $user, $password);

}catch(PDOException $e) {
    echo $e->getMessage();

}

if(isset($_POST['update'])) {
    $stmt = $dbc->prepare("UPDATE users SET first = ?, last = ?, product = ?, amount = ?, available = ? WHERE id = ?");

    $stmt->bindParam(1, $_POST['first']);
    $stmt->bindParam(2, $_POST['last']);
    $stmt->bindParam(3, $_POST['product']);
    $stmt->bindParam(4, $_POST['amount']);
    $stmt->bindParam(5, $_POST['available']);
    $stmt->bindParam(6, $newId);

    $stmt->execute();
}

    $stmt = $dbc->query("SELECT * FROM users WHERE id='$newId' ");
    $stmt->setFetchMode(PDO::FETCH_ASSOC);
    //print_r($stmt);
    while($row = $stmt->fetch()) {

?>

<form method="POST">
<input type="text" value="<?php echo $newId; ?>">
<input type="text" value="<?php echo $first; ?>">
<input type="text" value="<?php echo $last; ?>">
<input type="text" value="<?php echo $product; ?>">
<input type="text" value="<?php echo $row['amount']; ?>">
<input type="text" value="<?php echo $row['available']; ?>">

<button name="update" type="submit">Update Product Information</button>
</form>

When I hit the "Update" button to initiate the UPDATE query I get the invalid index errors for the variables I have at the top of this page. These:

$id = $_POST['id'];
$first = $_POST['first'];
$last = $_POST['last'];
$product = $_POST['product'];

If I am sending over the information correctly with those variables, why would I be getting an error saying they are undefined when I try to do this query?

Is there something else wrong in my code or placement issues?

UPDATED edit page

<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);

$id = $_POST['id'];
$first = $_POST['first'];
$last = $_POST['last'];
$product = $_POST['product'];

$newId = filter_var($id, FILTER_SANITIZE_STRING);


try {
    $host = 'localhost';
    $name = '';
    $user = '';
    $password = '';

    $dbc = new PDO("mysql:host=$host;dbname=$name", $user, $password);

}catch(PDOException $e) {
    echo $e->getMessage();

}

    $stmt = $dbc->query("SELECT * FROM users WHERE id='$newId' ");
    $stmt->setFetchMode(PDO::FETCH_ASSOC);
    //print_r($stmt);
    while($row = $stmt->fetch()) {

?>
<form name="update" method="POST">
<input type="text" value="<?php echo $newId; ?>">
<input type="text" value="<?php echo $first; ?>">
<input type="text" value="<?php echo $last; ?>">
<input type="text" value="<?php echo $product; ?>">
<input type="text" value="<?php echo $row['amount']; ?>">
<input type="text" value="<?php echo $row['available']; ?>">

<button type="submit">Update Product Information</button>
</form>
<?php 
}


if(isset($_POST['update'])) {
    $stmt = $dbc->prepare("UPDATE users SET first = ?, last = ?, product = ?, amount = ?, available = ? WHERE id = ?");

    $stmt->bindParam(1, $_POST['first']);
    $stmt->bindParam(2, $_POST['last']);
    $stmt->bindParam(3, $_POST['product']);
    $stmt->bindParam(4, $row['amount']);
    $stmt->bindParam(5, $row['available']);
    $stmt->bindParam(6, $newId);

    $stmt->execute();
}
?>
Becky
  • 2,283
  • 2
  • 23
  • 50
  • use a conditional `!empty()` or a ternary operator. – Funk Forty Niner Apr 08 '16 at 15:24
  • In my if statement or where? – Becky Apr 08 '16 at 15:25
  • @Jay Blanchard So, if I changed the variable names, so they aren't the same as the other page, would that work? – Becky Apr 08 '16 at 15:27
  • No, that shouldn't matter. Do yourself a favor, simply place a `print_r($_POST)` or `var_dump($_POST)` in the PHP page which receives the form submission. – Jay Blanchard Apr 08 '16 at 15:29
  • I get the correct information. I also echo out that information within the inputs. – Becky Apr 08 '16 at 15:30
  • You only set 4 variables and try to use some other POST variables that are not set: `$stmt->bindParam(4, $_POST['amount']); $stmt->bindParam(5, $_POST['available']);` Your error message probably reflects this very specifically. – Jay Blanchard Apr 08 '16 at 15:34
  • Would that be causing the index errors for the variables above it? Would it be best to move my if isset statement lower in the page (after the loop), create a variable for the amount and available database rows? – Becky Apr 08 '16 at 15:36
  • You have no name attributes for your second form, so none of variables will be set when you click `update`. Part of the problem appears to be that your logic flow seems to be a little convoluted. It took me a while to figure out where you were coming from and going to. – Jay Blanchard Apr 08 '16 at 15:39
  • @JayBlanchard I just updated my question with the current code. I am still getting the index errors. – Becky Apr 08 '16 at 15:46
  • You didn't update the inputs with names. The form doesn't need one. – Jay Blanchard Apr 08 '16 at 15:48
  • Ok, I added the names, but the query is not working as nothing is saving when the page refreshes. – Becky Apr 08 '16 at 15:51
  • What does `var_dump($_POST)` show after you made the changes? – Jay Blanchard Apr 08 '16 at 16:01
  • Don't mind the stupid names: `array(5) { ["id"]=> string(1) "4" ["first"]=> string(3) "Tom" ["last"]=> string(5) "Gi jo" ["product"]=> string(3) "Tea" ["edit"]=> string(4) "Edit" }` I put the dump right after the closing while loop bracket after the form. – Becky Apr 08 '16 at 16:04
  • It seems like it is picking up what I sent via my edit button on the previous page. – Becky Apr 08 '16 at 16:06
  • So amount and available still do not get set, and are therefore undefined. – Jay Blanchard Apr 08 '16 at 17:08
  • @JayBlanchard I don't get how they wouldn't be defined, though? – Becky Apr 08 '16 at 17:45
  • Without being able to test further I cannot tell why either. They are not in your `var_dump()` though, which means they aren't set. – Jay Blanchard Apr 08 '16 at 17:47
  • How would I test them? Sorry for the newb questions. I haven't worked with PHP in a long time, plus I am trying to learn PDO at the same time, so my code is current. – Becky Apr 08 '16 at 17:49
  • I'd create a simple form, fill it out and then submit it. Check what is being submitted by using `var_dump()`. then start adding other code. – Jay Blanchard Apr 08 '16 at 17:57
  • I mean aren't I doing that right now?> – Becky Apr 08 '16 at 17:59

0 Answers0