早些时候我发布了一个关于从循环中获取单个常量值并在循环之外使用它的问题(参考:问题)。现在我面临另一个while循环的问题。它是这样的:我有一个查询:
$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);
并且对于每一行,我都在 while 循环中分配了一个按钮:
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>
到目前为止,一切都很好。行显示正常,按钮在那里。(所有的while循环都在一个表单标签中,所以输入很酷。无论如何。我想做这样的事情:
}
if (isset($_POST['delete'])) {
delete_alarm_from_database($alarmid)
}
函数是这样的:
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);
}
}
我的问题是这样的:每一行都有一个 id 但将其设置为变量将始终覆盖,直到他将循环到达的最后一个 id 放入变量中。(意味着每行的每个删除按钮都会简单地删除每个页面加载的最后一个 id)
问题是这样的。如何让每个删除按钮识别它的行 ID?
在此先感谢 - 干杯。