【问题标题】:How can i update html table row which display sql data when click button in each row?单击每行中的按钮时,如何更新显示 sql 数据的 html 表行?
【发布时间】:2019-07-30 04:48:47
【问题描述】:

当我点击问题按钮允许点击第一行按钮。第二行按钮不起作用。 所以我想更新我想要的表格行。

这是界面 - 第一行仅在单击第二行时更新,不允许单击问题按钮。

Interface Table

这里是 itemdisplay.php 带有 ajax 脚本的页面。

<?php
include('../db_connector.php');

$req     = $_GET['cat1'];

$query = "select i.item_name ,r.qty, i.item_id , r.reqID, r.av from items i
JOIN req_items r on r.item_name = i.item_id
JOIN req rq on r.req_number = rq.req_number
WHERE i.status = 'common' AND 
rq.req_number = $req and r.status = 'approved' 
GROUP BY i.item_name ";

$result = mysqli_query($con,$query);

?> 

<table class="table table-hover" >
<thead>
<tr>
<th>#</th>
<th>Item Name</th>
<th>Quantity</th>
<th>Stock Available </th>


</tr>
</thead>
<tbody>
<?php
@$count = mysqli_num_rows($result);
$itemCount = 1;
if($count > 0) {
while ($row = mysqli_fetch_array($result)) {

$name = $row['item_name'];
$qty = $row['qty'];
$id = $row['item_id'];
$rqId = $row['reqID'];
$av = $row['av'];


$query1 = "select m.stock_level 
from main_stock m 
where m.item_id = $id and m.depot_id = 27";

$result1 = mysqli_query($con, $query1);

@$count = mysqli_num_rows($result1);
if ($count > 0) {
while ($row = mysqli_fetch_array($result1)) {

$level = $row['stock_level'];

?>

<tr id="mydiv">

<td><?php echo $itemCount; ?></td>
<td hidden><input class="form-control" name="rqId" 
type="text" id="rqId" readonly value="<?php echo $rqId; ?>"></td>
<td><input class="form-control" name="name" type="text" 
id="name" readonly value="<?php echo $name; ?>"></td>
<td><input class="form-control" name="qty" type="text" 
id="qty" readonly value="<?php echo $qty; ?>"></td>
<td><input class="form-control" name="level" type="text" 
id="level" readonly value="<?php echo $level; ?>"></td>
<td hidden><?php echo $av; ?></td>


<?php
if($level < $qty) {

if ($av != 1) {

echo 'Do not exists the available stock !!';
?>
<td><button class="btn btn-primary" type="submit" 
id="request" name="request">Request</button></td>


<?php

} else
{

?>

<td><p>Processing</p></td>


<?php
}

?>
<?php

} else
{


?>

<td><button class="btn btn-primary" type="submit" 
id="issue" name="issue">Issue</button></td>
<?php

}

?>


</tr>
<?php
$itemCount++;
}
}
}
}
else{
echo 'No records found';
}


?>
</tbody>
</table>

<script>



$('#issue').click(function(){

$.ajax({
url:"./ajax/cIssue.php",
method:"POST",
data:$('#rqId').serialize(),
success:function(data)
{
$("#mydiv").load(location.href + " #mydiv");
}
});
});
</script>

这里是cIssue.php查询文件

<?php
include('../db_connector.php');

$id = $_POST["rqId"];

$query = "";

$query = "UPDATE `req_items`
SET `status` = 'issue' 
WHERE `reqID`=$id";
$result = mysqli_query($con, $query);
echo "Item Issued !";

$r = "UPDATE `main_stock`
SET `stock_level` = `stock_level` - (select `qty` 
from `req_items`  
where  `reqID`= $id )  
WHERE `depot_id`='27'  and `item_id`= (select `item_name`
from `req_items` 
where  `reqID`= $id ) ";

$l = mysqli_query($con, $r);




?>

我想在单击问题按钮时更新我想要的任何行。如果您想了解有关我的代码的更多详细信息,我可以提供。

【问题讨论】:

  • 您似乎正在构建一个数据网格。这种东西肯定有一千个教程。
  • @Strawberry 如果您可以提供解决此问题的链接。
  • 使用 id 只会给你第一个按钮点击。因为 id 只能识别一次。如果有帮助,请在下面查看我的答案。
  • @Ricky 当我随机单击第二行问题按钮时,它将更新第一行数据(不是第二行)。这是什么原因?
  • 您需要在 ajax.rqID 中传递特定的行 ID,因为每一行都应该不同,以便单击的每一行都有自己的唯一 ID,您可以通过 ajax@LahiruMendis 传递该 ID

标签: php mysql ajax sql-update row


【解决方案1】:

您好,您正在使用元素的 id 进行点击,这就是它只识别第一个问题按钮的原因。请使用类代替问题按钮的点击事件。

$(".myIssuebtn").on("click",function(){
//your ajax call here.

});
<button class="btn btn-primary" class="myIssuebtn" type="submit" 
id="issue" name="issue">Issue</button>

【讨论】:

  • 当我随机点击第二行问题按钮时,它将更新第一行数据(不是第二行)。这是什么原因?
  • 您需要为每一行传递 rqId 而不仅仅是一个 rqid。尝试使用 $(this).id @LahiruMendis
猜你喜欢
  • 1970-01-01
  • 2013-11-21
  • 2022-11-23
  • 1970-01-01
  • 2018-01-17
  • 1970-01-01
  • 2012-07-30
  • 2018-06-14
  • 1970-01-01
相关资源
最近更新 更多