gpt4 book ai didi

php - 如何在数据库中保存页面 View

转载 作者:搜寻专家 更新时间:2023-10-30 23:02:17 40 4
gpt4 key购买 nike

我有一个页面,我在其中向用户显示 50 张图片,用户可以单击该图片并在另一个页面中仅加载该图片。现在我想将每个 ImageView 保存到数据库中,即点击图像 ID 的计数器。所以到目前为止我所做的是将另一行添加到表中,我称之为 hits。我在这里展示图片:

$result = $pdo->prepare("SELECT * FROM images ORDER BY id ASC LIMIT $start_from, 50");
$result->execute();
for($i=0; $row = $result->fetch(); $i++)
{
echo '
<h1><a href="post.php?id='.$row['id'].'">'.$row['title'].'</a></h1>
<img src="../upload/'.$row['name'].'" alt=""/>';
}

post.php 中,用户只能看到他点击的这张图片,我有这个:

if(isset($_GET['id']) && is_numeric($_GET['id']))
{
$id = $_GET['id'];
$result = $pdo->prepare("SELECT * FROM images WHERE id= ?");
if ($result->execute(array($_GET['id'])))
{
for($i=0; $row = $result->fetch(); $i++)
{
echo '
// source to show that image
';
}
}
}

据我所知,我必须用类似这样的内容更新 hits

UPDATE images SET hits = hits + 1 WHERE id = idofimage

我无法理解的是如何在当前代码中实现更新。还有哪里?

感谢任何帮助。

最佳答案

你几乎已经解决了所有问题

只需在显示图像的同一位置添加更新代码

if(isset($_GET['id']) && is_numeric($_GET['id']))
{
$id = $_GET['id'];
$result = $pdo->prepare("SELECT * FROM images WHERE id= ?");
if ($result->execute(array($_GET['id'])))
{
//replace for by if because we are only getting one result
if($row = $result->fetch())
{
//update the hits
$update = $pdo->prepare("UPDATE images SET hits = hits + 1 WHERE id = ?");
//I prefer binding values and have a call to execute()
//but both options are ok
//$update->bindParam(1, $_GET['id'], PDO::PARAM_INT);
//$update->execute();
$update->execute(array($_GET['id']));

echo '
// source to show that image
';
}
}
}

关于php - 如何在数据库中保存页面 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30757866/

40 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com