我有一个显示数据库记录的php脚本。它可能不是最好的脚本,因为我对php很新。
我已经在我的表格中添加了一个额外的列,并希望在该列中保留一个计数,以显示每个记录的查看次数。
继承代码的一部分,我认为我需要添加代码...如果我需要发布整个页面,我会,但我只是想我可以添加到这部分的行。
//Get the details from previous page
$SelectedCounty = $_POST["result"];
//set variable for next SEARCH
$option = '';
// Get the county names from database - no duplicates - Order A-Z
$query = "SELECT DISTINCT tradingCounty FROM offers ORDER BY tradingCounty ASC";
// execute the query, $result will hold all of the Counties in an array
$result = mysqli_query($con,$query);
while($row = mysqli_fetch_array($result)) {
$option .="<option>" . $row['tradingCounty'] . "</option>";
}
}
新列名称是&#39; views&#39;每次查看数据库中的记录时,我只想添加1。
任何帮助都非常感激。
答案 0 :(得分:5)
向表中添加新字段views
。
当用户查看页面时,触发SQL。
$query = "UPDATE offers SET views = views + 1";
答案 1 :(得分:2)
mysqli_query($con,"update offers set views = views + 1");
答案 2 :(得分:0)
如果您添加了列,则其值可能为NULL
。通过执行以下操作将值设置为0
update offers
set views = 0;
或使用:
update offers
set views = coalesce(views, 0) + 1;
答案 3 :(得分:0)
您可以使用此重写代码更改代码,假设您的表具有列视图(数据类型为int)。
//Get the details from previous page
$SelectedCounty = $_POST["result"];
//set variable for next SEARCH
$option = '';
// Get the county names from database - no duplicates - Order A-Z
$query = "SELECT DISTINCT tradingCounty FROM offers ORDER BY tradingCounty ASC";
// execute the query, $result will hold all of the Counties in an array
$result = mysqli_query($con,$query);
if($result){
$query2 = "UPDATE offers SET views=views+1;
mysqli_query($con,$query2);
}
while($row = mysqli_fetch_array($result)) {
$option .="<option>" . $row['tradingCounty'] . "</option>";
}
或者,如果您需要跟踪单个记录的视图计数,则需要稍微修改一下代码。也许你需要在数据库中再添加一个字段,例如。 id(datatype int),可以区分不同的记录。
请妥善清除您的问题。
答案 4 :(得分:0)
据我分析您的代码,它会显示以下情况。
对于tradingConty有不同的记录,每当用户通过单击该指针或任何其他指定的操作查看该特定记录(transactionCounty记录之一)时,php脚本将设置为增加该特定条目的查看计数(我们可以通过id)在数据库中获得。
如果是那个场景,我们可以相应地轻松生成代码。