php代码显示点击跟踪器无法正常工作

时间:2013-04-06 21:25:26

标签: php

我有这个点击跟踪的PHP代码,可以在我的网站上显示:

<?php
if (!file_exists("config.php")) {
    header("Location: installer");
}

require_once("config.php");

//Connect to database
@$con = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if (!$con) {
    die("Error: Could not connect to database (" . mysql_error() . "). Check your database settings are correct.");
}

//Check database exists
$does_db_exist = mysql_select_db(DB_NAME, $con);
if (!$does_db_exist) {
    die("Error: Database does not exist (" . mysql_error() . "). Check your database  settings are correct.");
}

if (isset($_GET["id"])) {
    $id = mysql_real_escape_string($_GET["id"]);
} else {
    die("Error: ID cannot be blank.");
}

//If ID exists, show count or else die
$showinfo = mysql_query("SELECT count FROM Data WHERE id = \"$id\"");
$showresult = mysql_fetch_assoc($showinfo);
if ($showresult != 0) {
    echo $showresult["count"];
} else {
    die("Error: ID does not exist.");
}

mysql_close($con);

?>

我想通过在HTML中插入此代码来显示使用此给定代码下载文件的次数,以便显示已下载的次数,但它无效:

<?php
$_GET["id"] = "download1";
include("indication/display.php");
?>

我将该代码放在我的HTML中,但它没有显示数字

2 个答案:

答案 0 :(得分:1)

1)计算行数:

$showinfo = mysql_query("SELECT COUNT(count) FROM Data WHERE id ='$id'");
$showresult = mysql_num_rows($showinfo);
if ($showresult !== 0) {

2)我认为你有一个下载链接,就像那样:

echo '<a href=index.php?id=download>Download</a>';

3)我看不到UPDATE数据库(每次点击下载时添加一个值)

if (isset($_GET["id"])) {
$id = mysql_real_escape_string($_GET["id"]);
$showinfo = mysql_query("UPDATE Data SET id = '$id' + 1 ");

答案 1 :(得分:0)

将您的查询更改为

'SELECT COUNT(1) FROM Data WHERE id = '.intval($id)

这比COUNT(id)快,非常轻微。

正如大家所说,请使用PDO。