微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

$_GET 在我的程序中不起作用,它下面没有任何行被执行

如何解决$_GET 在我的程序中不起作用,它下面没有任何行被执行

我正在尝试向我的网站添加一个函数,通过允许用户在某些文本框中输入文本并单击过滤器按钮,它将向 sql 选择语句添加过滤器。

我还没有达到那个程度,因为 $_GET 函数有问题。

这个想法是,如果设置了查询参数,它会在 sql 语句中使用它们,如果没有,它只会返回所有行。

下面是每次加载页面调用displayListings 函数。 虽然我展示了整个函数,但我还没有实现它的大部分,因为在我使用 $_GET 的那一行之后没有任何工作。

    <?PHP
    function displayListings() {
        global $dbConnection;

        //checks if the query parameters exist
        if (isset($_GET['title'])) {
            echo 'got here... :F';//for debugging
            var_dump($_GET['title']);//for debugging
            $title_filter = $_GET('title');// THIS IS THE LINE THAT THE SCRIPT STOPS AT
            var_dump($title_filter);//for debugging
        }
        //checks if the query parameters exist
        if (isset($_GET['artist'])) {
            $artist_filter = $_GET('artist');
            echo $artist_filter."/n";
        }
        //checks if the query parameters exist
        if (isset($_GET['release'])) {
            $release_filter = $_GET('release');
            echo $release_filter."/n";
        }

        echo 'here!';

        // connect to the database
        if (!connectToDb('musiconline')) {
            $_SESSION['errorMsg'] = "Sorry,we Could not connect to the database.";
            header('location:listItem.PHP');
            exit();
        }

        // after this point we have an open DB connection
        // gets the current highest ID so we kNow what the next should be.
        $sqlQuery = "SELECT listingid,recordtitle,artist FROM vinyl";
        $result = $dbConnection->query($sqlQuery);
        if (!$result) {
            $_SESSION['errorMsg'] = "There was a problem with the database: " . $dbConnection->error;
            closeConnection();
            header('location:listItem.PHP');
            exit();
        }
        
        //gets the results and puts them in a rows array
        while($row = $result->fetch_array()){
            $rows[] = $row;
        }

        //iterates through each row of the results (each vinyl)
        foreach($rows as $row){
            $listingID = $row['listingid'];
            $recordTitle = $row['recordtitle'];
            $artist = $row['artist'];

            echo '  
                    <div class="listing">
                        <table class="tableception">
                            <tr><td><img src="uploads/vinyl'.$listingID.'.png" alt="img1" ></td><td>
                                <table class="listing-table">
                                    <tr><td>Album title:&nbsp;&nbsp;&nbsp;&nbsp;</td><td>'.$recordTitle.'</td></tr>
                                    <tr><td>Artist name:&nbsp;&nbsp;&nbsp;&nbsp;</td><td>'.$artist.'</td></tr>
                                </table>
                            </td></tr>
                        </table>
                    </div>
            ' . "\n";
        }//END OF FOREACH

        /* free result set */
        $result->close();

        /* close connection */
        closeConnection();
    }
?>

当我在 IDE 中调试页面时,一切正常,可能是因为 URL 中没有查询参数。

页面在 $_GET 行之后停止加载,如下所示。shows screenshot of browser when query parameters exist in URL

我就是想不通我做错了什么。

提前致谢。

解决方法

我在行中使用了大括号而不是方括号。

我有$title_filter = $_GET('title'); 而不是 $title_filter = $_GET['title'];

感谢@AymDev 向我指出这一点!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。