我是PHP新手,正在尝试使用提交按钮来读取SQL表

siv3szwd  于 2023-03-11  发布在  PHP
关注(0)|答案(2)|浏览(396)

我试图在html上创建一个可见的产品列表。我已经得到了它的点,我有所有的产品和细节显示和一个提交按钮与文本框的搜索在顶部,什么也没做。然后我试图使提交按钮的搜索工作,但现在它只显示文本框和提交按钮,当查询搜索它不显示任何东西,除了网址更新。我正在尝试让它搜索表“产品”中的“说明单元格”数据,但它无法正常工作。请帮助我让搜索功能正常工作。
下面是代码。

`<!doctype html>
<html>
<head>
    <meta charset="UTF-8" /> 
    <title>My First SQL Page</title>
    <link rel="stylesheet" type="text/css" href="shopstyle.css" />
</head>
<body>
    <h1>Products List</h1>
    <?php 
        
        // include some functions from another file.
        include('functions.php');
        
        // if the user provided a search string.
        
        if(isset($_GET['search']))
        {
            $searchString = $_GET['search'];
        }
        
        // if the user did not provide a search string, assume an empty string
        
        else
        {
            $searchString = "";
        }
        
        $SqlSearchString = "%searchString%";
        $safeSearchString = htmlspecialchars($searchString, ENT_QUOTES,"UTF-8");
        
        echo "<form>";
        echo "<input name = 'search' type = 'text' value = '$safeSearchString'/>";
        echo "<input type = 'submit'/>";
        echo "</form>";
        
        // connect to the database using our function (and enable errors, etc)
        $dbh = connectToDatabase();
        
        $sql = "SELECT * FROM Products WHERE Description = ?";
        
        // select all the products.
        $statement = $dbh->prepare($sql);
        $statement ->bindValue(1,$SqlSearchString,PDO::PARAM_STR);
        
        //execute the SQL.
        $statement->execute();

        // get the results
        while($row = $statement->fetch(PDO::FETCH_ASSOC))
        {
            // Remember that the data in the database could be untrusted data. 
            // so we need to escape the data to make sure its free of evil XSS code.
            $ProductID = htmlspecialchars($row['ProductID'], ENT_QUOTES, 'UTF-8'); 
            $Price = htmlspecialchars($row['Price'], ENT_QUOTES, 'UTF-8'); 
            $Description = htmlspecialchars($row['Description'], ENT_QUOTES, 'UTF-8'); 
            
            // output the data in a div with a class of 'productBox' we can apply css to this class.
            echo "<div class = 'productBox'>";
            echo "<img src = '/ProductPictures/$ProductID.jpg' />";
            echo "$Description <br/>";
            echo "$Price <br/>";
            echo "</div> \n";           
        }
    ?>
</body>
</html>`

我试过更新我的SQL查询没有运气,我不知道问题出在哪里,因为搜索工作完美的SQL数据库时,我尝试选择 * 从产品的描述,如%无线电%和工作。

vqlkdk9b

vqlkdk9b1#

您应该使用like作为参数,然后将%添加到变量中

$sql = "SELECT * FROM Products WHERE Description LIKE ?";
    
    // select all the products.
    $statement = $dbh->prepare($sql);
    $statement ->bindValue(1,"%".$SqlSearchString."%",PDO::PARAM_STR);
7tofc5zh

7tofc5zh2#

我尝试从产品中选择 *,其中的描述如%radio%,效果很好。
验证码:

SELECT * FROM Products WHERE Description = ?

在将$SqlSearchString绑定到参数之前,您确实正确地将% Package 在了%符号中,但是在查询中使用了=而不是LIKE

相关问题