我有一个表从数据库中提取信息,每行的最后一列是一个标记为COMPLETE的输入标记,用户可以单击该标记。我希望能够点击标签并让它更新我的数据库以更改一个布尔字段,该字段将COMPLETE从0更改为1.现在,如果我按顺序点击它们,我似乎无法更新行。所以如果我点击更新第一行,然后点击下面的几行,它将更新第二行。如果您单击该行的按钮两次,它将更新您想要的任何行,但首先它会更新您之前离开的行...
<form action='' method='POST'>
<?php
$current_date = date("Y-m-d");
$n = 1;
$sql = "SELECT id, date, pname, details, time, tech FROM task WHERE task='LAB' AND complete=0 ORDER BY date DESC";
$result = $link->query($sql);
echo $current_date;
echo "<table>
<tr>
<th>Date</th>
<th>Project</th>
<th>Details</th>
<th>Start Time</th>
<th>Technician</th>
</tr>";
while ($row = $result->fetch_assoc()) {
echo "<tr align='center'><td>" .$row["date"]. "</td><td>" .$row["pname"]. "</td><td>"
.$row["details"]. "</td><td>" .$row["time"]. "</td><td>" .$row["tech"]. "</td>
<td><input type='submit' name=".$n." value='Complete' /></td></tr>";
if (isset($_POST[$n])) {
$id = $row["id"];
$update = "UPDATE task SET complete = 1 WHERE task='LAB' AND id = ".$id."";
echo $update;
$update_result = $link->query($update);
}
$n++;
}
echo "</table>";
$link->close();
?>
</form>
答案 0 :(得分:2)
我注意到的一件事是你没有做好concatenation。
您可以尝试为每一行添加form
,然后使用隐藏的输入字段存储id
:
while ($row = $result->fetch_assoc()) {
echo '<tr align="center">
<td>'.$row["date"].'</td>
<td>'.$row["pname"].'</td>
<td>'.$row["details"].'</td>
<td>'.$row["time"].'</td>
<td>'.$row["tech"].'</td>
<td>
<form action="" method="POST">
<input type="hidden" name="id" value="'.$row["id"].'">
<input type="submit" name="complete" value="Complete"/>
</form>
</td>
</tr>';
}
然后您可以处理表单:
if(isset($_POST["complete"])) {
$id = $_POST["id"];
$stmt = $link->prepare("UPDATE task SET complete = 1 WHERE task='LAB' AND id = ?");
$stmt->bind_param("i", $id);
$stmt->execute();
$stmt->close();
}
您可以尝试使用Ajax进行无缝交易:
while ($row = $result->fetch_assoc()) {
echo '<tr align="center">
<td>'.$row["date"].'</td>
<td>'.$row["pname"].'</td>
<td>'.$row["details"].'</td>
<td>'.$row["time"].'</td>
<td>'.$row["tech"].'</td>
<td>
<a href="#" class="btn" type="button" data-artid="'.$row["id"].'">Complete</a>
</td>
</tr>';
}
然后创建你的脚本(我将在这个例子中使用jQuery)。您可以将其包含在主文件的底部:
<script>
$(document).ready(function(){ /* PREPARE THE SCRIPT */
$(".btn").click(function(){ /* WHEN COMPLETE BUTTON IS CLICKED */
var elem = $(this),
id = elem.attr('data-artid'); /* GET THE ID OF THE CLICKED ELEMENT */
$.ajax({ /* PROCESS AJAX */
url: "process.php", /* THE FILE WHERE THE FORM WILL BE PROCESSED */
type: "POST", /* METHOD TO BE USED */
data: {"id":id}, /* THE DATA TO BE PASSED TO process.php */
dataType: 'json', /* TYPE OF DATA TO BE RETURNED */
success: function(result){ /* IF process.php IS SUCCESSFUL */
if(result.boolean){
elem.closest('tr').remove(); /* REMOVE THE ENTIRE ROW */
}
}
});
return false; /* THIS WILL PREVENT FROM SCROLLING TO THE TOP OF THE PAGE */
});
});
</script>
然后处理process.php
中的表单(单独的文件):
/** INCLUDE HERE SOMEWHERE YOUR DATABASE CONNECTION **/
$boolean = false; /** DEFAULT VALUE TO BE RETURNED **/
if(!empty($_POST["id"])){
$id = $_POST["id"];
$stmt = $link->prepare("UPDATE task SET complete = 1 WHERE task='LAB' AND id = ?");
$stmt->bind_param("i", $id);
$stmt->execute();
$stmt->close();
$boolean = true;
}
echo json_encode(array('boolean' => $boolean)); /* THIS WILL BE RETURNED TO THE AJAX REQUEST */