【问题标题】:How do I make a button within an HTML table, to update info in my database如何在 HTML 表中创建一个按钮,以更新我的数据库中的信息
【发布时间】: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
  • 你也没有用表单发送任何数据,在这里你可以添加一些隐藏的输入框,并从你的数据库中设置值。
  • &lt;form class="reset-form" action="members.php" method="post" &gt; &lt;div class="input-group"&gt; &lt;button type="submit" class="button_1" name="pay-commission"&gt;Pay Commission&lt;/button&gt;&lt;/div&gt;&lt;/form&gt; 你的表格是空的
  • $id=$row['id']; $pay=$row['pay']; $paid=$row['last_paid']; if(isset($_POST["pay-commission"] 内部毫无意义。 1)您实际上并不知道基于此单击了哪一行,并且您当然不想更新所有这些行。 2) 您 正在 更新行,但仅使用已经存在的完全相同的数据。正如 MUFAzmi 提示的那样,您需要 a) 表格行中的一些输入字段,而不仅仅是静态数据,以及 b) 在提交时通过 $_POST 捕获这些输入字段的值。
  • 贴出的代码包含太多语法错误。在 cmets 中发布的内容不止于此。

标签: php html button mysqli


【解决方案1】:

让它正常工作,只是它只需单击“支付佣金”列中的任何按钮即可更新所有用户。一键处理所有佣金可能不是一件坏事。 也许我移动了表格下方的按钮,以便一键更新所有用户。所有这些代码都在一个名为 members.php 的页面上

 <html>
 <head></head> <header></header>

  <body>
  //table structure

<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>

     //populate table with user data

<?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["last_paid"]; ?></td>
    <td align="center">
    <div class="input-group">
    <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'];

    //set the date and time of transaction

$trn_date = date('Y-m-d');




$conn->query("UPDATE users SET  last_paid='$pay'  WHERE id= '$id'");
$conn->query("UPDATE users SET  pay=0.00 WHERE id= '$id'");
$conn->query("UPDATE users SET  trn_date='$trn_date' WHERE id= '$id'");


if($conn){
    //refresh the page to see changes
header ('Location: members.php');
echo '<p class="success"> A Payment of $$pay Processed Successfully for member ID     $id</p>';
    }else{
        echo '<p class="error">Something went wrong! </p>';
    }


   }
  }
 }  

$conn->close();
  ?>    

 </tbody>
 </table>
 </div>




 </body>
 </html>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多