0

我在修改 php 应用程序以进行分页时遇到问题。我的错误似乎与我的逻辑有关,我不清楚我做错了什么。我以前遇到过,但目前没有收到 mysql_num_rows() 不是有效的结果资源以及向 foreach 提供了无效参数的错误。我认为我的逻辑存在问题,即阻止返回 mysql 的结果。

除了测试while循环外,我所有的“测试”回声都被输出。生成一个页面,其中包含查询名称和单词拍卖,以及第一个和上一个链接,但没有下一个和最后一个链接。如果可以指出一种为表中的行生成链接的更有效方法,而不是为每个单元格创建链接,我将不胜感激。是否可以为多个项目建立连续链接?

<?php
if (isset($_GET["cmd"]))
  $cmd = $_GET["cmd"]; else
die("You should have a 'cmd' parameter in your URL");
$query ='';
if (isset($_GET["query"])) {
    $query = $_GET["query"];
}
if (isset($_GET["pg"]))
{ 
 $pg = $_GET["pg"];
 }
  else $pg = 1;
$con = mysql_connect("localhost","user","password");
echo "test connection<p>";
if(!$con) {
    die('Connection failed because of' .mysql_error());
}
mysql_query('SET NAMES utf8');
mysql_select_db("database",$con);
if($cmd=="GetRecordSet"){
    echo "test in loop<p>"; 
    $table = 'SaleS';
    $page_rows = 10;
    $max = 'limit ' .($pg - 1) * $page_rows .',' .$page_rows;
    $rows = getRowsByProductSearch($query, $table, $max);
    echo "test after query<p>";
    $numRows = mysql_num_rows($rows);
    $last = ceil($rows/$page_rows);
    if ($pg < 1) {
        $pg = 1;
    } elseif ($pg > $last) {
        $pg = $last;
    }
    echo 'html stuff <p>';

    foreach ($rows as $row) {

echo "test foreach <p>";
        $pk = $row['Product_NO'];
        echo '<tr>' . "\n";
        echo '<td><a href="#" onclick="updateByPk(\'Layer2\', \'' . $pk . '\')">'.$row['USERNAME'].'</a></td>' . "\n";
        echo '<td><a href="#" onclick="updateByPk(\'Layer2\', \'' . $pk . '\')">'.$row['shortDate'].'</a></td>' . "\n";
        echo '<td><a href="#" onclick="updateByPk(\'Layer2\', \'' . $pk . '\')">'.$row['Product_NAME'].'</a></td>' . "\n";
        echo '</tr>' . "\n";
    }
    if ($pg == 1) {
    } else {
        echo " <a href='{$_SERVER['PHP_SELF']}?pg=1'> <<-First</a> ";
        echo " ";
        $previous = $pg-1;
        echo " <a href='{$_SERVER['PHP_SELF']}?pg=$previous'> <-Previous</a> ";
    }
    echo "---------------------------";
    if ($pg == $last) {
    } else {
        $next = $pg+1;
        echo " <a href='{$_SERVER['PHP_SELF']}?pg=$next'>Next -></a> ";
        echo " ";
        echo " <a href='{$_SERVER['PHP_SELF']}?pg=$last'>Last ->></a> ";
    }
    echo "</table>\n";
}
echo "</div>";
function getRowsByProductSearch($searchString, $table, $max) {
    $searchString = mysql_real_escape_string($searchString);
    $result = mysql_query("SELECT Product_NO, USERNAME, ACCESSSTARTS, Product_NAME, date_format(mycolumn, '%d %m %Y') as shortDate FROM {$table} WHERE upper(Product_NAME) LIKE '%" . $searchString . "%'" . $max);
    if($result === false) {
        echo mysql_error();
    }
    $rows = array();
    while($row = mysql_fetch_assoc($result)) {
        echo "test while <p>";
        $rows[] = $row;
    }
    return $rows;
    mysql_free_result($result);
}

编辑:我已经打印出没有的mysql错误。但是,从包含 100 多条记录的数据库中打印出 8 个“测试时间”。从未进入 foreach 循环,我不确定为什么。

4

4 回答 4

1

如果 (!(isset($pg))) { $pg = 1; }

$pg 将如何设置?您似乎没有从 $_GET 中读取它。如果你依赖于 register_globals:不要那样做!尝试从 $_GET 读取它并将其解析为正整数,如果失败则回退到 '1'。

