好的,所以我有一个连接到数据库的小字典程序。用户可以向数据库添加单词,删除单词,以及(在完美世界中)搜索所有现有单词并查看他们在屏幕上查找的定义。最后一点是问题。我不知道如何搜索我的数据库中的所有现有单词,并将我正在寻找的单词放在/home/<username>
之上。如何显示搜索结果?
以下是代码:
inputs
答案 0 :(得分:1)
if(isset($_POST['search'])){
$mysqli = //here is where my database connection info would go
$command = "SELECT *FROM users WHERE `word`='{$mysqli->real_escape_string($_POST['search'])}' or `def`= '{$mysqli->real_escape_string($_POST['search'])}' ";
$select = $mysqli->query($command);
if ($select->num_rows > 0) {
// output data of each row
while($row = $select->fetch_assoc()) {
echo "Word: " . $row["word"]. " " . $row["def"]. "<br>";
}
} else {
echo "0 results";
}
}
答案 1 :(得分:0)
获取搜索字符串的HTML
<form method="POST" action="someurl.php">
<input type="search" name="search-string">
<input type="submit" value="Search">
</form>
从数据库获取数据的代码(可能有轻微的语法错误)。
$sql = "SELECT word,def FROM users WHERE word LIKE '%{$mysqli->real_escape_string($_POST['search-string'])}% OR def LIKE ', '%{$mysqli->real_escape_string($_POST['search-string'])%}' ORDER BY word";
$result = $mysqli->query($sql);
$rows = [];
while ($row = $result->fetch_assoc()) {
$rows[] = $row;
}
以HTML格式显示结果的代码
<?php if (count($rows) > 0) : ?>
<dl>
<?php foreach ($rows as $r) : ?>
<dt><?= htmlentities($r['word']) ?></dt>
<dd><?= htmlentities($r['def']) ?></dd>
<?php endforeach ?>
</dl>
<?php endif ?>
答案 2 :(得分:0)
首先,您应该了解更多PHP并开始学习PHP中的OOP编码。其次,在您的代码中使用HTML5!(我在此答案中不使用它,请在Google上搜索有关HTML5的更多信息。)第三,我已根据您的要求更新了您的代码,但仍然不是最佳实践。
index.php
档案:
<?php
$link = //here is where my database connection info would go
if (mysqli_connect_error()) {
die("Could not connect to database");
}
if(isset($_POST['add'])){
$mysqli = //here is where my database connection info would go
$word = $mysqli->real_escape_string($_POST['word']);
$def = $mysqli->real_escape_string($_POST['def']);
$sql = "INSERT INTO users (word,def) VALUES (".$word.",".$def.")";
$insert = $mysqli->query($sql);
if ($insert) {
echo 'success';
} else {
echo 'error';
}
}
if(isset($_POST['delete'])){
$mysqli = //here is where my database connection info would go
$word = $mysqli->real_escape_string($_POST['delete-word']);
$def = $mysqli->real_escape_string($_POST['delete-def']);
$sql = "DELETE FROM users WHERE `word`='".$word."' OR `def`='".$def."'";
$delete = $mysqli->query($sql);
if ($delete) {
echo 'success';
} else {
echo 'error';
}
}
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
<title>Dictionary search</title>
</head>
<body>
<form class="forms" method="post" action="index.php">
<input type="text" name="word" placeholder="Add Word...">
<input type="text" name="def" placeholder="Add Definition...">
<input type="submit" name="add" value="Add">
</form><br>
<form class="forms" method="post" action="index.php">
<input type="text" name="delete-word" placeholder="Delete Word...">
<input type="text" name="delete-def" placeholder="Delete Definition...">
<input type="submit" name="delete" value="Delete">
</form>
<form class="forms" method="post" action="search.php">
<input type="text" name="search-word" placeholder="Look Up Word...">
<input type="text" name="search-def" placeholder="Look Up Definition...">
<input type="submit" name="search" value="Search Dictionary">
</form>
</body>
</html>
search.php
档案:
<html>
<body>
<?php
if(isset($_POST['search'])){
//this is where the code would go to search the database and output the answer on the screen.
$mysqli = //here is where my database connection info would go
$search_w = $mysqli->real_escape_string($_POST['search-word']);
$search_d = $mysqli->real_escape_string($_POST['search-def']);
$sql = "SELECT FROM `users` WHERE `word`='' OR `def`=''";
$result = $mysqli->query($sql);
$rows = [];
while ($row = $result->fetch_assoc()) :
$rows[] = $row;
?>
<p><?php echo $row; ?></p>
<?php endwhile;
}
?>
</body>
</html>