【问题标题】:Increment a counter in MySQL every time a product item is viewed每次查看产品项目时在 MySQL 中增加一个计数器
【发布时间】:2015-04-22 21:34:12
【问题描述】:

我的网站中有一个产品页面,该页面使用 MySQL 数据库,当点击缩略图时,产品项目会在页面上创建。

有什么方法可以在每次点击缩略图时增加 MySQL 计数器?

如果有办法做到这一点,有人可以为我指出正确的编码方向吗?

注意:此代码有效,但此更新所有产品命中计数。我只需要更新表中查看的产品项目行

我的mysql表是这样的

表名:图片

标识 |产品名称 |图片 |价格 |命中

这是我的 php 代码:


<?php
$connection = mysql_connect("localhost", "root", ""); // Establishing Connection with Server
$db = mysql_select_db("user"); // Selecting Database

$result=mysql_query("SELECT id,image,SUBSTRING_INDEX(product_name,' ',4) as product_name FROM images");

mysql_query("update images set hits = hits+1 where product_name = product_name");

$count = 0;

while($res=mysql_fetch_array($result))
        {
if($count==3) //three images per row
            {
print "</tr>";
$count = 0;
if($count==0)
print "<tr>";
print "<td>";

?>
<?php echo"<div>";?>

<?php echo"<img style='heigth:170px; width:200px'src='image1.php?id=$res[id]'>"?>
<?php echo "<a href=\"getImage.php?id={$res['id']}\">{$res['product_name']}<h3>Read More</h3></a>";?>

<?php echo"</div>";?>

<?php
$count++;
print "
</td>";

}

if($count>0)
print "</tr>";

?>

这里是getImage.php代码`


<!DOCTYPE HTML>
<HTML>
<HEAD>

<style>
img.floatLeft 
{ 
float: left; 
margin-right: 10px;
margin-bottom: 1px; 
padding-left: 2%;
}
p.padding {
padding-left: 2%;
padding-right: 2%;
}

h3 { color:#3300FF; font-family: Slab,Georgia,serif;font-size: 20px;
  letter-spacing: .03em;text-transform: none}

div{background-color: white; border-style: ridge; height: 500px; width: 400px; margin-left:auto;
margin-right:auto;  color: #000000; font: 12px arial, sans-serif; line-height: 18px;text-align: justify;}

</style>
</HEAD>
<body>

<?php
if (isset($_GET['id'])) {
$id = $_GET['id'];

$connection = mysql_connect("localhost", "root", ""); // Establishing Connection with Server
$db = mysql_select_db("user", $connection); // Selecting Database


$query1 = mysql_query("select * from images where id=$id", $connection);




echo"<div >";

while ($row1 = mysql_fetch_array($query1)) {
echo"<h3 align='center' color='red'>";
echo $row1['product_name'];
echo"</h3>";


echo "<img class='floatLeft'src=image1.php?id=".$row1['id']." width=150 height=140  />";


echo'<p class="padding">';
echo $row1['price'];
echo"</p>";
echo"</div>";
echo"</div>";



?>

<?php
}
}
?>

</body>
</html>

【问题讨论】:

  • 更新它,其中 ID 等于他们所在页面的特定 ID。此外,您不需要在每次输出后关闭 php,整个脚本可以在一个 PHP 块中。
  • 感谢您的回答...但我不想只计算 1 个 ID...我的数据库中有很多产品项目,每个项目都有 ID。所以只希望查看的产品项目被计入数据库。
  • 当用户点击产品图片时会发生什么?他们会被重定向到产品页面吗?
  • 这个页面是getImage.php吗?

标签: php mysql


【解决方案1】:

您没有将任何变量传递给 UPDATE 语句。检查mysql_querymysql_fetch_array

这样的事情可能会奏效

$result=mysql_query("SELECT id,image,SUBSTRING_INDEX(product_name,' ',4) as product_name FROM images");
$row = mysql_fetch_array($result)
mysql_query("update images set hits = hits+1 where product_name = " . $row['product_name']);

另请注意,mysql_query 不是从 PHP 访问 MySQL 数据库的推荐方式 - 请参阅 Choosing a MySQL API

【讨论】:

    【解决方案2】:

    好的,所以/product/product.php 在那里取出更新,因为您不想更新每个视图计数,删除

    mysql_query("update images set hits = hits+1 where product_name = product_name");
    

    然后在details.php页面添加

    if(!empty($_GET['id'])) {
        $id = (int)$_GET['id']; //the (int) forces this to be a number so you cant be injected
        mysql_query("update images set hits = hits+1 where id = $id");
    }
    

    您可能还想查看if ($count == 3) 的模运算符。您还应该改用 mysqli 或 PDO 驱动程序。

    【讨论】:

    • 该页面上的文本显示“查看详细信息”和指向 details.php 的链接。那是对的吗?如果是这样,getImage.php 在哪里使用或者是旧名称?
    • YES....我昨晚一直在努力但仍然没有完成任务....这就是为什么我修改了脚本以便更好地理解...
    • 我要给你新脚本吗?
    • 我已经发布了您应该对脚本进行的更新。
    • 干得好!您的代码运行良好...非常感谢老兄
    【解决方案3】:
    <?php
    session_start();
    
    if (!isset($_SESSION['views'])) { 
        $_SESSION['views'] = 0;
    }
    
    $_SESSION['views'] = $_SESSION['views']+1;
    
    $connection = mysql_connect("localhost", "root", ""); // Establishing Connection with Server
    $db = mysql_select_db("user"); // Selecting Database
    
    $result=mysql_query("SELECT id,image,SUBSTRING_INDEX(product_name,' ',4) as product_name FROM images");
    
    mysql_query("update images set hits = ".$_SESSION['views']." where product_name = product_name");
    
    $count = 0;
    
    while($res=mysql_fetch_array($result))
            {
    if($count==3) //three images per row
                {
    print "</tr>";
    $count = 0;
    if($count==0)
    print "<tr>";
    print "<td>";
    
    ?>
    <?php echo"<div>";?>
    
    <?php echo"<img style='heigth:170px; width:200px'src='image1.php?id=$res[id]'>"?>
    <?php echo "<a href=\"getImage.php?id={$res['id']}\">{$res['product_name']}<h3>Read More</h3></a>";?>
    
    <?php echo"</div>";?>
    
    <?php
    $count++;
    print "
    </td>";
    
    }
    
    if($count>0)
    print "</tr>";
    

    【讨论】:

    • 这段代码显示了这个错误...'Parse error: parse error, expecting `']'' in C:\xampp\htdocs\link\products.php on line 32'
    • 可能是单引号导致我浏览后遗漏],现在更正。
    • @SafinChowdhury - 您还必须在 hits 列中有一个值,即“0(零)”,因为将执行更新查询,并且必须存在一些现有值。
    • 当用户点击查看详细信息时。他们被重定向到产品页面?
    【解决方案4】:

    只需在代码末尾添加一行代码(如下所示)用于获取特定记录(在关闭连接之前)

    代码是:

    $result1 = $conn->query("UPDATE REPLACE_WITH_TABLE_NAME SET views = views + 1 WHERE REPLACE_WITH_CONDITION");
    

    【讨论】:

      猜你喜欢
      • 2020-11-01
      • 2019-06-09
      • 1970-01-01
      • 2019-02-26
      • 2020-01-03
      • 1970-01-01
      • 1970-01-01
      • 2016-02-06
      • 1970-01-01
      相关资源
      最近更新 更多