每次查看产品项时,都会在MySQL中增加一个计数器

时间:2015-04-22 21:34:13

标签: php mysql

我的网站上有一个产品页面,可以在MySQL数据库中运行,在点击缩略图时会在页面上创建产品项目。

每次点击缩略图时,有没有办法增加MySQL计数器?

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

注意:此代码正常运行,但此更新所有产品都会计入。我需要更新表中仅查看过的产品项

我的mysql表看起来像

表名:图片

id | product_name |图片|价格|命中

这是我的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 code`

<!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>

4 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

<?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>";

答案 2 :(得分:0)

好的,deletedObjects取消了那里的更新,因为你不想更新每个观看次数,删除

changedValues

然后在/product/product.php页面上添加

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

您还可以查看details.php的模数运算符。您还应切换到使用mysqli或PDO驱动程序。

答案 3 :(得分:0)

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

代码是:

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