PHP 在页面刷新时进行循环复制

PHP for loop duplicating on page refresh

我有一个二维数组,我在 for 循环中显示它。代码在这里:

foreach($products as $id => $product) {
        echo "<tr>
            <td style='border-bottom:1px solid #000000;'><a href='./index.php?view_product=$id'>" . $product['Book_Code'] . "</a></td>
            <td style='border-bottom:1px solid #000000;'>$" . $product['Title'] . "</td> 
            <td style='border-bottom:1px solid #000000;'>" . $product['Author'] . "</td>
        </tr>";
    }
    echo "</table>";
}

我的二维数组中目前有两个项目(最终会有 20 个)。当我加载页面时,我的 table 正确显示了两个项目,#1 和 #2。如果我刷新页面 #1 仍然存在,但也在下面重复,所以 table 看起来像

#1
#2
#1
#2

每次刷新时都会一次又一次地发生这种情况。每次打开新页面时如何重置它?我的 table 目前有 26 个项目并且还在增加,因此很难测试我的代码的其余部分。

编辑:

我正在这样填充我的数据库:

$db = sqlite_open ("products.db", 0666, $error);

@sqlite_query($db,"CREATE TABLE Books (Book_Code integer PRIMARY KEY, Author varchar(20), Title varchar(20), Brief_Synopsis varchar2(100), ISBN_Number integer, Publisher varchar(20), imgNumber integer)",$sqliteerror);


sqlite_query($db,"INSERT INTO Books (Author, Title, Brief_Synopsis, ISBN_Number, Publisher, imgNumber) VALUES ( 'Robin Nixon', 'Learning PHP, MySQL & JavaScript: With jQuery, CSS & HTML5','Build interactive data-driven websites with the potent combination of open-source technologies and web standards', 9781491918661, 'O&#39Reilly', '001')");
sqlite_query($db,"INSERT INTO Books (Author, Title, Brief_Synopsis, ISBN_Number, Publisher, imgNumber) VALUES ( 'Reiersol et al', 'PHP in action','This book takes on the most important challenges of web programming in PHP', 9781932394757, 'Greenwich', '002')");




$result=sqlite_query($db,"SELECT * from Books");
$products = array();
while($row=sqlite_fetch_array($result,SQLITE_ASSOC))
{
    $products[] = $row;
}

sqlite_close($db);

答案很简单。

删除行:

@sqlite_query($db,"CREATE TABLE Books (Book_Code integer PRIMARY KEY, Author varchar(20), Title varchar(20), Brief_Synopsis varchar2(100), ISBN_Number integer, Publisher varchar(20), imgNumber integer)",$sqliteerror);


sqlite_query($db,"INSERT INTO Books (Author, Title, Brief_Synopsis, ISBN_Number, Publisher, imgNumber) VALUES ( 'Robin Nixon', 'Learning PHP, MySQL & JavaScript: With jQuery, CSS & HTML5','Build interactive data-driven websites with the potent combination of open-source technologies and web standards', 9781491918661, 'O&#39Reilly', '001')");
sqlite_query($db,"INSERT INTO Books (Author, Title, Brief_Synopsis, ISBN_Number, Publisher, imgNumber) VALUES ( 'Reiersol et al', 'PHP in action','This book takes on the most important challenges of web programming in PHP', 9781932394757, 'Greenwich', '002')");

或评论它们以供将来使用。您插入数据库的每个请求。 create table 前面的 @ 符号会抑制您得到的 table Books 已经存在的错误。所以 table 不会被创建 'again' 并且记录仍然会插入

您还可以将整个块封装在检查 table 是否存在的过程中。如果它不存在,则创建一个包含 2 行的填充,以便您可以测试。

如何检查 table 是否存在

$tableCheck = sqlite_array_query($db, "SELECT name FROM sqlite_master WHERE type='table' AND name='Books'");

if(1 > count($tableCheck)) {

     sqlite_query($db,"CREATE TABLE Books (Book_Code integer PRIMARY KEY, Author varchar(20), Title varchar(20), Brief_Synopsis varchar2(100), ISBN_Number integer, Publisher varchar(20), imgNumber integer)",$sqliteerror);

    sqlite_query($db,"INSERT INTO Books (Author, Title, Brief_Synopsis, ISBN_Number, Publisher, imgNumber) VALUES ( 'Robin Nixon', 'Learning PHP, MySQL & JavaScript: With jQuery, CSS & HTML5','Build interactive data-driven websites with the potent combination of open-source technologies and web standards', 9781491918661, 'O&#39Reilly', '001')");
    sqlite_query($db,"INSERT INTO Books (Author, Title, Brief_Synopsis, ISBN_Number, Publisher, imgNumber) VALUES ( 'Reiersol et al', 'PHP in action','This book takes on the most important challenges of web programming in PHP', 9781932394757, 'Greenwich', '002')");
}