earlier i posted a question about getting a single constant value from a loop and use it outside of the loop (reference : Question).
now i’m facing another problem with another while loop.
it goes like this:
i have a query:
$sql = "
SELECT alarms.id, alarms.date, clients.name, clients.number, alarms.controller,
alarmstype.type, alarms.alarmstatus, alarms.starttime, alarms.endtime
FROM alarms
INNER JOIN clients ON alarms.clientid = clients.id
INNER JOIN alarmstype ON alarms.typeid = alarmstype.id
WHERE alarms.alarmstatus = 'ON' ORDER BY alarms.date ASC";
$result = mysql_query($sql);
and for each row i have assinged a button in the while loop:
while ($row = mysql_fetch_array($result)) {
print "<tr>
<td>" . $row['date'] . "</td>
<td>" . $row['number'] . " | " . $row['name'] . "</td>
<td>" . $row['controller'] . "</td>
<td>" . $row['type'] . "</td>
<td style='color: red'>
<select name = 'statusupdate' style='font-size:10px;'>
<option> " . $row['alarmstatus'] . " </option>
<option> OFF </option>
</select>
</td>
<td>" . $row['starttime'] . "</td>
<td>" . $row['endtime'] . "</td>
<td><input type='submit' name='delete' value='delete' /></td>
so far so good. The rows are displayed fine and the button’s there.
(all of the while loop is in a form tag so inputs are cool.
any way.
i want to make something like this:
}
if (isset($_POST['delete'])) {
delete_alarm_from_database($alarmid)
}
and the function goes like this:
function delete_alarm_from_database($id) {
//find alarm by id and delete:
$sql = "SELECT * FROM alarms WHERE id = " . $id;
$result = mysql_result($sql);
if (!$result) {
print "couldn't find record.";
}
//delete record from database:
else {
$sql = "DELETE FROM alarms WHERE id = " . $id;
$result = mysql_result($sql);
}
}
my problem goes like this:
each row has an id but setting it as a variable will always overwrite until he puts in the variable the last id the loop reached. (meaning every delete button of each row will simply delete the last id each page load)
the question is like this. how can i make each delete button recognize it’s row id?
thanks in advance – Cheers.
I suggest changing your process up a bit. Change the delete buttons to check boxes and allow the user to delete more than one at a time.
Change
to
Then add a new
Deletebutton at the end of the tableChange your PHP to handle an array of values now
or even better – change your
delete_alarm_from_databasefunction to handle an array and delete them all at once.