Reputation: 2610
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.
Upvotes: 0
Views: 1642
Reputation: 1766
You need to use separate form for each record.
While ($row=mysql_fetch_array($result)){
<form method="post" .....>
.....
<select>
<option value="'.$value.'">title</option>
</select>
.....
</form>
}
Upvotes: 1
Reputation: 9299
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
<td><input type='submit' name='delete' value='delete' /></td>
to
<td><input type='checkbox' name='ids_to_delete[]' value='<?php echo $row['id']; ?>' /></td>
Then add a new Delete
button at the end of the table
<input type='submit' name='delete' value='Delete selected rows' />
Change your PHP to handle an array of values now
if (isset($_POST['ids_to_delete']) && count($_POST['ids_to_delete']) > 0) {
foreach($_POST['ids_to_delete'] as $alarmid) {
delete_alarm_from_database($alarmid)
}
}
or even better - change your delete_alarm_from_database
function to handle an array and delete them all at once.
Upvotes: 1
Reputation: 8578
Add an id for each button. And call it with that id, probably the best shout being using jQuery. This way you will know the exact No. of the button :)
Take a look at this - it is kind of the same problem - PHP - making a switch statement using id from form input
Upvotes: 1