【问题标题】:$_GET variable is Undefined within the Pagination Code$_GET 变量在分页代码中未定义
【发布时间】:2013-12-26 01:10:41
【问题描述】:

我对 PHP 不熟悉,并且在我的分页代码中如何创建 $_GET 或 $_SESSION 变量时遇到了 2 周的问题。情况是:

我有一个来自index.php 页面的值,当我单击它(例如照片/图像)时,我使用 GET 获取该值,然后当我将该值回显到我的Category.php 页面时,该值在我的 BODY 页面使用“echo”,但我想要的是当页面加载时,GET 变量将是我的分页代码中的搜索键。但是当我在我的分页代码中回显 GET 变量时,我得到了这个结果“未定义”,这意味着该变量没有在我的分页代码中定义。这是我的分页。哦,是的,我对我的代码进行了更改,我很抱歉,但这是我之前使用 $mycat 和 GET 方法的代码。对不起。

<?php
session_start();
$mycat =$_GET['category'];
//PHP code for specific search
$action = (isset($_REQUEST['action'])&& $_REQUEST['action'] !=NULL)?$_REQUEST['action']:'';

if($action == 'ajax'){

    /* Connect To Database*/
    $dbname = 'ufile';
    $link = mysql_connect("localhost","root","") or die("Couldn't make connection.");
    $db = mysql_select_db($dbname, $link) or die("Couldn't select database");

    $mycat =  htmlspecialchars ($_GET['category']) ? trim($_GET['category']) : ''; //SQL injection protection

    include 'pagination.php'; //include pagination file

    //pagination variables
    $page = (isset($_REQUEST['page']) && !empty($_REQUEST['page']))?$_REQUEST['page']:1;
    $per_page = 10; //how many records you want to show
    $adjacents  = 4; //gap between pages after number of adjacents
    $offset = ($page - 1) * $per_page;

    echo 'Result: ' .$mycat; //////////THIS IS THE GET VALUE....WHICH TURNS OUT AS UNDEFINED IN VALUE IT SHOULD BE 'PHOTOS/IMAGES, AUDIOS/VIDEOS or COMPRESSED FILES'....PLEASE HELP ME.

    //Count the total number of row in your table*/
    $count_query   = mysql_query("SELECT COUNT(category) AS numrows FROM udocs WHERE category LIKE '%" . $mycat . "%'"); //Count the number of search result here (tagal kitang hinanap dito ka lng pala)
    $row     = mysql_fetch_array($count_query);
    $numrows = $row['numrows'];
    $total_pages = ceil($numrows/$per_page);
    $reload = 'cat_index.php';

    //main query to fetch the data
    $result = mysql_query("SELECT * FROM udocs WHERE category LIKE '%" . $mycat . "%' ORDER BY date_upload LIMIT $offset,$per_page");


    //loop through fetched data
    while($test = mysql_fetch_array($result)){

        $id = $test['file_name'];   
            $fileid=$test['file_path'];

            echo "<div class='content'>";
                echo'<div class="img_content"  ><img src='.$test['file_icon'].' style=height:50px;width:50px;float:left;margin-right:10px;></div>';
                echo"<font  color='black'; font size='2px !important' ;font family='Century Gothic, sans-serif' ;letter spacing='2px';>Name:   </font><font color='yellowgreen'; font size='2px !important' ;font family='Century Gothic, sans-serif' ;letter spacing='2px';>" .$test['file_name']."</font><br>";
                    echo"<font color='black'; font size='2px !important' ;font family='Century Gothic, sans-serif' ;letter spacing='2px';>Category:   </font><font color='gray'; font size='2px !important' ;font family='Century Gothic, sans-serif' ;letter spacing='2px';>" .$test['category']."</font></font><font color='skyblue'; font size='2px !important' ;font family='Century Gothic, sans-serif' ;letter spacing='2px';>   |   </font><font color='black'; font size='2px !important' ;font family='Century Gothic, sans-serif' ;letter spacing='2px';>Size:   </font><font color='gray'; font size='2px !important' ;font family='Century Gothic, sans-serif' ;letter spacing='2px';>" .$test['file_size']."</font></font><font color='skyblue'; font size='2px !important' ;font family='Century Gothic, sans-serif' ;letter spacing='2px';>   |   </font><font color='black'; font size='2px !important' ;font family='Century Gothic, sans-serif' ;letter spacing='2px';>Credits to:   </font><font color='gray'; font size='2px !important' ;font family='Century Gothic, sans-serif' ;letter spacing='2px';>" .$test['author']."</font><font color='skyblue'; font size='2px !important' ;font family='Century Gothic, sans-serif' ;letter spacing='2px';>   |   </font><font color='black'; font size='2px !important' ;font family='Century Gothic, sans-serif' ;letter spacing='2px';>Date Uploaded:   </font><font color='gray'; font size='2px !important' ;font family='Century Gothic, sans-serif' ;letter spacing='2px';>" .$test['date_upload']."</font><br>";
                    echo"<font color='black'; font size='2px !important' ;>Description:". $test['description']. "</font><br>";
                    echo"<a href ='download.php?file_path=$fileid' ><font color='orange' title='$mycat'>Download File</p></a>";
            echo        "</div>";

}
    //BOTTOM
    if ($numrows==0)
    {

    }
    else
    {
        echo paginate($reload, $page, $total_pages, $adjacents);
    }

    //header('Location:index.php');
    //die();

} // end of ajax first condition
else
{

?>
<body>

<?php
 echo 'You are searching for: '.$mycat;
?>

</body>

<?php } ?>

