使用 POST 方法从 HTML 表单中检索 SQL 语句不会查询数据库

Retrieving an SQL statement from an HTML form with the POST method doesn't query the database

我目前正在构建一个简单的 CRUD 应用程序,并认为直接从浏览器(即从 HTML table)查询数据库并显示 ('Read') 该特定查询的结果。

但是,我一直在做一些阅读,这似乎本质上是一种 SQL 注入,应该避免。显然,要求用户输入 SQL 语句是不正常的做法。

尽管如此,我一直在尝试添加此功能来试验我的代码,但是 HTML 表单提供的 SQL 语句没有被执行。

回顾一下:

我在 index.php 中的表单要求用户提供 SQL 语句。

然后由 read.php 处理,它使用超全局 $_POST['submitsql'] 检索 SQL 语句('submitsql' 只是表单的名称提交按钮)并使用 query() 方法查询数据库。它还会显示带有 _$_SESSION[''] superglobal.

的消息

来自 index.php:(用户输入 SQL 语句的表单)

 <form action ="read.php" method ="post">
 
 SQL statement: <input type="text" name="sql_stat">
 <button type= 'submit' name = 'submitsql'>Query</button>
 </form><br>

read.php(检索SQL语句并查询数据库)

<?php
include ('server.php');

 if(!isset($_SESSION)){

  session_start();

 }

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

 $sql = $_POST['sql_stat'];
 $results = $conn->query($sql);
 $conn->close();
 $_SESSION['message'] = "Query successfully sent: ".$sql;
 header('location: index.php');
}else{

 $sql = "SELECT * FROM `Students` ORDER BY `degree`"; 
 $results = $conn->query($sql);
 $conn->close();


}

?>

由于某种原因,包含SQL 语句的消息正确显示,但未查询数据库并显示所有记录(在index.php 中的table 中)。

我希望我说得有道理。当 read.php 直接查询数据库而不是从 index.php 中的 HTML 表单中检索 SQL 语句时,我的代码工作正常。如果我没有正确表达自己,请道歉。

如果有任何不同,这里是整个 index.php:

<?php
include('server.php');
include('create.php');
include('read.php');
include('delete.php');
?>
<!DOCTYPE html>
<html>
 <head>
  <title>CRUD PROJECT</title>
  <meta charset="utf-8"/>
 </head>
 <body>
  <h1>CRUD project</h1>

 <h4>Query the database:</h4>
 <form action ="read.php" method ="post">
 
 SQL statement: <input type="text" name="sql_stat">
 <button type= 'submit' name = 'submitsql'>Query</button>
 </form><br>
 
 <?php
 if(isset($_SESSION['message'])){

  echo $_SESSION['message'];
  session_unset();
  session_destroy();
 
 }

 ?>

  <table border = '1' cellpadding = '10' >
  <tr>
   <th>Student ID</th><th>Degree</th><th>Grade</th><th>Graduation Year</th>
  </tr>
  <tr>
   
  </tr>

  <?php



  if($results->num_rows>0){

   while($row = $results->fetch_assoc()){

    echo "<tr><td>".$row['student_id']."</td>";
    echo "<td>".$row['degree']."</td>";
    echo "<td>".$row['grade']."</td>";
    echo "<td>".$row['graduation_year']."</td>";
    echo "<td><a href = 'update.php?student_id=".$row['student_id']."'>Edit</a></td>";
    echo "<td><a href = 'delete.php?student_id=".$row['student_id']."'>Delete</a></td>";


   }
  }else {

   echo "NO RESULTS TO DISPLAY";
  }


  ?>
  </table>


 <br>

 <h2> Add new records </h2>
 <form action ="create.php" method ="post">
 
 Degree: <input type="text" name="degree"><br>
 Grade: <input type="text" name="grade"><br>
 Graduation year: <input type="text" name="graduation_year"><br>
 <button type= 'submit' name = 'submit'>Submit</button>
 </form>

 </body>
 </html>

和 server.php 我连接到数据库并初始化我的变量的地方:

<?php

//Define connection parameters
$db_server = 'localhost';
$db_user = 'root';
$db_password = 'therasmus1';
$db_name = 'University_records';

$conn = new mysqli($db_server,$db_user,$db_password,$db_name);

// Toggle error display 

mysqli_report(MYSQLI_REPORT_ERROR);

// Check connection

if ($conn->connect_error) {
  trigger_error('Database connection failed: '  . $conn->connect_error, E_USER_ERROR);
}

// Initialise your variables (optional - good practice)
 $Degree = "";
 $Grade = "";
 $Graduation_year = "";
 $sql = "SELECT * FROM `Students`";
  $results = $conn->query($sql);

?>

欢迎所有反馈。提前致谢。

我看出你的逻辑有问题。您正在将表单提交到 read.php,然后在该文件中准备 $results,然后立即重定向到 index.php,因此您在提交表单时永远不会使用 $results

但是您还在 index.php 文件中包含了 read.php。那么发生的事情是,您将表单提交到 read.php,创建 $results(但从不使用它),重定向到 index.php,在 index.php 中您已经包含 read.php 所以现在它检查 if(isset($_POST['submitsql'])){ 并且由于请求方法现在不是 post 它转到 else 块:

$sql = "SELECT * FROM `Students` ORDER BY `degree`"; 
$results = $conn->query($sql);
$conn->close();

因此 $results 包含 Students table 的所有记录。

按照这个逻辑,无论你在 <input type="text" name="sql_stat"> 中输入什么,你都会得到 $sql = "SELECT * FROM Students ORDER BY degree";

解决此问题的最简单方法是:

  1. 将您的表格提交至 index.php。在 index.php 中将 <form action="read.php" method="post"> 更改为 <form action="index.php" method="post">

  2. read.php

  3. 中删除 header('location: index.php');

此修复程序将解决您当前的问题。