按钮代码应该放在哪里?

时间:2016-04-27 12:18:01

标签: php html mysql

我正在尝试显示包含数据库中值的表。我想在表中添加一个列,可以放置一个按钮,以便更新数据库。这是php代码。

<!DOCTYPE html>
<head>
   <style type="text/css">.mui-textfield.mui-textfield--float-label > label {-webkit-transition:all .15s ease-out;-moz-transition:all .15s ease-out;-o-transition:all .15s ease-out;transition:all .15s ease-out;}</style>
   <meta charset="utf-8">
   <meta http-equiv="X-UA-Compatible" content="IE=edge">
   <meta name="viewport" content="width=device-width, initial-scale=1">
   <link rel="stylesheet" type="text/css" media="screen" 
      href="//cdn.muicss.com/mui-0.5.3/css/mui.css">
   <link rel="shortcut icon" href="final.ico" />
   <link rel="stylesheet" href="new_style.css">
   <script src="//cdn.muicss.com/mui-0.5.3/js/mui.js"></script>
   <style>
      /* Add font-smoothing */
      html,
      body,
      input,
      textarea,
      buttons {
      -webkit-font-smoothing: antialiased;
      -moz-osx-font-smoothing: grayscale;
      text-shadow: 1px 1px 1px rgba(0, 0, 0, 0.004);
      }
   </style>
   <style>
      a:link, a:visited {
      background-color: #ff0066;
      color: white;
      padding: 10px 10px;
      text-align: center;    
      text-decoration: none;
      display: inl
      a:hover, a:active {
      ine-block;
      }
      a:hover, a:active {
      background-color: red;
      }
   </style>
</head>
<body>
   <form method = "GET" action = "update.php">
      <body>
         <header class="mainheader">
         <div style="background-color:#d9d9d9">
            <nav>
               <ul>
                  <li><a href="/" style="color:#FFF">Home</a></li>
                  <li><a href="/loginProf.php" style="color:#FFF">Log-in</a></li>
               </ul>
            </nav>
         </div>
</body>
<center>
<h2> Attendance for Classroom 106 </h2>
<div class="table-responsive-vertical shadow-z-1">
<table class='mui-table mui-table--bordered'>
<thead><tr><th>ID</th><th>Name</th><th>Attendance</th><th>Status</th><th>Mark Attendance</th></tr></thead><tbody>
<?php
   $servername = "localhost";
   $username = "root";
   $password = "root";
   $dbname = "attendance";

   // Create connection
   $conn = new mysqli($servername, $username, $password, $dbname);
   // Check connection
   if ($conn->connect_error) {
       die("Connection failed: " . $conn->connect_error);
   }

   $sql = "SELECT pid, name, attendance, status FROM students";
   $result = $conn->query($sql);

   if ($result->num_rows > 0) {
      // echo "<table><tr><th>ID</th><th>Name</th><th>Attendance</th><th>Status</th></tr>";
       // output data of each row
       while($row = $result->fetch_assoc()) {
           echo "<tr><td>".$row["pid"]."</td><td>".$row["name"]."</td><td> ".$row["attendance"]."</td><td> ".$row["status"]."</td><td><a href='update.php?uname=" .$row['uname'].'">update</a></td></tr>";

       }
       echo "</table>";
   } else {
       echo "0 results";
   }
   $conn->close();
   ?>
</div> <br><br>
<a href="loggedinProf.html"> Go back </a>
</center>
<input type = "submit" name = "update" value = "MarkAttendance"/></form>
</body>
</html>

该按钮应添加为最后一列。 这是update.php代码

<?php

require "init.php";
$attendance = $_POST["attendance"];
$uname      = $_POST["uname"];
$count      = $_POST["status"];
$query      = "UPDATE students SET attendance = ('$attendance' + attendance) WHERE uname = '$uname';";
$query1     = "UPDATE students SET status = ('$count') WHERE uname = '$uname';";
$result     = mysqli_query($con, $query);
$result1    = mysqli_query($con, $query1);
if ($result > 0 && $result1 > 0) {
    echo "success";
} else {
    echo "failed";
}
?>

1 个答案:

答案 0 :(得分:0)

改变这个:

   echo "<tr><td>".$row["pid"]."</td><td>".$row["name"]."</td><td> ".$row["attendance"]."</td><td> ".$row["status"]."</td><td><a href='update.php?uname=" .$row['uname'].'">update</a></td></tr>";

对此:

  echo "<tr><td>".$row["pid"]."</td><td>".$row["name"]."</td><td> ".$row["attendance"]."</td><td> ".$row["status"]."</td><td><a href='update.php?uname=" .$row['uname'].">update</a></td></tr>";