php - 从 PHP HTML 表提交表单

标签 php html mysql

应该从数据库中获取数据,并显示在表格上。每条记录上都有一个文本框,可以在其中插入收据编号并提交。提交后,收据编号应插入到该记录的数据库中,并且页面重新加载,显示插入的记录而不是文本字段。我不确定我做错了什么,因为页面加载到空白页面。

$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><form action= 'receipt.php' method='post'>
    <input type ='text' name='receipt'>
    <input type ='hidden' name='hidden' value='".$row["receipt"]."'>
    <input type='submit' name='submit' value='submit'></td></form></tr>";
 }
 echo "</table> ";


 // receipt.php

 if(isset($_POST['submit']))
  {

 //connection

 $sql = "INSERT INTO customer (receipt, date_entered) VALUES ('" . $_POST['receipt'] . "','NOW()')";


 if ($conn->query($sql) === TRUE) {

   // echo "New record created successfully";
 } else {
    echo "Error: " . $sql . "<br>" . $conn->error;
 }

 $conn->close(); 


 header('Location: http://localhost/com/payf.php');
 }
  else {
    echo "Please Enter the Receipt Number";
    header('Location: http://localhost/com/payf.php');
  }

最佳答案

在您的receipt.php文件中;

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>title</title>
  </head>
  <body>
<?php

//NEW 
  $servername = "example.com";
  $username = "username";
  $password = "password";
  $dbname = "database";

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

$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><form action= 'receipt.php' method='post'>
    <input type ='text' name='receipt'>
    <input type ='hidden' name='tid' value='".$row["id"]."'>
    <input type='submit' name='submit' value='submit'></td></form></tr>";
 }
 echo "</table>";
} else { //FIX ************
  echo "no results";
}
 // receipt.php

 if(isset($_POST['submit'])) {
  //connection
  //$sql = "INSERT INTO customer (receipt, date_entered) VALUES ('".$_POST['receipt']."',NOW())";  //FIX  ***
  $sql = "UPDATE customer SET `receipt` = '".$_POST['receipt']."', `date_entered` = NOW() WHERE `id` = '".$_POST['tid']."'"; //NEW
   if ($conn->query($sql) === TRUE) {
     // echo "New record created successfully";
   } else {
      echo "Error: " . $sql . "<br>" . $conn->error;
      exit(); //NEW
   }

   $conn->close(); 
   header("Location: http://{$servername}/project/payf.php"); //FIX ************
 } else {
    //echo "Please Enter the Receipt Number"; //FIX ************ this goes into payf.php
    //header("Location: http://{$servername}/project/payf.php");//FIX ******** this redirects when main table is dispayed
  }
echo "</body></html>";

?>
</body></html>

根据注释,此代码中存在许多问题

  • 在 header 重定向之前回显
  • 没有 $conn 设置
  • 表格显示时重定向
  • 缺少右括号
  • 不正确的 SQL 语法(引号)
  • 插入而不是更新
  • HTML 质量较差
  • 结构不良

请查看这些页面;

此代码可以正常运行并经过测试。 快乐编码!

关于php - 从 PHP HTML 表提交表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38121804/

相关文章:

html - 谷歌字体在实时服务器上工作但不在本地主机上工作

java - 如何从pdf中读取数据并插入到db中?

php - CakePhp 3 - 链接并保存与包含两个相同模型外键的表的关联

c# - 在 C# 中使用 TripleDES 加密的字符串,需要在 PHP 中解密

php datetime 与我的 windows 系统不匹配

php - 无法从 Linux 中的 HTMl 提交按钮运行 PHP 脚本

jquery - 尝试使用 jquery 设置动画时如何定位 html 元素?

php - 半径/最近的结果 - Google Maps API

php - MySQL 表/查询设计 - 一项的倍数,许多评论

php - 连接到 comment_text() 以添加周围的标签