【发布时间】:2023-04-01 12:47:02
【问题描述】:
我正在尝试从具有两列的数据库中获取用户提交。一个用于艺术家,一个用于标题。我想从简单的表格中获取他们的输入,并将所有类似的结果输出到下一页的表格中。到目前为止,我已经包含了我编写的整个脚本。我在页面上没有收到任何错误,但我也没有得到任何结果。我花了几天时间在网上寻找我是否可以自己解决这个问题,但我没有这样的运气。很抱歉这么罗嗦,但我是这个网站的新手,想提供尽可能多的细节。
<?php
include("db_connect.php");
// - get form data from "searchform.php"
$song_query = $_GET['song_query'];
// - column 1 and column 2 are all we're looking for in the db
// - title and artist are currently the two cols. Table is named
"song_list"
$col1 = "title";
$col2 = "artist";
$tablename = "song_list";
echo "<H1>Search results</H1>";
if ($song_query == "") {
echo "What are you going to sing? You didn't enter a search term! Please
try again.";
exit;
}
// - pick the db connection out of the included file and give error if
failed.
mysqli_select_db($conn, $db_name) or die(mysqli_error());
// - cleans up string inputted by user to avoid harmful code insertion
into form
$song_query = strtoupper($song_query);
$song_query = strip_tags($song_query);
$song_query = trim($song_query);
// - set up parameters for accessing the query of the db
$sql = "SELECT $col1, $col2 FROM $tablename WHERE $col1, $col2 LIKE
'%$song_query%'";
$result = mysqli_query($conn, $sql);
if (isset($_GET['$result'])){
if (mysqli_num_rows($result) > 0){
echo "<table><tr>";
echo "<th>Artist</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['$result'] . "</td>";
echo "</tr>";
echo "</table>";
}
}
}
?>
【问题讨论】:
-
$_GET['$result']应该在做什么... -
你希望
WHERE $col1, $col2 LIKE '%$song_query%'";做什么? -
MySQLi 往往会默默地失败。始终检查 [mysqli 错误](php.net/manual/en/mysqli.error.php) 以确保您的数据库调用正常工作。
-
我很抱歉我是个业余爱好者,我这样做是为了自学图形设计以外的东西。我正在拼凑几个教程来开发此代码。 isset 摆脱了我的最后一个错误,w3c 给了我使用 WHERE 和 LIKE 与两列名称的想法。
-
感谢大家的意见!我对此仍然很陌生,我很感激我得到的所有帮助。我不得不完全重写这个东西五次,但我终于让它工作了。这是一次很棒的学习经历。谢谢! - 我使用了每个人的一些建议来让它发挥作用,我评论了它的废话。大声笑
标签: php mysql database forms search