Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    New Coder
    Join Date
    Dec 2011
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Add INT value to field

    How would I add (or minus) and integer value of 1 to a field using a link?


    Thanks very much! Oh and I've really only just begun learning php, so please explain (if u can) in complete amateur terms!

    Thank you!


  • #2
    New Coder
    Join Date
    Dec 2011
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    please anyone?

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,191
    Thanks
    80
    Thanked 4,562 Times in 4,526 Posts
    Well, I don't use PHP, at all.

    But I can give you the basic direction to go.

    Suppose your link looks like this:
    Code:
        http://www.yoursite.com/updateCounter.php?id=777&increment=yes
    or
        http://www.yoursite.com/updateCounter.php?id=777&decrement=yes
    (where the 777 is replaced by the actual id of the record to be changed).

    So then your PHP code could be as simple as this:
    Code:
    <?php
        ... create and open your database connection...
    $change = 0;
    if isset($_GET["increment"] ) ) $change = " + 1";
    else if ( isset($_GET["decrement"]) ) $change = " -1";
    
    if ( isset($_GET["id"]) && $change != 0 )
    {
        $id = $_GET["id"];
        $sql = "UPDATE nameOfTable SET nameOfField = nameOfField $change WHERE id=$id";
        mysql_query($sql) or die("Error in sql: " . mysql_error() );
    }
    ?>
    That code might have a minor error in it (since, as I said, I don't use PHP), but it's bound to be pretty close.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    New Coder
    Join Date
    Dec 2011
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation

    Quote Originally Posted by Old Pedant View Post
    Well, I don't use PHP, at all.

    But I can give you the basic direction to go.

    Suppose your link looks like this:
    Code:
        http://www.yoursite.com/updateCounter.php?id=777&increment=yes
    or
        http://www.yoursite.com/updateCounter.php?id=777&decrement=yes
    (where the 777 is replaced by the actual id of the record to be changed).

    So then your PHP code could be as simple as this:
    Code:
    <?php
        ... create and open your database connection...
    $change = 0;
    if isset($_GET["increment"] ) ) $change = " + 1";
    else if ( isset($_GET["decrement"]) ) $change = " -1";
    
    if ( isset($_GET["id"]) && $change != 0 )
    {
        $id = $_GET["id"];
        $sql = "UPDATE nameOfTable SET nameOfField = nameOfField $change WHERE id=$id";
        mysql_query($sql) or die("Error in sql: " . mysql_error() );
    }
    ?>
    That code might have a minor error in it (since, as I said, I don't use PHP), but it's bound to be pretty close.




    Thanks very much for replying, I had a go doing what you said, as this is basically what I have done:

    table name: vote
    field name that i want to increase: votes


    <?php

    mysql_connect("hostname", "dbuser", "dbpassword");
    mysql_select_db("dbname");



    $change = 0;
    if (isset($_GET["increment"] ) ) $change = " + 1";
    else if ( isset($_GET["decrement"]) ) $change = " -1";

    if ( isset($_GET["id"]) && $change != 0 )
    {
    $id = $_GET["id"];
    $sql = "UPDATE vote SET votes= votes $change WHERE id=$id";
    mysql_query($sql) or die("Error in sql: " . mysql_error() );
    }

    ?>

    It still doesn't seem to be working, I am presented with no error but when I visit the MYSQL database after clicking on the link the 'votes' value remained unchanged. Do you know what I'm doing wrong?

    Thanks very much!

  • #5
    Super Moderator guelphdad's Avatar
    Join Date
    Mar 2006
    Location
    St. Catharines, Ontario Canada
    Posts
    2,634
    Thanks
    4
    Thanked 148 Times in 139 Posts
    echo out the value for $sql and make sure it is exactly as you expect it and not missing some part of what you expect the string to have in it.

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,191
    Thanks
    80
    Thanked 4,562 Times in 4,526 Posts
    DEBUG DEBUG DEBUG

    What GuelphDad said but more. We don't even know if you are getting to the point of executing the sql.

    Code:
    <?php
    
    mysql_connect("hostname", "dbuser", "dbpassword");
    mysql_select_db("dbname");
    
    $change = 0;
    if (isset($_GET["increment"] ) ) $change = " + 1";
    else if ( isset($_GET["decrement"]) ) $change = " -1";
    
    echo "DEBUG change is " . $change . "<hr/>\n";
    echo "DEBUG isset of id says " . isset($_GET["id"]) . "<hr/>\n";
    
    if ( isset($_GET["id"]) && $change != 0 )
    {
        $id = $_GET["id"];
        $sql = "UPDATE vote SET votes= votes $change WHERE id=$id";
    
        echo "DEBUG SQL: " . $sql . "<hr/>\n";
    
        mysql_query($sql) or die("Error in sql: " . mysql_error() );
    } else {
        echo "DEBUG: id is not set or chnage is zero, so no UPDATE performed<hr/>\n";
    }
    ?>
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #7
    New Coder
    Join Date
    Dec 2011
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation

    Quote Originally Posted by Old Pedant View Post
    DEBUG DEBUG DEBUG

    What GuelphDad said but more. We don't even know if you are getting to the point of executing the sql.

    Code:
    <?php
    
    mysql_connect("hostname", "dbuser", "dbpassword");
    mysql_select_db("dbname");
    
    $change = 0;
    if (isset($_GET["increment"] ) ) $change = " + 1";
    else if ( isset($_GET["decrement"]) ) $change = " -1";
    
    echo "DEBUG change is " . $change . "<hr/>\n";
    echo "DEBUG isset of id says " . isset($_GET["id"]) . "<hr/>\n";
    
    if ( isset($_GET["id"]) && $change != 0 )
    {
        $id = $_GET["id"];
        $sql = "UPDATE vote SET votes= votes $change WHERE id=$id";
    
        echo "DEBUG SQL: " . $sql . "<hr/>\n";
    
        mysql_query($sql) or die("Error in sql: " . mysql_error() );
    } else {
        echo "DEBUG: id is not set or chnage is zero, so no UPDATE performed<hr/>\n";
    }
    ?>


    It says that there has been no update performed, but I'm still unaware of the problem? Sorry guys, what could possibly be wrong?

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,191
    Thanks
    80
    Thanked 4,562 Times in 4,526 Posts
    So what are you getting from the *OTHER* DEBUG lines???

    In particular, what is the value of $change and the isset if the id??
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #9
    New Coder
    Join Date
    Dec 2011
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    So what are you getting from the *OTHER* DEBUG lines???

    In particular, what is the value of $change and the isset if the id??
    This is what I get:

    DEBUG change is + 1
    DEBUG isset of id says 1
    DEBUG: id is not set or chnage is zero, so no UPDATE performed



    help meee

  • #10
    Super Moderator guelphdad's Avatar
    Join Date
    Mar 2006
    Location
    St. Catharines, Ontario Canada
    Posts
    2,634
    Thanks
    4
    Thanked 148 Times in 139 Posts
    This is really more of a php question as you seem to be having problems passing your values along.

    post the relevant part of the script here and if we can't assist then we'll move the thread to the php forum.

  • #11
    New Coder
    Join Date
    Dec 2011
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation

    Quote Originally Posted by guelphdad View Post
    This is really more of a php question as you seem to be having problems passing your values along.

    post the relevant part of the script here and if we can't assist then we'll move the thread to the php forum.
    Well, it's what already has been posted but okay.

    Here is the link to the php code

    PHP Code:
    <a href="http://www.voting.comze.com/updatecounter.php?id=1&increment=yes">Vote Up</a

    Here is the actual code itself (updatecounter.php)

    PHP Code:
    <?php

    mysql_connect
    ("dbserver""dbuser""dbpass);
    mysql_select_db("
    dbname");

    $change = 0;
    if (isset($_GET["
    increment"] ) ) $change = " 1";
    else if ( isset($_GET["
    decrement"]) ) $change = " -1";

    echo "
    DEBUG change is " . $change . "<hr/>n";
    echo "
    DEBUG isset of id says " . isset($_GET["id"]) . "<hr/>n";

    if ( isset($_GET["
    id"]) && $change != 0 )
    {
        $id = $_GET["
    id"];
        $sql = "
    UPDATE vote SET votesvotes $change WHERE id=$id";

        echo "
    DEBUG SQL" . $sql . "<hr/>n";

        mysql_query($sql) or die("
    Error in sql" . mysql_error() );
    } else {
        echo "
    DEBUGid is not set or chnage is zeroso no UPDATE performed<hr/>n";
    }
    ?>


    The table in the database is called vote, if has three fields 'id', 'name' and 'votes', I wish to add or minus one from the votes by clicking a link.


    Thanks again.

  • #12
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,191
    Thanks
    80
    Thanked 4,562 Times in 4,526 Posts
    I don't use PHP. But...

    The only thing I can see there that could be going wrong is that the line
    Code:
    if ( isset($_GET["id"]) && $change != 0 )
    isn't working properly.

    I wonder if that is because we are doing $change != 0 when $change is now a string?

    So why not do this:
    Code:
    $change = "";
    if (isset($_GET["increment"] ) ) $change = " + 1";
    else if ( isset($_GET["decrement"]) ) $change = " -1";
    
    echo "DEBUG change is " . $change . "<hr/>n";
    echo "DEBUG isset of id says " . isset($_GET["id"]) . "<hr/>n";
    
    if ( isset($_GET["id"]) && $change != "" )
    {
    ...
    See that? Now $change is always a string and we compare it as a string.

    The last thing to try (shouldn't be needed, but again I don't use PHP) would be
    Code:
    if ( ( isset($_GET["id"]) != 0 ) && ( $change != "" ) )
    {
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #13
    New Coder
    Join Date
    Dec 2011
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    I don't use PHP. But...

    The only thing I can see there that could be going wrong is that the line
    Code:
    if ( isset($_GET["id"]) && $change != 0 )
    isn't working properly.

    I wonder if that is because we are doing $change != 0 when $change is now a string?

    So why not do this:
    Code:
    $change = "";
    if (isset($_GET["increment"] ) ) $change = " + 1";
    else if ( isset($_GET["decrement"]) ) $change = " -1";
    
    echo "DEBUG change is " . $change . "<hr/>n";
    echo "DEBUG isset of id says " . isset($_GET["id"]) . "<hr/>n";
    
    if ( isset($_GET["id"]) && $change != "" )
    {
    ...
    See that? Now $change is always a string and we compare it as a string.

    The last thing to try (shouldn't be needed, but again I don't use PHP) would be
    Code:
    if ( ( isset($_GET["id"]) != 0 ) && ( $change != "" ) )
    {

    thanks very much, it works now!

    cheers all,

    coder11001


  •  

    Tags for this Thread

    Posting Permissions

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