<a href='{$_SERVER['PHP_SELF']}?pg=$next'>下一个-></a>

您似乎正在丢失页面所需的其他参数,“查询”和“cmd”。

一般来说,我发现阅读您的代码非常困难,尤其是 echo() 的无缩进使用。此外,每次您“...$template...”或将字符串连接到 HTML 时,您都会遇到数不清的 HTML/脚本注入漏洞,而无需 htmlspecialchars() 处理它。

PHP 是一种模板语言:使用它,不要对抗它!例如:

<?php
    // Define this to allow us to output HTML-escaped strings painlessly
    //
    function h($s) {
        echo(htmlspecialchars($s), ENT_QUOTES);
    }

    // Get path to self with parameters other than page number
    //
    $myurl= $_SERVER['PHP_SELF'].'?cmd='.urlencode($cmd).'&query='.urlencode($query);
?>

<div id="tableheader" class="tableheader">
    <h1><?php h($query) ?> Sales</h1>
</div>
<div id="tablecontent" class="tablecontent">
    <table border="0" width="100%"> <!-- width, border, cell width maybe better done in CSS -->
        <tr>
            <td width="15%">Seller ID</td>
            <td width="10%">Start Date</td>
            <td width="75%">Description</td>
        </tr>
        <?php foreach ($rows as $row) { ?>
            <tr id="row-<?php h($row['Product_NO']) ?>" onclick="updateByPk('Layer2', this.id.split('-')[1]);">
                <td><?php h($row['USERNAME']); ?></td>
                <td><?php h($row['shortDate']); ?></td>
                <td><?php h($row['Product_NAME']); ?></td>
            </tr>
        <?php } ?>
    </table>
</div>
<div class="pagercontrols">
    <?php if ($pg>1) ?>
        <a href="<?php h($myurl) ?>&amp;pg=1"> &lt;&lt;- First </a>
    <?php } ?>
    <?php if ($pg>2) ?>
        <a href="<?php h($myurl) ?>&amp;pg=<?php h($pg-1) ?>"> &lt;-- Previous </a>
    <?php } ?>
    <?php if ($pg<$last-1) ?>
        <a href="<?php h($myurl) ?>&amp;pg=<?php h($pg+1) ?>"> Next --> </a>
    <?php } ?>
    <?php if ($pg<$last) ?>
        <a href="<?php h($myurl) ?>&amp;pg=<?php h($last) ?>"> Last ->> </a>
    <?php } ?>
</div>

是否可以为多个项目建立连续链接?

跨细胞,没有。但无论如何,你并没有真正使用链接——那些“#”锚不会去任何地方。上面的示例将 onclick 放在表格行上。究竟什么更适合可访问性取决于您的应用程序到底想要做什么。

(上面还假设 PK 实际上是数字,因为其他字符可能无法放入“id”。您可能还需要考虑删除内联“onclick”并将代码移动到下面的脚本 - 请参阅“unobtrusive脚本”。)

于 2009-02-02T13:25:28.457 回答
1

问题(或至少其中一个)出在以下代码中:

$rows = getRowsByProductSearch($query, $table, $max);
$numRows = mysql_num_rows($rows);

$numRows 变量不是 MySQL 结果集,它只是 getRowsByProductSearch 返回的普通数组。

将代码更改为:

$rows = getRowsByProductSearch($query, $table, $max);
$numRows = count($rows);

那么它至少应该为你找到一些结果。

祝你好运,詹姆斯

你好呀,

下一个问题是如下行:

$last = ceil($rows/$page_rows);

应改为:

$last = ceil($numRows / $page_rows);

建议至少在调试时将以下行添加到脚本的开头:

ini_set('error_reporting', E_ALL | E_STRICT);
ini_set('display_errors', 'On');

因为那会引发致命错误并为您节省大量时间。

于 2009-02-02T19:22:15.460 回答
0

这是错误的:

if($cmd=="GetRecordSet")
echo "test in loop\n"; {

它应该是:

if($cmd=="GetRecordSet") {
    echo "test in loop\n";
于 2009-02-02T13:12:37.873 回答
0

在您的getRowsByProductSearch函数中,您返回mysql_error是否发生的结果。为了调试代码,也许您可​​以将其打印出来,这样您就可以轻松查看问题所在。

于 2009-02-02T13:29:36.110 回答