SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Member
    Join Date
    May 2001
    Location
    lab' ora' toree'
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question: TIMESVISITED (coding) please help

    I tried two ways to get timevisited on an ID from one of my tables (assuming the table is Jokes)...

    basically i want my php page to update timevisited whenever the id is accessed like Jokes.php?ID=1...

    1)
    -------
    $sql = "UPDATE Jokes SET TimesViewed=TimesViewed+1 ".
    "WHERE ID=$ID";
    // i got this from kevin yank's article in sitepoint.com
    // btw thank you kevin, your article helped me out ALOT
    -------
    2)
    -------
    $updates = mysql_query("SELECT TimesViewed From Jokes WHERE ID=$ID");
    $update = mysql_fetch_array($updates);
    $counts = $update["TimesViewed"];
    echo("$counts"); // this can be ignored
    $counts = $counts + 1;
    echo("$counts"); // this can be ignored
    $sql = "UPDATE Deals SET TimesViewed=$counts ".
    "WHERE ID=$ID";
    -------

    for both i loaded up the db before...

    as for the second example, the first echo with show (for example) 1, and the second on will show 2... like how it is suppose to... but the new counts somehow isn't updated...

    This is extremely fustrating... i been working on this for... hmm... four hours, trying different methods, but somehow all efforts been unsuccessful.

    the sql query command works under sql command prompt, but doesn't under php.

    am i missing something...

    Thank You (beforehand)...

    Labmice

    -------
    ps. anyone can try this code, just create a db table with ID=1 and TimesViewed=(zero or 1). remember to set $ID=1 in the beginning of the script...
    -------
    Last edited by labmice; May 31, 2001 at 22:49.

  2. #2
    SitePoint Wizard johnn's Avatar
    Join Date
    Mar 2001
    Location
    Southern California, USA
    Posts
    1,181
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hello,
    Try put $ID in single quotes to see if it works.

    WHERE ID='$ID'"

    John

  3. #3
    SitePoint Member
    Join Date
    May 2001
    Location
    lab' ora' toree'
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanx johnn...

    tried it... sadly, it didn't work...

    greatly appreciated though... =)

    labmice



    -----
    // i added this to the end of the second example just to
    // show that the $ID variable return the assigned value

    echo("$ID"."$counts");

    // though it obviously should, but at this point im willing
    // to try anything
    // im beginning to think it's me j/p

  4. #4
    SitePoint Wizard johnn's Avatar
    Join Date
    Mar 2001
    Location
    Southern California, USA
    Posts
    1,181
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi again,
    Check your table in your database to make sure that you have this record. Check the fields spell correctly. If you have a record what value of $ID. Say if $ID is 2 then hard coded in your script just for testing such as WHERE ID=2".
    Make sure what TimeViewed is.Does it have a value when you examine it in the table and what type it is?

  5. #5
    SitePoint Member
    Join Date
    May 2001
    Location
    lab' ora' toree'
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yello,

    tried to 'hardcode' it in... didn't work... the TimesViewed still won't update the new value...

    The TimesViewed Column is created with INT;

    in other words,

    CREATE TABLE Jokes (
    ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    ....
    TimesViewed INT);

    ------
    just a side note, i got db to insert, update, and retrieve values with no problem... but somehow i just can't get this to work within php
    ------

    btw johnn, what are you doing up this late... oh yeah, i go to school in irvine (UCI), not too far from westminister...

    thanks again...
    Last edited by labmice; Jun 1, 2001 at 00:34.

  6. #6
    SitePoint Wizard johnn's Avatar
    Join Date
    Mar 2001
    Location
    Southern California, USA
    Posts
    1,181
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Click refresh button in the browser. Better yet press CTRL and F5 together to force refresh, or close apache and browser and restart apache again, and run the script again. If not working, select * from .... and just prints out all fields to see if it displays the fields (the choosen record must exist in table of db). Otherwise, sorry I couldn't find anything wrong with your code.
    Yes, it's 1 am, can't sleep, am working on my content management scripts.
    John
    Last edited by johnn; Jun 1, 2001 at 01:03.

  7. #7
    SitePoint Guru
    Join Date
    Jan 2001
    Location
    Alkmaar, Netherlands
    Posts
    710
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your code in second example is
    PHP Code:
    $sql "UPDATE Deals SET TimesViewed=$counts "
    "WHERE ID=$ID"
    and I think it should be
    PHP Code:
    $sql "UPDATE Jokes SET TimesViewed=$counts "
    "WHERE ID=$ID"
    you use another table???

  8. #8
    Talk to the /dev/null Theiggsta's Avatar
    Join Date
    Mar 2001
    Location
    Tampa, FL
    Posts
    376
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try re-defining $ID before it goes up into the query again...most often times... an ID will get lost int he scope somehow. Its worth a shot though...give it a try...helped me out once or twice

    like so:

    $ID=$ID;

    give it a whirl
    Aaron "Theiggsta" Kalin
    Pixel Martini
    Ruby and Rails Developer

  9. #9
    SitePoint Member
    Join Date
    May 2001
    Location
    lab' ora' toree'
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    sylow: yeap... it should have been one table... i edited my original codes to make the example easier to understand...

    theigasta: trying that right now... hmm... ok didn't work...

    ----
    it might be just me and my computer; anyway, im going to start from the beginning and write each step

    ***sql***
    CREATE DATABASE DB;
    USE DB;
    CREATE TABLE count (
    ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    Counts INT);
    INSERT INTO count SET Counts="1";
    SELECT * FROM count;
    ***sql***

    // the select commad is just to make sure the table is created
    // correctly and data inserted correctly

    ***sql result***
    ID Counts
    -- ------
    1 1

    (1 row(s) affected)
    ***sql result***

    // now to edit the data through php

    ***php***
    <?php
    $ID=$ID;
    mysql_connect("localhost", "mysql", "mypasswd");
    mysql_select_db("DB");
    $update = mysql_query("SELECT Counts From count WHERE ID=1");
    $update = mysql_fetch_array($update);
    $counts = $update["Counts"];
    echo("$counts");
    $counts = $counts + 1;
    echo("$counts");
    $ID=$ID;
    $counts=$counts;
    $sql = "UPDATE count SET Counts=$counts ".
    "WHERE ID=1";
    echo("$ID"."$counts");
    ?>
    ***php***

    !!!!ANSWER!!!
    ok i just figured out the problem... hehe... sutpid me...

    $result=MYSQL_QUERY($sql);

    !!!ANSWER!!!

    // thanks you all for your help...
    // btw if anyone wants a timesvisited counter... here it is...
    // just be sure to del all the echo... =]


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •