也许我已经累了,也许我只是在犯这个错误,但不管怎么说都不对。
勾选复选框后,数据库会更新,但忽略$ id,只更新具有空pu_time列的第一条记录。应该发生的是当勾选复选框时,当前时间被插入到提供了id的相应记录中。我可以回复$ id,但它仍然被忽略。
我已成功使用文本框进行此工作....
$sql = "SELECT * FROM dispatch WHERE driver = '' OR pu_time = '' OR do_time = ''";
$result = mysql_query($sql);
$i = 0;
$color1 = "#2b2823";
$color2 = "#333333";
while($row = mysql_fetch_array($result)){
$id = $row['id'];
if($i%2 == 1) {$color = $color1;}
else {$color = $color2;}
echo "<tr style='background-color: $color'>";
echo '<td><input name="id" type="hidden" value="'.$row['id'].'" />'.$row['call_time'].'</td>';
echo "<td>".$row['start_address']."</td>";
echo "<td>".$row['end_address']."</td>";
echo "<td>".$row['phone']."</td>";
echo "<td>".$row['puat']."</td>";
if (isset($row['pu_time']) === true && empty($row['pu_time']) === true){
echo '<td>
<form id = "pu_time">
<input name="pick_up_time" type="checkbox" value="" onchange="this.form.submit()" />
</form></td>';
if (isset($_GET['pick_up_time'])){
$pick_up = date('g:ia');
$update_p = "UPDATE dispatch SET pu_time = '{$pick_up}' WHERE id = '{$id}'";
mysql_query($update_p,$con)
or die;
unset($_GET['pick_up_time']);
echo '<script type="text/JavaScript">
window.location.href = "new_index.php";
</script>';
}
}
else {
echo "<td>".$row['pu_time']."</td>";
}
答案 0 :(得分:1)
只需进行两项更改即可解决您的问题:
将行ID添加到隐藏字段中,然后提交:
<?php echo '
<td><form id = "pu_time">
<input name="pick_up_time" type="checkbox" value="" onchange="if(this.checked)this.form.submit()" />
<input name="rowId" type="hidden" value="'.$id.'" />
</form></td>';
当然,数据库更新部分将在循环之外,以避免不必要的数据库调用,因为我们正在重定向到新页面(或同一页面):
if (isset($_GET['pick_up_time']) && !empty($_GET['rowId'])){ // must have id;
$pick_up = date('g:ia');
$rowId = $_GET['rowId']; //mark this!
$update_p = "UPDATE dispatch SET pu_time = '{$pick_up}' WHERE id = '{$rowId}'";
mysql_query($update_p,$con)
or die;
unset($_GET['pick_up_time']);
echo '<script type="text/JavaScript">
window.location.href = "new_index.php";
</script>';
}
//and then write your code what is currently in the first line: $sql = "SELECT * FROM dispatch WHERE dr ...... and so;