问题
I have a page that displays content from different database tables. Each of the sections has a link in it to lead to a page (view_article.php, for example) where that content is displayed with its' comment section below it. The link in question being;
'<a href="'.$site.'/views/track.php?id='.$row['article_id'].'">
<font face="arial black" color="#0000CC" size="5">
Click Here For Full Story
</font>
</a>
<hr>';
I know there's something I'm completely missing as this link works correctly for me as it's supposed to. This links to the track.php page which updates the clicks column (and in PDO form as an extra answer I contributed at PHP: How to increment a value in the table row to count views and to limit counts to one IP address). My Googlethons and Documentation were a bit fruitless (although, I thought a user-contributed motif was the closest to my current predicament - by dyukemedia). I just can't get the correct article text to sync up to the article id - the article id is correct in each URL access;
example.com/views/view_article.php?id=4
(as an example article URL shown in the address bar) - but my problem is that the FIRST row of article text (i.e. id=1) is the only one being pulled from the database. For the life of me, I haven't been able to retrace my steps to a suggestion that I found here about (possibly?) that ensures that the correct article text is called relative to the article id. Even as I type this question, I've got about six tabs of supplemental reading going on simultaneously (lol!). Gotta git 'er done! Thanks for any help or pointers in advance.
Further Information
I apologize for not providing the query information earlier;
$stmt = $pdo->prepare("
SELECT article_id, name, title, category, clicks
FROM articles
WHERE is_published = TRUE
ORDER BY publish_date DESC
");
$stmt->bindValue(':is_published', 1);
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
And this is the Table structure for table articles
`article_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`user_id` int(10) unsigned NOT NULL,
`name` varchar(100) NOT NULL,
`is_published` tinyint(1) NOT NULL DEFAULT '0',
`submit_date` datetime NOT NULL,
`publish_date` datetime DEFAULT NULL,
`title` varchar(255) NOT NULL,
`article_text` mediumtext,
`category` varchar(10) NOT NULL,
`clicks` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`article_id`),
KEY `user_id` (`user_id`,`submit_date`),
FULLTEXT KEY `title` (`title`,`article_text`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=6 ;
To clarify: the 1st link example is a pass-through to a file named track.php which records how many individual clicks-throughs there are on a particular content title via article id. From track.php, the viewer is supposed to be taken to the content article via;
header('Location: view_article.php?id='.$_GET['id']);
exit();
Even with an experiment where I substituted $article_id for $_GET['id'], on each link hover, the correct article id was displayed, and, when selected, the correct article id showed up in the address bar. Only the article content text was identical in all three cases currently on the site home page (called by the query listed above).
UPDATE 1
Had to change the main article navigation link back to;
<a href="'.$site.'/views/track.php?view_article.php&id='.$row['article_id'].'">
as all subsequent links after the second article id stopped updating the clicks column. So, now I'm still stuck in the situation of being able to update the link clicks to track the popularity of a certain article content piece, but unable to have the link connect to the correct article text. I'm always able to get the correct article id in the URL address bar;
www.example.com/views/view_article.php?id=4
And even with multiple tweaklings of the track.php code, I'm still at gridlock. Can anyone see where I'm going wrong? I'll include any other necessary info requested.
回答1:
Thanks to @dan08 and @JiFus for cluing me in on the fact that it was INDEED the SELECT query that was stuffing it all up for me - too bad it took almost a day-and-a-half to ascertain that I should've been looking at the query on the view_article.php page;
$sql = "SELECT name, title, category, publish_date, clicks, article_text
FROM articles
WHERE is_published = TRUE
ORDER BY publish_date DESC LIMIT 1";
$result = $pdo->query($sql);
!=
$stmt = $pdo->prepare("
SELECT
article_id, name, title, category, clicks, article_text, publish_date
FROM
articles
WHERE
article_id = :article_id
AND
is_published = TRUE
ORDER BY
publish_date DESC
LIMIT 1
");
$stmt->execute(array(':article_id' => $_GET['id']));
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
I was able to change the link back to;
<a href="'.$site.'/views/track.php?id='.$row['article_id'].'">
since in track.php the header() sends the viewer to the particular article id text URL ;
header('Location: view_article.php?id='.intval($_GET['id']));
Now this fixes the link navigational abilities across all of the content sections I have to employ it in. Helps to find the problem in the right place. OF COURSE I'm not going to get the correct article id text if the statement isn't able to execute. GRRR...Human Error Strikes Again... (lol!)
来源:https://stackoverflow.com/questions/31483025/link-going-through-tracking-script-pulls-correct-article-id-incorrect-article-t