尝试通过submit按钮更新列值

nhaq1z21  于 2021-06-21  发布在  Mysql
关注(0)|答案(3)|浏览(346)

我想更新一下 status 通过按“批准/拒绝”按钮在“我的叶”表上。以下是我的表架构:

到目前为止,我已经做到了:

  1. <?php
  2. if(isset($_POST['approved']))
  3. {
  4. echo "Approved";
  5. $status=$_POST['approved'];
  6. }
  7. if(isset($_POST['rejected']))
  8. {
  9. echo "Rejected";
  10. $status=$_POST['rejected'];
  11. }
  12. ?>
  13. <!-- Begin page content -->
  14. <div class="container">
  15. <div class="page-header">
  16. <h3>Employee Leaves</h3>
  17. <div class="table-responsive">
  18. <table class="table">
  19. <tr>
  20. <th>Employee Name</th>
  21. <th>Phone</th>
  22. <th>Email</th>
  23. <th>From</th>
  24. <th>To</th>
  25. <th>Reason</th>
  26. <th>Status</th>
  27. <th>---</th>
  28. </tr>
  29. <?php
  30. include ('database.php');
  31. $result = $database->prepare ("SELECT * FROM leaves order by id DESC");
  32. $result ->execute();
  33. for ($count=0; $row_message = $result ->fetch(); $count++){
  34. ?>
  35. <tr>
  36. <td><?php echo $row_message['full_name']; ?></td>
  37. <td><?php echo $row_message['phone']; ?></td>
  38. <td><?php echo $row_message['email']; ?></td>
  39. <td><?php echo $row_message['fromdate']; ?></td>
  40. <td><?php echo $row_message['todate']; ?></td>
  41. <td><?php echo $row_message['reason']; ?></td>
  42. <td><?php echo $row_message['status']; ?></td>
  43. <td>
  44. <form method="post" action="update.php">
  45. <input type="hidden" value="<?php echo $row_message['id']; ?>" />
  46. <input type="submit" value="Approved" name="approved"></input>
  47. <input type="submit" value="Rejected" name="rejected"></input>
  48. </form>
  49. </td>
  50. </tr>
  51. <?php } ?>
  52. </table>
  53. <a href="home"><button type="button" class="btn btn-primary"><i class="glyphicon glyphicon-arrow-left"></i> Back</button></a>
  54. </div>
  55. </div>
  56. </div>
  57. </div>
  58. </div>

这是我的update.php

  1. <?php
  2. $con = mysqli_connect('localhost', 'root', '');
  3. mysqli_select_db($con, 'leaves');
  4. $sql = "UPDATE leaves SET status = :status WHERE id = :id";
  5. if(mysqli_query($con, $sql))
  6. header("refresh:1; url=view-leave.php");
  7. else
  8. echo "Error";
  9. ?>

当我点击approve/reject时,我得到了“error”。我想问题出在 update.php . 我不知道是什么,也不知道在哪里。

5jvtdoz2

5jvtdoz21#

必须在此处写入数据库名称:

  1. mysqli_select_db($con, 'dbname');
9jyewag0

9jyewag02#

您是否尝试为 $msg=''; 以及 $status = ''; ?

zpjtge22

zpjtge223#

试试这个: update.php ```

  1. 以及 `view-leave.php` ```
  2. <?php
  3. if(isset($_GET['msg']))
  4. {
  5. echo $_GET['msg'];
  6. }
  7. ?>
  8. <!-- Begin page content -->
  9. <div class="container">
  10. <div class="page-header">
  11. <h3>Employee Leaves</h3>
  12. <div class="table-responsive">
  13. <table class="table">
  14. <tr>
  15. <th>Employee Name</th>
  16. <th>Phone</th>
  17. <th>Email</th>
  18. <th>From</th>
  19. <th>To</th>
  20. <th>Reason</th>
  21. <th>Status</th>
  22. <th>---</th>
  23. </tr>
  24. <?php
  25. include ('database.php');
  26. $result = $database->prepare ("SELECT * FROM leaves order by id DESC");
  27. $result ->execute();
  28. for ($count=0; $row_message = $result ->fetch(); $count++){
  29. ?>
  30. <tr>
  31. <td><?php echo $row_message['full_name']; ?></td>
  32. <td><?php echo $row_message['phone']; ?></td>
  33. <td><?php echo $row_message['email']; ?></td>
  34. <td><?php echo $row_message['fromdate']; ?></td>
  35. <td><?php echo $row_message['todate']; ?></td>
  36. <td><?php echo $row_message['reason']; ?></td>
  37. <td><?php echo $row_message['status']; ?></td>
  38. <td>
  39. <form method="post" action="update.php">
  40. <input type="hidden" name="id" value="<?php echo $row_message['id']; ?>" />
  41. <input type="submit" value="Approved" name="approved"></input>
  42. <input type="submit" value="Rejected" name="rejected"></input>
  43. </form>
  44. </td>
  45. </tr>
  46. <?php } ?>
  47. </table>
  48. <a href="home"><button type="button" class="btn btn-primary"><i class="glyphicon glyphicon-arrow-left"></i> Back</button></a>
  49. </div>
  50. </div>
  51. </div>
  52. </div>
  53. </div>
展开查看全部

相关问题