如何在表中包含输入文本字段和提交按钮

时间:2016-06-29 11:39:37

标签: php html

这是我在桌面上显示数据库数据的代码。我希望在表的最后一列“收据号”上有一个文本框和一个提交按钮,用户可以在其中键入一些文本并提交保存。这应该在从数据库获取的每个记录上打开。我在设计方面遇到了挑战

$sql = "SELECT * FROM customer";
$result = $conn->query($sql);

if ($result->num_rows > 0) {

 echo " <table><tr><th>ID</th><th>Name</th><th>Amount</th><th>TransactionID</th><th>Mobile Number</th><th>Time Paid</th><th>Account</th><th>Receipt Number</th></tr>";

 // output data of each row
 while($row = $result->fetch_assoc()) {
     echo "<tr><td>" . $row["id"]. "</td><td>" . $row["name"]. "</td><td>" . $row["amount"]. "</td><td>" . $row["trans_id"]. "</td><td>" . $row["msisdn"]. "</td><td>" . $row["time_paid"]. "</td><td>" . $row["status"]. "</td><td>" . $row["account"]. "</td><td></td></tr>";
 }
 echo "</table> "; 
} else {
 echo "0 results";
}

1 个答案:

答案 0 :(得分:1)

只需在上一个form内使用tag td,然后使用text fieldsubmit button,并检查form是否已设置为“{1}}”该行queryupdate。 还可以使用hidden field来存储行的id。

<?php    
$sql = "SELECT * FROM customer";
$result = $conn->query($sql);
if(isset($_POST['submit'])){
//echo "<pre>";print_r($_POST); 
// get the vakue of hidden in which i saved row primary id field and receipt  in which i saved input field value and run query  and update data

    }
if ($result->num_rows > 0) {
   echo " <table><tr><th>ID</th><th>Name</th><th>Amount</th><th>TransactionID</th><th>Mobile Number</th><th>Time Paid</th><th>Account</th><th>Receipt Number</th></tr>";

 // output data of each row
 while($row = $result->fetch_assoc()) {
      echo "<tr><td>" . $row["id"]. "</td><td>" . $row["name"]. "</td><td>" . $row["amount"]. "</td><td>" . $row["trans_id"]. "</td><td>" . $row["msisdn"]. "</td><td>" . $row["time_paid"]. "</td><td>" . $row["status"]. "</td><td><form method='post'><input type ='text' name='receipt'><input type ='hidden' name='hidden' value='".$row["student_id"]."'><input type='submit' name='submit' value='submit'></td></form></tr>";
 }
 echo "</table> "; 
     } else {
 echo "0 results";
}