#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2000
    Location
    Toronto, Ontario, Canada
    Posts
    19
    Rep Power
    0
    Just wondering if someone can point me in the right direction of incrementing a counter in a MySQL feild when a preformed query displays results and the link gets clicked.

    Thanks in advance
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2000
    Posts
    79
    Rep Power
    15
    If the page which the link goes to is yours then u can put a query at the top of the page along the lines of...
    mysql_query($database,"UPDATE table_name set count=count+1 where link=$link);

    if you don't control the page it goes to then you will need to make a seperate page which the link will go to. Like this..

    <?
    mysql_query($database,"UPDATE table_name set count=count+1 where link=$link);
    header("Location: $link");
    exit;
    ?>

    that will then update your mysql counter and forward the user to $link.

    Basil

  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2000
    Posts
    11
    Rep Power
    0
    I want to include something like this in a site i'm working on. But what can you do about reloads etc. ? Because people can press reload 5 times to screw everything up..
  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2000
    Location
    Toronto, Ontario, Canada
    Posts
    19
    Rep Power
    0
    Can anyone tell me why this won't work? (I'm new to php btw).

    <?php
    $link = mysql_connect ("", "login", "password");
    mysql_select_db ("database");


    $result = mysql_query ("SELECT link FROM links WHERE number=$locn");
    $row = mysql_fetch_array($result);

    mysql_query($link,"UPDATE links set counter=counter+1 where link=$row);
    header("Location: $row["link"]);
    echo "nn";

    mysql_close ($link);
    ?>
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2000
    Location
    Ontario, Canada
    Posts
    498
    Rep Power
    18
    About the reloads --

    Your best bet is to use a text file to hold the counter, and lock the file for maybe 1 or 2 seconds after each access... that way people can't reload hundreds of times and **** it up. And about that header() thing, I think you have to send header() before any other PHP statements.

    ------------------
    To alcohol! The cause of, and solution to, all of life's problems. -- Homer Simpson
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2000
    Posts
    32
    Rep Power
    15
    You can do any php code you want before you send a header - you just can't output anything (no print or echo statments) before sending a header.

    thecrazed1: your problem lies (I believe) in this line

    header("Location: $row["link"]);

    First of all, that should go outside the php tags. Second, it should read

    header("Location: <?php echo $row["link"]; ?>"); What error message are you receiving?
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2000
    Posts
    79
    Rep Power
    15
    First of all..
    header() does not go outside the php tags

    TheCrazed1, here is what i think your code should read..

    <?php
    mysql_connect ("localhost", "login", "password");
    mysql_select_db ("database");

    $result = mysql_query ("SELECT link FROM links WHERE number=$locn");
    while($row = mysql_fetch_array($result)){

    $link = $row["link"];
    mysql_query("UPDATE links set counter=counter+1 where link=$link");

    }
    mysql_close();
    header("Location: $link");
    exit;
    ?>

    Please note that you should not echo anything after use of header()

    Basil

    [This message has been edited by 'tantrum (edited September 17, 2000).]
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2000
    Location
    Indonesia
    Posts
    87
    Rep Power
    15
    Can you tell me how to do the file locking ???

    Or any site for references ??

    Tnx before !!

    See ya

    ------------------
    CyberOsc
    sphmjf_oscario@gurlmail.com

    ** It's the world of Oscario **
  16. #9
  17. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2000
    Location
    Toronto, Ontario, Canada
    Posts
    19
    Rep Power
    0
    Okay thanks everyone for your help thus far the script seems to work without any errors being generated except it doesn't seem to update the counter in the database. The feild type is set as INT and thats about it. Any Ideas? Once again thanks for the help so far!

Similar Threads

  1. Replies: 2
    Last Post: August 18th, 2004, 11:45 PM
  2. PHP with UnixODBC
    By coder4hire in forum PHP Development
    Replies: 2
    Last Post: January 20th, 2004, 02:14 PM
  3. Passing string from PHP to Javascript
    By ka8oad in forum JavaScript Development
    Replies: 4
    Last Post: January 17th, 2004, 04:12 PM
  4. UnixODBC + PHP + Apache + Empress
    By coder4hire in forum Database Management
    Replies: 0
    Last Post: January 15th, 2004, 02:41 PM
  5. Replies: 4
    Last Post: January 6th, 2004, 04:56 PM

IMN logo majestic logo threadwatch logo seochat tools logo