我想从我的数据库(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;
我想在搜索网站上打印结果。
答案 0 :(得分:1)
我建议使用这样的东西:
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.
}
答案 1 :(得分:0)
首先,将输入搜索的名称更改为“搜索”:
<input type="text" name="search">
您正在使用“POST”方法将表单发送到同一个.php文件。 这意味着您可以通过访问$ _POST变量来访问发送到页面的信息。
将其添加到search.php
标记内的<?php ?>
文件顶部:
if (isset($_POST['search']) {
echo $_POST['search'];
}
这将让您了解如何处理从<form>
发布的数据。
关于处理表单,请查看此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();
}