【问题讨论】:

  • 请求此文件的 URL 看起来如何?
  • 看不到 whare $mycat 被传递到 paginate() ,如果您尝试在 paginate 函数中访问 $mycat 将超出范围
  • 回显输出变为未定义投影到页面...这意味着变量未被读取..但是当我用正文回显它时...我得到的值为照片/图像。 ..我希望这个值出现在我上面的查询中......
  • charlietfl 我只是放置它,但它没有读取它...
  • 在我的 index.php 中我得到这样的值
  • 照片和图像"; @Joren

标签: php jquery mysql ajax pagination


【解决方案1】:

正如我对 ajax 的理解,你可以得到变量,但是刷新时你不能正确吗?如果我是真的,你应该把它存储在 session 中,然后先查看 get/request,然后查看 session。

放轻松

【讨论】:

  • 嗨..我尝试了你刚才的建议..是的!!!!最后我通过会话获得了查询,但我的问题是如何在不使用破坏的情况下取消设置会话。我只是想在返回 index.php 时取消设置会话,因为当我再次返回时将值更改为“音频/视频” " 但它不会改变旧的会话值,即 "照片/图像" 请帮助我...我即将解决我的问题...
  • 我在 cat_index.php 之上的会话声明就像这个 session_start(); $mycat =$_GET['类别']; if(!isset($_SESSION['cid'])) { $_SESSION['cid']=$mycat; } $mycat=$_SESSION['cid'];
  • 我不建议为此进行会话。如果你不知道如何正确使用它们,你最终会得到奇怪的结果。
  • 它无法更改,因为您在案例中使用它,但您应该在另一个案例中使用它(declaration)。首先,您应该检查 $_GET,然后如果不在 $_GET 上,则应该查看会话。如果在 get 上,你应该在这种情况下设置 if(isset($_GET['cid'])) { $mycat = $_GET['cid']; $_SESSION['cid'] = $mycat; }else{ $mycat = $_SESSION['cid']; }
【解决方案2】:

如果 $_REQUEST 允许您访问参数,但 $_GET 不允许,请尝试 $_POST,这意味着使用的 HTTP 方法是 POST 而不是 GET。检查提交给这个脚本的表单/javascript。 $_REQUEST 是一个包含 get 和 post 参数(还有 cookie!)的包罗万象的超全局变量。

$_REQUEST

我还建议深入阅读 HTTP 协议,它可以帮助解决这些问题:)

除了 Joren 刚刚评论的内容之外,请务必包含您用于访问此页面的 URL。

【讨论】:

  • 除了我上面的回答,请不要使用mysql_* apis,而是使用mysqli和PDO。 mysql_* 已弃用。此外,您的 SQL 并不安全。如果您绝对必须使用 mysql api,则使用 mysql_real_escape_string 而不是 trim() 和 htmlspecialchars()。尝试转换为 mysqli/PDO 并改用准备好的语句。
猜你喜欢
相关资源
最近更新 更多
热门标签