• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 329
  • Last Modified:

update mysql field on click?

I want to update a specific field in a specific row in a database using php...

i.e. when <a href="index.html>Home</a> is clicked it adds 1 to a running tally in:
Database - exampledatabase
table - exampletable
Where row = example record
in Field = hit_count

Any Ideas?
0
maccaj51
Asked:
maccaj51
  • 4
  • 4
  • 2
  • +1
1 Solution
 
Pieter JordaanCommented:

If you link to index.html the page will just count every time it is read, so you can add the code anywhere.

<?php
 mysql_query("update tablename set field = field + 1 where condition");
?>
0
 
maccaj51Author Commented:
so:

<?php
$con = mysql_connect("localhost","USERNAME","PASSWORD");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("DBNAME", $con);
mysql_query("update TABLENAME set PAGECOUNT = field + 1 where condition");
?>

Would this be right?!
0
 
Pieter JordaanCommented:
It depends on your table layout.
If there is only one field, use
 update TABLENAME set PAGECOUNT = PAGECOUNT + 1

Or use your primary key.
 update TABLENAME set PAGECOUNT = PAGECOUNT + 1 where id=1
0
Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 
Pieter JordaanCommented:

Do you still require assistance with this Question?
0
 
maccaj51Author Commented:
Hi BitFreeze...

So sorry been snowed under... havent tried it yet!

But will get back to you and will accredit you as necessary!!

Maccaj51
0
 
Ray PaseurCommented:
It sounds like the thing you're looking for is called a "page hit counter" and you might get better results if you used the Google Analytics web service -- instead of trying to write one yourself.

But that said, here are some resources that describe how it is done ;-)
http://www.lmgtfy.com?q=php+page+hit+counter
0
 
maccaj51Author Commented:
BitFreeze....

This is my code... But doesnt seem to be working or incrementing on page view...?  

<?php
mysql_query("update news set PAGECOUNT = PAGECOUNT + 1 where filename=$filename");
?>
0
 
Marco GasiFreelancerCommented:
Try first solution posted by BitFreeze in post id ID:33421505

<?php
 mysql_query("update tablename set hit_count = hit_count + 1 where examplerecord = '' ");
?>

You have only to add the correct condition to identify row wich has to be updated.

Bye
0
 
Pieter JordaanCommented:

So your MySQL field has "filename" as the Primary ?
Please post the table layout.
0
 
maccaj51Author Commented:
Im being very slow haha! BitFreeze ur first post has worked!! BRILLIANT!!

Thanks to marqusG: for pointing out my ineptitude!!
0
 
Marco GasiFreelancerCommented:
Sorry, but if so you could assign an assited solution to my comment, don't you?
Kind regards
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

  • 4
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now