【问题标题】:search data from html input in mysql从 mysql 中的 html 输入中搜索数据
【发布时间】:2014-11-04 20:16:08
【问题描述】:

我想从我的数据库(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;

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

【问题讨论】:

  • 只要您为数据库准备好信息,它是绝对安全的。由于您使用 mysqli,您可以使用准备好的语句。 prepared statements mysqli 或者您可以在直接使用 sql 之前使用 php 清理字符串。然而,第一个选择更好。

标签: javascript php html mysql


【解决方案1】:

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

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

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

将此添加到search.php 文件顶部的&lt;?php ?&gt; 标签内:

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

这将使您了解如何处理从&lt;form&gt; 发布的数据。

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

mysqli 允许您使用prepared-statements,这是一种将用户输入传递给数据库查询的安全方式。

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

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();
}

【讨论】:

    【解决方案2】:

    我建议使用这样的东西:

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

    % 使您的查询返回具有搜索关键字的所有内容,例如:“hello” 并且您的数据库有“1hello”、“hello2382” 如果您使用没有 % 的查询,您的结果将是“1hello”和“hello2382”,您将得到 0 个结果。 More Info

    这应该可以解决问题:

    $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?你能把文本框中的数据给我看吗?
    猜你喜欢
    • 2011-06-22
    • 1970-01-01
    • 2016-12-02
    • 2019-01-08
    • 2013-04-07
    • 2020-02-16
    • 2021-06-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多