Hrvoje Posted April 30, 2014 Share Posted April 30, 2014 Hi there, I have simple code with echo link on external page. <?PHP $sql="SELECT * FROM links WHERE link='external'"; if (!$q=mysql_query($sql)) { echo "Error" . mysql_query(); die(); } if (mysql_num_rows($q)==0) { echo "No data"; } else { while ($row=mysql_fetch_array($q)) { ?> <a href="<?PHP echo $row["link"];?>"><?PHP echo $row["link"];?></a> <?PHP } } ?> In database I have field clicks, how to increase that field on every click for 1. Thanks, Hrvoje Quote Link to comment Share on other sites More sharing options...
Psycho Posted April 30, 2014 Share Posted April 30, 2014 You can do this in one of two ways. 1. Use JavaScript to add an onclick function to the links to run a function that would make an AJAX call to a PHP page to increment the counter for the page being requested then allow the link to open the selected page. 2. Use an intermediary page. Instead of having your links open directly to the external page have them open to a page you host - passing a value to identify the external page. On that intermediary page, perform your Database query to update the hit count and then redirect to the external page. Assuming you have a unique ID for your links, change your current page to something like this: <?php $query = "SELECT link FROM links WHERE link='external'"; $result = mysql_query($query) if (!$result) { echo "Error" . mysql_query(); die(); } if(!mysql_num_rows($result)) { echo "No data"; } else { while ($row = mysql_fetch_assoc($result)) { echo "<a href='referrer.php?link={$row['link']}'>{$row['link']}</a>\n"; } } ?> <?php //Get value from URL and update count $link = mysql_real_escape_string($_GET['id']); $query = "UPDATE link SET count = count + 1 WHERE link='$link'"; $result = mysql_query($query); if (!$result || !mysql_num_rows($result)) { echo "Error" . mysql_query(); die(); } //Redirect user to external link header("Location: {$link}"); exit(); ?> Quote Link to comment Share on other sites More sharing options...
Psycho Posted May 1, 2014 Share Posted May 1, 2014 I had originally wrote the code with the assumption that you had an ID column in your database and then decided to change it to only use the link value. In doing so, I forgot to revert a line to the correct value This $link = mysql_real_escape_string($_GET['id']); Should be this $link = mysql_real_escape_string($_GET['link']); Also, in case it isn't clear. The second block of code in my first reply would be for the page referrer.php that you would create for the links to point to. Quote Link to comment Share on other sites More sharing options...
Hrvoje Posted June 9, 2014 Author Share Posted June 9, 2014 Thanks Psycho Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.