I am working on a PHP form that loads data from a database, and for each record, there is a select box that allows users to change the record's status (Accepted, Rejected, Pending). When a user changes the status, it should update the corresponding record's status in the database.
The problem I am facing is that the select box works perfectly for all records, except for the first one displayed in the table. When I try to change the status of the first record, nothing happens. The form loads five rows, and this issue only occurs with the first record.
Here is the PHP code for the form:
<?php
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
$stav = htmlspecialchars($row['stav']);
echo "<tr data-stav='$stav'>";
echo "<td><input type='checkbox' name='selected_ids[]' value='" . $row['id'] . "'></td>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $stav . "</td>";
echo "<td>" . $row['datum_vlozeni'] . "</td>";
echo "<td>" . $row['pocet_stojanu'] . "</td>";
echo '<td>
<form method="post" action="php/change_stav.php">'; // Vytvoříme formulář pro změnu stavu
echo '<input type="hidden" name="id" value="' . $row['id'] . '">'; // Skryté pole s ID záznamu
echo '<select name="stav" onchange="this.form.submit()">'; // Dropdown se jménem "stav"
echo '<option value="odesláno" ' . ($stav === 'odesláno' ? 'selected' : '') . '>Odesláno</option>';
echo '<option value="přijato" ' . ($stav === 'přijato' ? 'selected' : '') . '>Přijato</option>';
echo '<option value="odmítnuto" ' . ($stav === 'odmítnuto' ? 'selected' : '') . '>Odmítnuto</option>';
echo '<option value="Předáno BKOM" ' . ($stav === 'Předáno BKOM' ? 'selected' : '') . '>Předáno BKOM</option>';
echo '</select>';
echo '</form>';
echo '</td>';
echo '<td>
<a href="update.php?hash_kod=' . $row['hash_kod'] . '" title="Upravit">
<i class="bi bi-pencil-square"></i>
</a>
<a href="php/export_pdf.php?hash_kod='. $row['hash_kod'] .'" target="_blank" title="Export do PDF">
<i class="bi bi-file-earmark-pdf"></i>
</a>
</td>';
echo "</tr>";
}
} else {
echo "<tr><td colspan='6'>Žádné záznamy k zobrazení.</td></tr>";
}
?>
And here is the PHP code that processes the form action:
<?php
if ($_SERVER["REQUEST_METHOD"] === "POST") {
// připojení k databázi
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "podnety";
$conn = new mysqli($servername, $username, $password, $dbname);
$conn->set_charset("utf8");
// Ochrana proti SQL injection
$id = mysqli_real_escape_string($conn, $_POST['id']);
$stav = mysqli_real_escape_string($conn, $_POST['stav']);
// Aktualizace stavu v databázi
$sql = "UPDATE podnety SET stav = '$stav' WHERE id = '$id' AND delete_record IS NULL";
if (mysqli_query($conn, $sql)) {
header("Location: ../admin.php"); // Přesměrování zpět na hlavní stránku
exit;
} else {
echo "Chyba: " . mysqli_error($conn);
}
}
?>
I don't understand why the select box behaves differently for the first record compared to others. Can someone please help me identify the issue and provide a solution? Thank you!
I have thoroughly reviewed my code, but I cannot pinpoint the reason why the select box behaves differently for the first record. I have also searched for similar problems but haven't found a suitable solution yet. Any help or insights into this matter would be greatly appreciated. Thank you!