1

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.

2 Answers 2

5

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

Sign up to request clarification or add additional context in comments.

11 Comments

thank you, should I be adding the line of your code or replace one of the line from my code with the UPDATE?
If you don't need the current counter on php-side that update statement will be enough.
I have removed the last 2 lines from my code and replaced with your code but it doesnt add 1 to the LINKCOUNT column in my database
there was but i have deleted that row and still does not work
This row NEEDS to be there - that query doesn't add new row, it updates the existing one.
|
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.