In the form below I'm creating a loop but I need unique input name(s). The name "api_key" and name "delete_button" need to be something like "api_key_0" "api_key_1", etc. and delete_button_1, delete_button_2, etc. How do I get unique names in the loop? Second part: How do I get variables inside of the $_POST that are the correct input names created in the loop? I need to pass the correct variables names to the $_POST for each delete button and api key.
<?php // Delete API code.
if(isset($_POST['delete_button'])) {
$delete_api_key = $_POST['api_key'];
if ($mysqli->query("DELETE FROM api_keys WHERE api_key = '$delete_api_key'")) {
echo "API Key Successfully Deleted.";
}
else {
echo "Error:" . $mysqli->error;
}
}
?>
Form Part:
<?php
if ($api_cnt >= 1) {
$list_api_keys = $mysqli->query("SELECT api_key, user FROM api_keys WHERE user = 'testuser'");
?>
<form method= "post" action ="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<table>
<tr>
<th>API Keys</th>
</tr>
<?php
while($row = mysqli_fetch_row($list_api_keys)) {
?>
<tr>
<td><input name = "api_key" type = "hidden" value="<?php echo $row[0]; ?>"><?php echo $row[0]; ?></td>
<td><input type = "submit" name = "delete_button" value = "Delete"></td>
</tr>
<?php
}
?>
</form>
</table>
<?php
$mysqli->close();
}
?>