【问题标题】:I'm getting no errors, but also getting no results - mysql php db我没有得到任何错误,但也没有得到任何结果 - mysql php db
【发布时间】: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


【解决方案1】:

你错了SQL,它是在运行时构造的

$sql = "SELECT $col1, $col2 FROM $tablename WHERE $col1, $col2 LIKE 
'%$song_query%'";

变成了

$sql = "SELECT title, artist FROM $tablename WHERE title, artist LIKE 
'%$song_query%'";

WHERE title, artist LIKE这里

$song_query$_GET['song_query'] 获取值,该值在运行时发生变化。

【讨论】:

    【解决方案2】:

    这个WHERE $col1, $col2 LIKE '%$song_query%'是你需要说的无效语法

    WHERE col1 LIKE '%something%' AND col2 LIKE '%something%'
    

    所以这应该可以解决问题

    $sql = "SELECT $col1, $col2 
            FROM $tablename 
            WHERE $col1 LIKE '%$song_query%'
            AND $col2 LIKE '%$song_query%'";
    

    尽管这对SQL Injection Attack 开放 甚至if you are escaping inputs, its not safe! 使用prepared parameterized statements

    $sql = "SELECT title, artist 
            FROM songlist 
            WHERE title LIKE ? AND artist LIKE ?";
    
    $stmt = $conn->prepare($sql);
    $val = sprintf('%%%s%%', $song_query);
    $stmt->bind_param('ss',$val, $val);
    
    $stmt->execute();
    
    $stmt->bind_result($title, $artist);
    
    echo "<table><thead><tr>";
    echo "<th>Artist</th><td>Title</th>";
    echo "</tr></thead><tbody>";
    
    while ($stmt->fetch()) {
        echo "<tr>";
            echo "<td>$artist</td>";
            echo "<td>$title</td>";
        echo "</tr>";
    }
    echo "</tbody></table>";
    

    另请注意,您在构建表格时犯了几个错误,我认为我已经修复了。

    【讨论】:

    • 谢谢 RiggsFolly!我将在我的卡拉 OK 节目中在本地服务器上使用它,所以我不太担心注入攻击。托管这个的系统上不会有任何互联网访问。
    • 至少现在我收到您提供的代码 sn-p 的错误。大声笑...警告:sprintf():第 36 行 C:\xampp\htdocs\bluesky\runit.php 中的参数太少致命错误:未捕获的错误:在 C:\xampp 中的布尔值上调用成员函数 bind_param() \htdocs\bluesky\runit.php:37 堆栈跟踪:#0 {main} 在第 37 行的 C:\xampp\htdocs\bluesky\runit.php 中抛出
    • 啊,是的。我的错。见修改sprintf()
    • $val = "%$song_query%";可能更简单
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多