increment using update

Ask a Question related to PHP Development, Design and Development.

  1. #1

    Default increment using update

    I would like to keep track of how many times each user logs in. When a user
    fills in the form information on my login.html page it activates a PHP file
    which.. well, here:

    // BEGIN CODE //

    <?php
    session_start();
    $auth = false; // Assume user is not authenticated

    // Connect to MySQL
    mysql_connect('localhost', 'username', 'password')
    or die ( 'Unable to connect to server.' );

    // Select database on MySQL server
    mysql_select_db("dungeon_MemberDirectory")
    or die ( 'Unable to select database.' );

    // Formulate the query
    $parameters = "SELECT * FROM Member WHERE
    loginName = '$loginName' AND
    password = '$password'";

    // Execute the query and put results in $result
    $result = mysql_query( $parameters )
    or die ( 'Unable to execute query.' );

    // Get number of rows in $result.
    $num = mysql_numrows( $result );

    if ( $num != 0 ) {
    // A matching row was found - the user is authenticated.
    $auth = true;
    }

    if ( ! $auth ) {
    echo '<A HREF="javascript:history.go(-1)">Go Back to Login</A><br><br>' ;
    echo '<h3>Unable to login. Username and/or password invalid.</h3>';
    exit;

    }
    else {
    $_SESSION['auth']="true";
    header("Location: thisweek.html");
    }

    ?>

    // END CODE //


    The member table has a field called 'userCounter', which I would like to
    update each time they log in. Right below $auth = true in my code, could I
    insert something like

    mysql_query(UPDATE Members SET userCounter = userCounter + 1 WHERE
    loginName = '$loginName');

    or does a mathematical operater not work in update and I need to take out
    the userCounter before hand, add one to it, and then insert the new number?

    Thanks in advance,

    ~Chris


    Chris Martin Guest

  2. Similar Questions and Discussions

    1. Access update, CFLOOP, number increment
      I have a table with four columns. field0 | field1 | field3 | field10 (primary key) L ABC 101 ...
    2. how to insert with id=increment
      Hi! I'm coming straight from php and mysql and I want to run this INSERT command from asp.net to mssql with ID as PK and auto incremented. ID...
    3. Increment 'A'
      Does anybody knows how to increment the 'A' so it will be 'B' and so on to 'Z'???? Thanx
    4. Time Increment
      What is the smallest time increment that flash recognizes? I need something that is sensitive to the microsecond (.0000001 sec) Thanks
    5. What else will cause the SCN to increment?
      Beside the "commit" statement, what else will cause the SCN to increment? Thanks
  3. #2

    Default Re: increment using update

    Chris Martin wrote:
    > I would like to keep track of how many times each user logs in. When a
    > user fills in the form information on my login.html page it activates a
    [..]
    >
    > mysql_query(UPDATE Members SET userCounter = userCounter + 1 WHERE
    > loginName = '$loginName');
    >
    > or does a mathematical operater not work in update and I need to take out
    > the userCounter before hand, add one to it, and then insert the new
    > number?
    [..]

    why you ask.. try.. and enjoy..

    [..]
    > Thanks in advance,
    >
    > ~Chris
    --
    [url]www.iuz-lab.info[/url]
    iuz Guest

  4. #3

    Default Re: increment using update

    "iuz" <user@host.network> wrote in message
    news:0bhad.18288$b5.899335@news3.tin.it...
    > Chris Martin wrote:
    >
    > > I would like to keep track of how many times each user logs in. When a
    > > user fills in the form information on my login.html page it activates a
    > [..]
    > >
    > > mysql_query(UPDATE Members SET userCounter = userCounter + 1 WHERE
    > > loginName = '$loginName');
    > >
    > > or does a mathematical operater not work in update and I need to take
    out
    > > the userCounter before hand, add one to it, and then insert the new
    > > number?
    > [..]
    >
    > why you ask.. try.. and enjoy..
    >
    > [..]
    > > Thanks in advance,
    > >
    > > ~Chris
    >
    > --
    > [url]www.iuz-lab.info[/url]
    I ask for a few different reasons.. Firstly, I'd rather not inconvenience
    the users by messing up the web page accidently :). Secondly, I have a
    research paper to complete, presentations to design, other papers to write
    and a few books to read, and not much time to do it in. Posting a note to
    hear responses is a wonderful way to use my time, as I'll get feedback on
    not only if an idea would work, but also if there is a better way of going
    about doing it. Maybe the way I suggested would work but takes three times
    the processing time as some other way. Getting code to work usually isn't a
    problem for me.. it'll look nasty and going back to it later will not be
    fun, but it'll work. If I can discuss a better way of implementing my code
    in the design phase then downtime will be invisible to the users and I would
    have learned a better way to do something.

    ~Chris

    PS - I do understand what you are saying, though... over the weekend I made
    a database system for a user interface using PHP and SQL, which I had never
    touched before, and had a wonderful time doing it.


    Chris Martin Guest

  5. #4

    Default Re: increment using update

    "Chris Martin" <christopher.martin@REMOVEadelphia.net> wrote in message
    news:pJidnYKN3P3wOvTcRVn-hw@adelphia.com...
    > "iuz" <user@host.network> wrote in message
    > news:0bhad.18288$b5.899335@news3.tin.it...
    > > Chris Martin wrote:
    >
    > I ask for a few different reasons.. Firstly, I'd rather not inconvenience
    > the users by messing up the web page accidently :).
    You shouldn't be doing that in a production environment. Do it in a test
    environment or on a test table.
    > Secondly, I have a
    > research paper to complete, presentations to design, other papers to write
    > and a few books to read, and not much time to do it in.
    And you could have executed that SQL statement (which you already wrote) in
    less time than it took to write these notes. With all you have to do, I
    would have taken the quickest route.
    > Posting a note to
    > hear responses is a wonderful way to use my time, as I'll get feedback on
    > not only if an idea would work, but also if there is a better way of going
    > about doing it.
    Well, that's a completely different issue. And you're too busy to spend time
    exploring code optimizations.
    > Maybe the way I suggested would work but takes three times
    > the processing time as some other way. Getting code to work usually isn't
    a
    > problem for me.. it'll look nasty and going back to it later will not be
    > fun, but it'll work. If I can discuss a better way of implementing my
    code
    > in the design phase then downtime will be invisible to the users and I
    would
    > have learned a better way to do something.
    You forgot... you're too busy for the discussions.

    (just messin' with ya)

    - Virgil



    Virgil Green Guest

  6. #5

    Default Re: increment using update (with OT question)


    "Virgil Green" <vjg@DESPAMobsydian.com> wrote in message
    news:rEUad.4272$Al3.3315@newssvr30.news.prodigy.co m...
    > "Chris Martin" <christopher.martin@REMOVEadelphia.net> wrote in message
    > news:pJidnYKN3P3wOvTcRVn-hw@adelphia.com...
    > > "iuz" <user@host.network> wrote in message
    > > news:0bhad.18288$b5.899335@news3.tin.it...
    > > > Chris Martin wrote:
    > >
    > > I ask for a few different reasons.. Firstly, I'd rather not
    inconvenience
    > > the users by messing up the web page accidently :).
    >
    > You shouldn't be doing that in a production environment. Do it in a test
    > environment or on a test table.
    >
    > > Secondly, I have a
    > > research paper to complete, presentations to design, other papers to
    write
    > > and a few books to read, and not much time to do it in.
    >
    > And you could have executed that SQL statement (which you already wrote)
    in
    > less time than it took to write these notes. With all you have to do, I
    > would have taken the quickest route.
    >
    > > Posting a note to
    > > hear responses is a wonderful way to use my time, as I'll get feedback
    on
    > > not only if an idea would work, but also if there is a better way of
    going
    > > about doing it.
    >
    > Well, that's a completely different issue. And you're too busy to spend
    time
    > exploring code optimizations.
    >
    > > Maybe the way I suggested would work but takes three times
    > > the processing time as some other way. Getting code to work usually
    isn't
    > a
    > > problem for me.. it'll look nasty and going back to it later will not be
    > > fun, but it'll work. If I can discuss a better way of implementing my
    > code
    > > in the design phase then downtime will be invisible to the users and I
    > would
    > > have learned a better way to do something.
    >
    > You forgot... you're too busy for the discussions.
    >
    > (just messin' with ya)
    >
    > - Virgil
    >
    I certainly feel like I'm too busy for it sometimes =0P. If I post a
    question on a newsgroup, 99% of the time it isn't something that I need to
    get done right away and it is just something i'm thinking about doing. If
    that statement was something I needed to get done ASAP I would have just
    tried it.

    On a complete side note (now for the off topic question), any of you people
    get jobs through temp agancies or even work for one? I'm going to school
    full time and working full time (as a barista at starbucks), but I've signed
    up with a temp agency. I only just did it and I was just curious how they
    work interviews in or whatever they need to do? With two weeks notice I can
    get whatever time off I need from work to do a temp job, but is that
    plausable? Do temp agencies know about jobs 2 weeks ahead of time or am I
    just putting in some wishful thinking? Does it depend a lot on what the
    temp agency is like and broad answers won't do anything for me?

    ~LookingForExperience.


    Chris Martin Guest

  7. #6

    Default Re: increment using update

    Well... I am just turning my attention to PHP, but I have recently done
    exactly the same thing in ASP for a visitorcounter in flash, which really
    wouldn't make any difference since it in fact is a SQL question, and Yes,
    your sql update works.just fine.
    Too bad You haven't been able to get any answers except "try it", and "don't
    do it in a production environment".
    Not everybody have a test and development environment available.
    /Jari


    "Chris Martin" <christopher.martin@REMOVEadelphia.net> skrev i meddelandet
    news:f7CdnRvZycLA4fTcRVn-pQ@adelphia.com...
    >I would like to keep track of how many times each user logs in. When a
    >user
    > fills in the form information on my login.html page it activates a PHP
    > file
    > which.. well, here:
    >
    > // BEGIN CODE //
    >
    > <?php
    > session_start();
    > $auth = false; // Assume user is not authenticated
    >
    > // Connect to MySQL
    > mysql_connect('localhost', 'username', 'password')
    > or die ( 'Unable to connect to server.' );
    >
    > // Select database on MySQL server
    > mysql_select_db("dungeon_MemberDirectory")
    > or die ( 'Unable to select database.' );
    >
    > // Formulate the query
    > $parameters = "SELECT * FROM Member WHERE
    > loginName = '$loginName' AND
    > password = '$password'";
    >
    > // Execute the query and put results in $result
    > $result = mysql_query( $parameters )
    > or die ( 'Unable to execute query.' );
    >
    > // Get number of rows in $result.
    > $num = mysql_numrows( $result );
    >
    > if ( $num != 0 ) {
    > // A matching row was found - the user is authenticated.
    > $auth = true;
    > }
    >
    > if ( ! $auth ) {
    > echo '<A HREF="javascript:history.go(-1)">Go Back to Login</A><br><br>' ;
    > echo '<h3>Unable to login. Username and/or password invalid.</h3>';
    > exit;
    >
    > }
    > else {
    > $_SESSION['auth']="true";
    > header("Location: thisweek.html");
    > }
    >
    > ?>
    >
    > // END CODE //
    >
    >
    > The member table has a field called 'userCounter', which I would like to
    > update each time they log in. Right below $auth = true in my code, could
    > I
    > insert something like
    >
    > mysql_query(UPDATE Members SET userCounter = userCounter + 1 WHERE
    > loginName = '$loginName');
    >
    > or does a mathematical operater not work in update and I need to take out
    > the userCounter before hand, add one to it, and then insert the new
    > number?
    >
    > Thanks in advance,
    >
    > ~Chris
    >
    >

    Jari Ivanoff Guest

  8. #7

    Default Re: increment using update

    Top posting cause it was my own post that I'm answering and I've already
    read it...

    Yes, the following code does actually work, in case anyone wanted to know.

    mysql_query("UPDATE Member SET loginCounter = loginCounter + 1 WHERE
    loginName = '$loginName'");

    I was curious if inline addition was allowed using the update query, so
    there is the answer, it does.

    Next question is not a simple yes it does or no it doesn't answer. Is this
    the most efficient method of achieving this. While in my mind I see one
    command, I know it is going to be broken down into different steps.. so is
    it more efficient to use a command to retrieve the value, then do the math
    in the PHP code, then insert the new variable?

    TIA,
    ~Chris
    __________________________________________________ ___________

    "Chris Martin" <christopher.martin@REMOVEadelphia.net> wrote in message
    news:f7CdnRvZycLA4fTcRVn-pQ@adelphia.com...
    > I would like to keep track of how many times each user logs in. When a
    user
    > fills in the form information on my login.html page it activates a PHP
    file
    > which.. well, here:
    >
    > // BEGIN CODE //
    >
    > <?php
    > session_start();
    > $auth = false; // Assume user is not authenticated
    >
    > // Connect to MySQL
    > mysql_connect('localhost', 'username', 'password')
    > or die ( 'Unable to connect to server.' );
    >
    > // Select database on MySQL server
    > mysql_select_db("dungeon_MemberDirectory")
    > or die ( 'Unable to select database.' );
    >
    > // Formulate the query
    > $parameters = "SELECT * FROM Member WHERE
    > loginName = '$loginName' AND
    > password = '$password'";
    >
    > // Execute the query and put results in $result
    > $result = mysql_query( $parameters )
    > or die ( 'Unable to execute query.' );
    >
    > // Get number of rows in $result.
    > $num = mysql_numrows( $result );
    >
    > if ( $num != 0 ) {
    > // A matching row was found - the user is authenticated.
    > $auth = true;
    > }
    >
    > if ( ! $auth ) {
    > echo '<A HREF="javascript:history.go(-1)">Go Back to Login</A><br><br>' ;
    > echo '<h3>Unable to login. Username and/or password invalid.</h3>';
    > exit;
    >
    > }
    > else {
    > $_SESSION['auth']="true";
    > header("Location: thisweek.html");
    > }
    >
    > ?>
    >
    > // END CODE //
    >
    >
    > The member table has a field called 'userCounter', which I would like to
    > update each time they log in. Right below $auth = true in my code, could
    I
    > insert something like
    >
    > mysql_query(UPDATE Members SET userCounter = userCounter + 1 WHERE
    > loginName = '$loginName');
    >
    > or does a mathematical operater not work in update and I need to take out
    > the userCounter before hand, add one to it, and then insert the new
    number?
    >
    > Thanks in advance,
    >
    > ~Chris
    >
    >

    Chris Martin Guest

  9. #8

    Default Re: increment using update


    "Chris Martin" <christopher.martin@REMOVEadelphia.net> wrote in message
    news:CbudnUH-fPwKlu7cRVn-vA@adelphia.com...
    > Top posting cause it was my own post that I'm answering and I've already
    > read it...
    >
    > Yes, the following code does actually work, in case anyone wanted to know.
    >
    > mysql_query("UPDATE Member SET loginCounter = loginCounter + 1 WHERE
    > loginName = '$loginName'");
    >
    > I was curious if inline addition was allowed using the update query, so
    > there is the answer, it does.
    >
    > Next question is not a simple yes it does or no it doesn't answer. Is
    this
    > the most efficient method of achieving this. While in my mind I see one
    > command, I know it is going to be broken down into different steps.. so is
    > it more efficient to use a command to retrieve the value, then do the math
    > in the PHP code, then insert the new variable?
    >
    > TIA,
    > ~Chris
    It's the most atomic way. Your other suggestion leaves holes for concurrent
    updates unless you are using proper commitment control.

    - Virgil


    Virgil Green Guest

  10. #9

    Default Re: increment using update

    "Jari Ivanoff" <jari.ivanoff@@comhem.se> wrote in message
    news:CkCcd.6432$d5.53660@newsb.telia.net...
    > Well... I am just turning my attention to PHP, but I have recently done
    > exactly the same thing in ASP for a visitorcounter in flash, which really
    > wouldn't make any difference since it in fact is a SQL question, and Yes,
    > your sql update works.just fine.
    Most of us knew that... but he won't learn to fend for himself if we just
    tell him that.
    > Too bad You haven't been able to get any answers except "try it", and
    "don't
    > do it in a production environment".
    Actually, it's good *precisely* because this is the kind of question that is
    best answered by experimentation and consultation with the manuals.
    > Not everybody have a test and development environment available.
    I'm interested in knowing the situation where someone can't have a test
    environment... at least enough to test the code in question. The software is
    free. The computers are already in place, obviously. So, other than an
    environment where *no* development was allowed, what would be the case in
    which something like this could not be tested?

    - Virgil


    Virgil Green Guest

  11. #10

    Default Re: increment using update


    "Virgil Green" <vjg@DESPAMobsydian.com> wrote in message
    news:QZOcd.7216$Al3.7211@newssvr30.news.prodigy.co m...
    >
    > "Chris Martin" <christopher.martin@REMOVEadelphia.net> wrote in message
    > news:CbudnUH-fPwKlu7cRVn-vA@adelphia.com...
    > > Top posting cause it was my own post that I'm answering and I've already
    > > read it...
    > >
    > > Yes, the following code does actually work, in case anyone wanted to
    know.
    > >
    > > mysql_query("UPDATE Member SET loginCounter = loginCounter + 1 WHERE
    > > loginName = '$loginName'");
    > >
    > > I was curious if inline addition was allowed using the update query, so
    > > there is the answer, it does.
    > >
    > > Next question is not a simple yes it does or no it doesn't answer. Is
    > this
    > > the most efficient method of achieving this. While in my mind I see one
    > > command, I know it is going to be broken down into different steps.. so
    is
    > > it more efficient to use a command to retrieve the value, then do the
    math
    > > in the PHP code, then insert the new variable?
    > >
    > > TIA,
    > > ~Chris
    >
    > It's the most atomic way. Your other suggestion leaves holes for
    concurrent
    > updates unless you are using proper commitment control.
    >
    > - Virgil
    >
    >
    Alright, thanks. I like to ask questions like I did instead of put a
    snippit of code into a test enviornment because I get people's views on the
    code as well. I knew I could get the code to work if I messed with it for 5
    minutes, so I was more wondering if there was a better way of doing it that
    I was overlooking.

    Thanks again,
    ~Chris


    Chris Martin Guest

  12. #11

    Default Re: increment using update

    "Chris Martin" <christopher.martin@REMOVEadelphia.net> wrote in message
    news:o8edncGryaLsae7cRVn-1w@adelphia.com...
    >
    > "Virgil Green" <vjg@DESPAMobsydian.com> wrote in message
    > news:QZOcd.7216$Al3.7211@newssvr30.news.prodigy.co m...
    > >
    > > "Chris Martin" <christopher.martin@REMOVEadelphia.net> wrote in message
    > > news:CbudnUH-fPwKlu7cRVn-vA@adelphia.com...
    > > > Top posting cause it was my own post that I'm answering and I've
    already
    > > > read it...
    > > >
    > > > Yes, the following code does actually work, in case anyone wanted to
    > know.
    > > >
    > > > mysql_query("UPDATE Member SET loginCounter = loginCounter + 1 WHERE
    > > > loginName = '$loginName'");
    > > >
    > > > I was curious if inline addition was allowed using the update query,
    so
    > > > there is the answer, it does.
    > > >
    > > > Next question is not a simple yes it does or no it doesn't answer. Is
    > > this
    > > > the most efficient method of achieving this. While in my mind I see
    one
    > > > command, I know it is going to be broken down into different steps..
    so
    > is
    > > > it more efficient to use a command to retrieve the value, then do the
    > math
    > > > in the PHP code, then insert the new variable?
    > > >
    > > > TIA,
    > > > ~Chris
    > >
    > > It's the most atomic way. Your other suggestion leaves holes for
    > concurrent
    > > updates unless you are using proper commitment control.
    > >
    > > - Virgil
    > >
    > >
    >
    > Alright, thanks. I like to ask questions like I did instead of put a
    > snippit of code into a test enviornment because I get people's views on
    the
    > code as well. I knew I could get the code to work if I messed with it for
    5
    > minutes, so I was more wondering if there was a better way of doing it
    that
    > I was overlooking.
    Chris,

    I think the main point of (the drift of) this thread was that you will get
    much better responses here if you try it first, present your findings, and
    then ask for opinions regarding improvements in method/technique.

    - Virgil


    Virgil Green Guest

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139