Php 在搜索中显示mysql内容
好的,我有一个连接到数据库的小字典程序。用户可以将单词添加到数据库中,删除单词,然后(在一个完美的世界中)搜索所有现有的单词,并在屏幕上查看他们正在查找的定义。最后一点就是问题所在。我不知道如何搜索数据库中的所有现有单词,并将我要查找的单词放在Php 在搜索中显示mysql内容,php,mysql,Php,Mysql,好的,我有一个连接到数据库的小字典程序。用户可以将单词添加到数据库中,删除单词,然后(在一个完美的世界中)搜索所有现有的单词,并在屏幕上查看他们正在查找的定义。最后一点就是问题所在。我不知道如何搜索数据库中的所有现有单词,并将我要查找的单词放在输入的上方。如何显示搜索结果 代码如下: <?php $link = //here is where my database connection info would go if (mysqli_connect_error())
输入的上方。如何显示搜索结果
代码如下:
<?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
$sql = "INSERT INTO users ( word,def) VALUES ( '{$mysqli->real_escape_string($_POST['word'])}', '{$mysqlii->real_escape_string($_POST['def'])}' )";
$insert = $mysqli->query($sql);
header("Refresh:0");
}
if(isset($_POST['delete'])){
$mysqli = //here is where my database connection info would go
$command = "DELETE FROM users WHERE `word`='{$mysqli->real_escape_string($_POST['delete-word'])}' or `def`= '{$mysqli->real_escape_string($_POST['delete-def'])}' ";
$delete = $mysqli->query($command);
header("Refresh:0");
}
if(isset($_POST['search'])){
//this is where the code would go to search the database and output the answer on the screen.
}
?>
<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 = "">
<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>
HTML以获取搜索字符串
<form method="POST" action="someurl.php">
<input type="search" name="search-string">
<input type="submit" value="Search">
</form>
用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 ?>
首先,您应该更多地了解PHP,并开始学习PHP中的OOP编码。第二,在代码中使用HTML5!(我在回答这个问题时没有用到它,请在谷歌上搜索更多关于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>
<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>
if(isset($\u POST['search'])){
$mysqli=//下面是我的数据库连接信息
$command=“SELECT*来自以下用户:`word`='{$mysqli->real\u escape\u string($\u POST['search'])}'或`def`='{$mysqli->real\u escape\u string($\u POST['search'])}';
$select=$mysqli->query($command);
如果($select->num\u rows>0){
//每行的输出数据
而($row=$select->fetch_assoc()){
回显“Word:.$row[“Word”]。$row[“def”]。
”;
}
}否则{
回显“0结果”;
}
}
$mysqlii->real\u escape\u string
onei
太多了是的。哈哈。有没有关于如何搜索数据库并输出响应的想法?使用select sql命令并循环其结果集并打印结果。从表格中选择def,其中word='searched word'对不起,这里太乱了。我在Windows操作系统,这不是我的编码环境。另外,请下次学习PDO而不是mysqli,甚至mysql;)非常感谢。很高兴知道。谢谢你!那有帮助@欢迎光临。感谢你的支持投票。谢谢,谢谢!非常有用。
<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>
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";
}
}