0
$pagename = 'index.html';
$dbh = connectDb();
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $dbh->query("select 'tcount' from accessNum where pagename = '$pagename'");
$record = $stmt->fetchAll();

if (sizeof($stmt) != 0) {
    $counter = $record['tcount'];
    $counter++;
    $dbh->exec("update accessNum set tcount = '$counter' where pagename = '$pagename'");
}
mysql (table name is accessNum) is
pagename(varchar(255)) = index.html,
today(date) = 2015-08-31,
tcount(int(11)) = 1,
totalcount(int(11)) = 1,

the tcount keeps 1 every update. why it occurs?

gen_Eric
  • 223,194
  • 41
  • 299
  • 337
Matha
  • 41
  • 1
  • 4

3 Answers3

0

If you work with int you don't need quotes on variable like this

if (sizeof($stmt) != 0) {
    $counter = $record['tcount'];
    $counter++;
    $dbh->exec("update accessNum set tcount = $counter where pagename = '$pagename'");
}
bicho
  • 134
  • 1
  • 11
0

You do not need quotes around a column name in a SELECT query

$stmt = $dbh->query("select tcount from accessNum where pagename = '$pagename'");

Also, you do not need quotes around a number in an UPDATE query

$dbh->exec("update accessNum set tcount = $counter where pagename = '$pagename'");
MaggsWeb
  • 3,018
  • 1
  • 13
  • 23
0

Always escape column and table names with backticks, and values with quotes. They're quite different in MySQL. This should work:

$pagename = 'index.html';
$dbh = connectDb();
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $dbh->query("select `tcount` from `accessNum` where `pagename` = '$pagename'");
$record = $stmt->fetchAll();

if (sizeof($stmt) != 0) {
    $counter = $record['tcount'];
    $counter++;
    $dbh->exec("update `accessNum` set `tcount` = '$counter' where `pagename` = '$pagename'");
}
Lucian D.
  • 95
  • 5