从 mysql 输入的 html 中搜索数据

IT技术 javascript php html mysql
2021-02-27 09:55:25

我想从我的数据库(mysql)中获取数据并将其显示在网站上,脚本应该被保存。

这是我已经拥有的文件结构:

/includes
    db_connect.php
    functions.php
    getdata.php
    logout.php
    process_login.php
    psl-config.php
    register.inc.php
/js
    forms.js
    sha512.js
login.php
protected_page.php
register.php
register_success.php
seach.php

现在按照重要文件:psl-config.php

<?php
/**
 * Das sind die Login-Angaben für die Datenbank
 */  
define("HOST", "localhost");     // Der Host mit dem du dich verbinden willst.
define("USER", "sec_user");    // Der Datenbank-Benutzername. 
define("PASSWORD", "eKcGZr59zAa2BEWU");    // Das Datenbank-Passwort. 
define("DATABASE", "secure_login");    // Der Datenbankname.

define("CAN_REGISTER", "any");
define("DEFAULT_ROLE", "member");

define("SECURE", FALSE);    // NUR FÜR DIE ENTWICKLUNG!!!!
?>

db_connect.php

<?php
include_once 'psl-config.php';   // Da functions.php nicht included ist
$mysqli = new mysqli(HOST, USER, PASSWORD, DATABASE);
?>

这是我的search.php

<?php
include_once 'includes/db_connect.php';
include_once 'includes/functions.php';

sec_session_start();
?>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Secure Login: Protected Page</title>
        <link rel="stylesheet" href="styles/main.css" />
    </head>
    <body>
        <?php if (login_check($mysqli) == true) : ?>
            <p>Welcome <?php echo htmlentities($_SESSION['username']); ?>!</p>

            <h3>Search  Contacts Details</h3> 
            <p>You  may search either by first or last name</p> 
            <form  method="post" action="search.php?go"  id="searchform"> 
                <input  type="text" name="name"> 
                <input  type="submit" name="submit" value="Search"> 
            </form>
        <?php else : ?>
            <p>
                <span class="error">You are not authorized to access this page.</span> Please <a href="login.php">login</a>.
            </p>
        <?php endif; ?>
    </body>
</html>

我应该把我的文本框条目放在哪里才能安全地搜索我的网站?我的代码可以安全使用吗?

这是我的 sql 命令: SELECT * FROM produckte WHERE beschreibung = $search LIMIT 100;

我想在搜索网站上打印结果。

2个回答

首先,将输入搜索的名称更改为“搜索”:

<input  type="text" name="search">

您正在使用“POST”方法将表单发送到同一个 .php 文件。这意味着您可以通过访问 $_POST 变量来访问发送到页面的任何信息。

将此添加到search.php文件顶部<?php ?>标签内:

if (isset($_POST['search']) {
  echo $_POST['search'];
}

这将使您了解如何处理从<form>.

看看这个 PHP doc,关于处理表单。

mysqli允许您使用预准备语句,这是将用户输入传递给数据库查询的安全方式。

关于如何使用准备好的语句查询数据库的示例:

if (isset($_POST['search']) {
  $stmt = $mysqli->prepare("SELECT * FROM produckte WHERE beschreibung = ? LIMIT 100;")
  $stmt->bind_param("s", $_POST['search']);
  $stmt->execute();

  $result = $stmt->get_result();
  while ($row = $result->fetch_array(MYSQLI_NUM))
  {
    .....handle your data here....
  }
  $stmt->close();
}

我会建议使用这样的东西:

SELECT * FROM produckte WHERE beschreibung LIKE '%".$search."%'; 如果您有准备好的语句,则此查询是安全的

% 使您的查询返回具有搜索关键字的所有内容,例如您搜索:“hello” 并且您的数据库有“1hello”、“hello2382” 如果您使用的查询没有% 你将有 0 个结果。更多信息

这应该可以解决问题:

$query = "SELECT * FROM produckte WHERE beschreibung LIKE '%".$search."%';";

if ($result = $mysqli->query($query)) {

    /* fetch associative array */
    while ($row = $result->fetch_assoc()) {
        printf ("%s (%s)\n", $row["Name"], $row["column name"]);
        //You could echo a div in here with the data you want to be displayed
    }

    /* free result set */
    $result->free();
}

在你的search.php

你应该做这个:

//your form submit button name
if(isset($_POST['submit'])){
    //your form textfield name
    $search = $_POST['name'];

    //execute the while query here.

}
这部分我已经理解了,但我如何获得 $search?你能把文本框中的数据展示给我看吗?
2021-05-15 09:55:25