0

I am trying to create a to-do list for practice. When I look through the results I get back from the database, I see all of the results in order with a complete button and delete anchor tag. That part works fine but whenever I click on the complete button I only update the third row. Even if I try to click complete on the second or first row only the third row gets updated.

I tried using get_result instead of bind_result, I tried using anchor tags, I tried using fetch_assoc() instead of fetch() and I tried changing my query multiple times but nothing helped. I've been searching answers on here since last night and still nothing. Any help please ?

$addItem = $_POST['add_item'] ?? '';
$userLoggedIn = $_SESSION['session_user_id'];

        $counter = 1;

    echo $userLoggedIn . "'s list<br><br>";

    $user_query = $con->prepare('SELECT item_id, username, item_name FROM lists WHERE username = ? 
        ORDER BY item_id ASC');
    $user_query->bind_param("s", $userLoggedIn);
    $user_query->execute();
    /* Store the result (to get properties) */
    $user_query->store_result();
    /* Get the number of rows */
    $num_of_rows = $user_query->num_rows;
    /* Bind the result to variables */
    $user_query->bind_result($itemId, $username, $itemAdded);

    /*while ($row = $data_query_result->fetch_assoc()) {

        $itemId = $row['item_id'];
        $username = $row['username'];
        $itemAdded = $row['item_name'];
    } */

    while ($user_query->fetch()) {
        echo "<p>" . $counter . ". " . $itemAdded . $itemId .

            "<form action='index.php' method='POST'><button type='submit' 
            name='completed_button' value=". $itemId .">" .
            $itemAdded . " </button></form>" .

            " <a href='#'>Delete</a>" . "</p>";

        $counter++;
    }

    $user_query->free_result();
    $user_query->close();

if (isset($_POST['add_item_button'])) {

    $stmt = $con->prepare("INSERT INTO lists (username, item_name) VALUES (?, ?)");
    $stmt->bind_param("ss", $username, $addItem);
    $stmt->execute();
}

if (isset($_POST['completed_button'])) {

    $user_data_query = $con->prepare('UPDATE lists SET completed = "1" WHERE item_id = ?');
    $user_data_query->bind_param("i", $_POST['item_id']);
    $user_data_query->execute();

    //$updateCompleted = "UPDATE lists SET completed = '1' WHERE item_id = " . $itemId . " ";
    //$query2 = $mysql->query($updateCompleted);
}
sir toby
  • 11
  • 4
  • When you check the generated HTML, are all the values properly assigned to buttons? – El_Vanja Feb 23 '21 at 17:19
  • @El_Vanja Yeah they're all correct. The name of the items, the id everything – sir toby Feb 23 '21 at 17:26
  • Ah, I see it now. `$user_data_query->bind_param("i", $itemId);` - here you use `$itemId`, which was last assigned in the final iteration of the fetching loop. You need to use the value from the `$_POST` instead. – El_Vanja Feb 23 '21 at 17:29
  • @El_Vanja no that still didn't help. I also change the name of the variable because two of them were the same – sir toby Feb 23 '21 at 17:33
  • What exactly have you altered? Please edit the question to reflect any changes you've made. – El_Vanja Feb 23 '21 at 17:35
  • @El_Vanja I've just edited the question. – sir toby Feb 23 '21 at 17:38
  • You have no form element with the name `item_id`, so you can't look for `$_POST['item_id']`. Your value will be under `$_POST['completed_button']`, because that's what your button is named. You should be seeing an "Undefined index" error after the change. If you don't, consider turning on [error reporting](https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display). – El_Vanja Feb 23 '21 at 17:41
  • Ok it works now. Thank you. Will you put it as an answer ? – sir toby Feb 23 '21 at 17:45
  • I'll pass. The problems were basics, so there are bound to be duplicates that cover them. – El_Vanja Feb 23 '21 at 18:51

0 Answers0