【发布时间】:2020-02-10 23:11:05
【问题描述】:
我对编码完全陌生。话虽如此,我希望你能帮助我解决我遇到的这个问题。 我使用 PHP 和 Mysqli 在 html 表中填充信息。我在其中一列中添加了一个按钮。我想单击按钮并更新数据库中该表行的信息。 用户赚取佣金,所以通过点击按钮,我想将“支付”列中的金额移动到“支付”列并更新数据库。
当我单击按钮时,我收到“成功”消息,显示“付款已成功处理”,但数据库中没有任何变化! 提前感谢您提供的任何帮助。 这是我的代码:
<html>
<body>
<div class="form">
<h2>View Records</h2>
<table width='100%'>
<thead>
<tr>
<th><strong>user ID</strong></th>
<th><strong>Fist Name</strong></th>
<th><strong>Last Name</strong></th>
<th><strong>pay commission</strong></th>
<th><strong>Paid</strong></th>
<th><strong>Pay Commission</strong></th>
<th><strong>Delete</strong></th>
</tr>
</thead>
<tbody>
<?php
$count=1;
include "db.php";
$sql = "SELECT * FROM users ";
$result = $conn-> query($sql);
if($result -> num_rows > 0){
while ($row = $result-> fetch_assoc()) { ?>
<td align="center"><?php echo $row["id"]; ?></td>
<td align="center"><?php echo $row["first_name"]; ?></td>
<td align="center"><?php echo $row["last_name"]; ?></td>
<td align="center"><?php echo $row["pay"]; ?></td>
<td align="center"><?php echo $row["paid"]; ?></td>
<td align="center">
<div class="input-group">
<form class="reset-form" action="members.php" method="post" >
<div class="input-group">
<button type="submit" class="button_1" name="pay-commission">Pay Commission</button></div></form>
</td>
<td align="center">
<a href="delete.php?id=<?php echo $row["id"]; ?>">Delete</a>
</td>
</tr>
<?php
}
// process commission*******
if(isset($_POST["pay-commission"])){
$id=$row['id'];
$pay=$row['pay'];
$paid=$row['last_paid'];
//date and time of transaction
$trn_date = date("Y-m-d H:i:s");
require('db.php');
$ins_query="update users last_paid='$pay' trm_date='$trn_date' where id= '$id'";
mysqli_query($conn,$ins_query);
if($ins_query){
echo "<p class= 'success'> Payment Processed Successfully <p>";
}else{
echo"<p class= 'error'>something went wrong!!</p>";
}
}
}
?>
</tbody>
</table>
</div>
</body>
</html>
【问题讨论】:
-
$ins_query="update users SET last_paid='$pay' trm_date='$trn_date' where id= '$id'";您忘记在查询中添加SET -
你也没有用表单发送任何数据,在这里你可以添加一些隐藏的输入框,并从你的数据库中设置值。
-
<form class="reset-form" action="members.php" method="post" > <div class="input-group"> <button type="submit" class="button_1" name="pay-commission">Pay Commission</button></div></form>你的表格是空的 -
$id=$row['id']; $pay=$row['pay']; $paid=$row['last_paid'];在if(isset($_POST["pay-commission"]内部毫无意义。 1)您实际上并不知道基于此单击了哪一行,并且您当然不想更新所有这些行。 2) 您 正在 更新行,但仅使用已经存在的完全相同的数据。正如 MUFAzmi 提示的那样,您需要 a) 表格行中的一些输入字段,而不仅仅是静态数据,以及 b) 在提交时通过 $_POST 捕获这些输入字段的值。 -
贴出的代码包含太多语法错误。在 cmets 中发布的内容不止于此。