简体   繁体   中英

Creating a click count on link in PHP

I want to make a code that counts the click on the link which is on my website and adds 1 to the LINKCOUNT row in the mysql database, but im having trouble over coding.

This is my code when I click the LINK on my website:

mysql_connect("localhost", "123", "123") or die(mysql_error());
mysql_select_db("123") or die(mysql_error());

$result = mysql_query("SELECT id, sitename FROM links WHERE siteurl = \"www\"");
$row = mysql_fetch_array($result);

mysql_query("INSERT INTO links (linkcount) VALUES  $row['linkcount']");
$count = 0;

Not sure if that is right way of doing it, any ideas what the code will be?

EDIT

I can now add 1 to the LINKCOUNT row but every link i click adds to the same row, i need each link to have seperate row and click count.

Try this query:

UPDATE `links` SET `linkcount` = `linkcount` + 1 WHERE `siteurl` = 'www'

And about your code: your query doesn't select the linkcount column, so $row['linkcount'] is always null.

EDIT:

If your table doesn't contain rows for every link you want to track, you should use that kind of code:

mysql_query("UPDATE `links` SET `linkcount` = `linkcount` + 1 WHERE `siteurl` = 'SITE_URL'");
if(mysql_affected_rows() == 0)
    mysql_query("INSERT INTO `links` (`sitename`, `siteurl`, `linkcounter`) VALUES ('SITE_NAME', 'SITE_URL', 1)");

You have to insert proper SITE_NAME and SITE_URL

You can hash the links with md5 . Use the md5sum of the link as the primary key and check for the link in the table. If it is already present then do an update or else insert the md5sum to the table with the linkcount = 0 .

Follow the following steps :

  • Get the link's md5sum
  • Fire a select query on the table to check for that md5sum
  • If there are more than 0 rows returned the fire an Update query like

UPDATE links SET linkcount = linkcount + 1 WHERE md5sum = "md5sum"

  • Else insert a new row into the table with linkcount = 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM