Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Page 1 of 3 1 2 3 LastLast
Results 1 to 10 of 21
  1. #1
    Senior Member
    Join Date
    Aug 2011
    Posts
    226
    Member #
    29153
    Liked
    4 times

    Why is my MySQL being written to twice with only one code line?

    I have in my code,
    mysql_query("INSERT INTO 80sMusicLibraryUserNumPageVisits (userNum, date, page) VALUES ('$userNum', '$dateToday', '$newUrl')");

    It will write a record of:
    1 2014-02-16 1980
    wich would be correct. I'm user 1 because I was the first on the site. Date and page is also correct. But, it will follow with a record of:
    1 2014-02-16 missing.html

    Why is it doing this??
    It does it every time. I even went to the site on my phone and it did the same thing.

    This is the only query to this table. It is not in a loop. It is not in a function.

    Where did the value "missing.html" come from, and how could the $newUrl variable be set to that? I have no page called that.

  2.  

  3. #2
    WDF Staff mlseim's Avatar
    Join Date
    Apr 2004
    Location
    Cottage Grove, Minnesota
    Posts
    7,686
    Member #
    5580
    Liked
    716 times
    Show us the whole script.


  4. #3
    Senior Member
    Join Date
    Aug 2011
    Posts
    226
    Member #
    29153
    Liked
    4 times
    $userNum = $_COOKIE['80smusiclibrary'];

    $result = mysql_query("SELECT MAX(userNum) FROM 80sMusicLibraryUserNum");
    $row = mysql_fetch_row($result);
    $visitors = $row[0];


    $_GET['newUrl'];
    $newUrl = $_GET['newUrl'];
    $newUrlParts = explode('/', $newUrl);
    $dateToday = date("Y/m/d");


    if ($userNum == "") {
    $userNum = $visitors + 1;
    mysql_query("INSERT INTO 80sMusicLibraryUserNum (userNum) VALUES ('$userNum') ");
    }

    setcookie('80smusiclibrary', $userNum, time()+60*60*24*90);

    mysql_query("INSERT INTO 80sMusicLibraryUserNumPageVisits (userNum, date, page) VALUES ('$userNum', '$dateToday', '$newUrl')");

  5. #4
    Senior Member
    Join Date
    Aug 2011
    Posts
    226
    Member #
    29153
    Liked
    4 times
    It is doing it mostly only with my computer. Most other users it does not.

  6. #5
    WDF Staff mlseim's Avatar
    Join Date
    Apr 2004
    Location
    Cottage Grove, Minnesota
    Posts
    7,686
    Member #
    5580
    Liked
    716 times
    Yes, it's doing it twice .... you are telling it to.

    It will do it if there are no visitors, and then do it again ... it will either happen once or twice depending on if ($userNum == "") {

    The 2nd one (mysql_query("INSERT INTO ...) always happens no matter what.

    You can't write a cookie and read it in the same script execution. Cookies are sent in a header, so it needs a page refresh in-between. That's why it works on some computers, because the cookie has a value after the first refresh.

    EDIT:
    I think you need to describe the "rules" of your visitor number ... what is your script supposed to do?

    EDIT AGAIN:
    I see now that you are using two different table names. Now I'm really confused as to what you're trying to do.
    Last edited by mlseim; Feb 17th, 2014 at 01:16 PM.


  7. #6
    Senior Member
    Join Date
    Aug 2011
    Posts
    226
    Member #
    29153
    Liked
    4 times
    Quote Originally Posted by mlseim View Post
    Yes, it's doing it twice .... you are telling it to.

    It will do it if there are no visitors, and then do it again ... it will either happen once or twice depending on if ($userNum == "") {

    The 2nd one (mysql_query("INSERT INTO ...) always happens no matter what.

    You can't write a cookie and read it in the same script execution. Cookies are sent in a header, so it needs a page refresh in-between. That's why it works on some computers, because the cookie has a value after the first refresh.

    EDIT:
    I think you need to describe the "rules" of your visitor number ... what is your script supposed to do?

    EDIT AGAIN:
    I see now that you are using two different table names. Now I'm really confused as to what you're trying to do.
    Here is the first section of my code.

    <?php
    session_start(); // start up your PHP session!
    include 'includes/mysqlconnect.php';

    $userNum = $_COOKIE['80smusiclibrary'];

    $result = mysql_query("SELECT MAX(userNum) FROM 80sMusicLibraryUserNum");
    $row = mysql_fetch_row($result);
    $visitors = $row[0];


    $_GET['newUrl'];
    $newUrl = $_GET['newUrl'];
    $newUrlParts = explode('/', $newUrl);
    $dateToday = date("Y/m/d");
    date_default_timezone_set("US/Central");
    $time = date(" H:i");


    if ($userNum == "") {
    $userNum = $visitors + 1;
    mysql_query("INSERT INTO 80sMusicLibraryUserNum (userNum) VALUES ('$userNum') ");
    }

    setcookie('80smusiclibrary', $userNum, time()+60*60*24*90);

    mysql_query("INSERT INTO 80sMusicLibraryUserNumPageVisits (userNum, time, date, page) VALUES ('$userNum', '$time', '$dateToday', '$newUrl')");

    ?>






    It shouldn't be executing the if $userNum == "" since my cookie is set to 1 already.

  8. #7
    Senior Member
    Join Date
    Aug 2011
    Posts
    226
    Member #
    29153
    Liked
    4 times
    I just now changed the code to:



    if ($userNum != 1) {
    mysql_query("INSERT INTO 80sMusicLibraryUserNumPageVisits (userNum, time, date, page) VALUES ('$userNum', '$time', '$dateToday', '$newUrl')");
    }



    and it created a new $userNum and recorded a page visit by that new userNum to each page I went to.

  9. #8
    WDF Staff mlseim's Avatar
    Join Date
    Apr 2004
    Location
    Cottage Grove, Minnesota
    Posts
    7,686
    Member #
    5580
    Liked
    716 times
    Try changing this:

    if ($userNum == "") {

    To this:

    if (isset($_COOKIE['80smusiclibrary'])){


    I'm trying to figure out what your script is supposed to do. It seems like you're assigning a User Number based on how many visitors there are, but cookies can be deleted. I could visit your website over and over again without you knowing it ... creating a new cookie each time. I don't understand how User Numbers can be unique, and why you would use cookies.

    You do know that you can set MySQL to automatically assign a unique row ID to each row created, and the ID is never re-used? Even if a row is deleted, that ID is gone forever.


  10. #9
    Senior Member
    Join Date
    Aug 2011
    Posts
    226
    Member #
    29153
    Liked
    4 times
    Yes, I want a user to have a unique id. I understand that cookies can be deleted but usually not within one visit or even within a few days in most cases. I want to follow where visitors are going within a visit and if they come back. Again, I know it's not 100% but can give me an idea.

  11. #10
    WDF Staff mlseim's Avatar
    Join Date
    Apr 2004
    Location
    Cottage Grove, Minnesota
    Posts
    7,686
    Member #
    5580
    Liked
    716 times
    Here's how I would do it.

    Do these steps on each page a user visits, or each file they download, etc...

    1) Whenever a visitor arrives, check to see if they have an existing cookie.
    If they don't, give them a cookie (which is a unique random ID like, C84TrHm367ma9WuQc0).
    If they already have a cookie, then you don't do anything, just go to step 2.

    2) Everywhere they go, you just log the visitor's cookie ID, where they are (the page or file, etc), and the time it was written. At the beginning of each page, do a MySQL INSERT. Log that cookie for that page or file download (if you are tracking file downloads).

    3) That's it.

    You don't have to increment anything. All you care about is where a visitor is going, and you log their movement on each page, or page refresh, or file that they download, etc.

    As an admin person, you can write a script that groups the ID's, and counts them by doing MySQL queries.
    You'll know these things:
    1) Which ID's have the most visits by day, week, month.
    2) Which pages have the most visits, and which ID has visited the page more than any other.
    3) If a particular ID has downloaded a file multiple times.
    4) If a particular ID always visits a page every morning at 9AM, you will know that and maybe wonder why.

    If a user erases their cookie and the unique ID is gone, it will simply give them a new ID the next time they visit. You already said you don't care if cookies get deleted, or if the user moves around your website with 4 different browsers ... in which case that one user has 4 different cookies at the same time.

    Unless a user actually logs-into your site, you know nothing about them other than you've given them a cookie and that cookie has been on various pages.
    Last edited by mlseim; Feb 18th, 2014 at 08:39 AM.



Page 1 of 3 1 2 3 LastLast

Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 05:02 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com