如何使用该列上的输入字段更新一列

时间:2017-06-21 18:11:20

标签: php mysql html5 twitter-bootstrap

我有这个查询

$sql = ("SELECT * FROM reception WHERE date = '$date'");
  $result = $conn->query($sql); 

然后在表格中显示结果。

<?php while ($row=$result->fetch_assoc()) { 
    $id = $row['id']; ?>

<tr> 
<td ><?php echo  $id; ?></td>
<td>   
    <form method="post" action="que.php">
    <input type="text" name="bill" placeholder="amount in pula" value="<?php echo  $row['bill']; ?>

        <input type="submit" class="btn btn-info" value="UPDATE" name="update_bill" >   

        <?php   if( isset($_POST['update_bill']) ){

$bill = mysqli_real_escape_string($conn, $_POST['bill']);
$sql =("UPDATE reception SET bill = '$bill' WHERE id = '$id' ");
$result = $conn->query($sql);




}
        ?>
    </div>
    </div>
</td>
</form>
</tr>

                      <?php }  ?> 


</table>

所以在那张桌子上我有一个带有输入字段的表单,在完成一次遭遇后会更新,但无论我选择哪条记录,它只更新第一条记录,有什么方法可以更新我的查询或代码能够更新我选择的记录吗?

1 个答案:

答案 0 :(得分:0)

$sql = ("SELECT * FROM reception WHERE date = '$date'");
$result = $conn->query($sql); 
    <?php while ($row=$result->fetch_assoc()) { 
    $id = $row['id']; ?>

<tr> 
<td ><?php echo $id; ?></td>
<td>   
    <form method="post" action="que.php">
    <input type="text" name="bill" placeholder="amount in pula" value="<?php echo  $row['bill']; ?>
          <input type="hidden" value="<?php echo $id; ?>" name="id" >
        <input type="submit" class="btn btn-info" value="UPDATE" name="update_bill" >
    </div>
    </div>
</td>
</form>
</tr>
<?php }  ?> 
</table>

<强> que.php

<?php   if( isset($_POST['update_bill']) ){
    $id = $_POST['id'];
    $bill = mysqli_real_escape_string($conn, $_POST['bill']);
    $sql =("UPDATE reception SET bill = '$bill' WHERE id = '$id' ");
    $result = $conn->query($sql);
}
?>

您应该在que.php中添加连接以使用该代码。

这是解决方案希望它能帮到你