Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-14-2011, 06:44 PM   PM User | #1
coder11001
New Coder

 
Join Date: Dec 2011
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
coder11001 is an unknown quantity at this point
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!

coder11001 is offline   Reply With Quote
Old 12-14-2011, 06:59 PM   PM User | #2
coder11001
New Coder

 
Join Date: Dec 2011
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
coder11001 is an unknown quantity at this point
please anyone?
coder11001 is offline   Reply With Quote
Old 12-14-2011, 07:57 PM   PM User | #3
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,178
Thanks: 59
Thanked 3,995 Times in 3,964 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 12-14-2011, 09:00 PM   PM User | #4
coder11001
New Coder

 
Join Date: Dec 2011
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
coder11001 is an unknown quantity at this point
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


Quote:
<?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!
coder11001 is offline   Reply With Quote
Old 12-14-2011, 09:18 PM   PM User | #5
guelphdad
Super Moderator


 
guelphdad's Avatar
 
Join Date: Mar 2006
Location: St. Catharines, Ontario Canada
Posts: 2,629
Thanks: 4
Thanked 147 Times in 138 Posts
guelphdad will become famous soon enoughguelphdad will become famous soon enough
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.
guelphdad is offline   Reply With Quote
Old 12-14-2011, 09:19 PM   PM User | #6
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,178
Thanks: 59
Thanked 3,995 Times in 3,964 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 12-14-2011, 10:00 PM   PM User | #7
coder11001
New Coder

 
Join Date: Dec 2011
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
coder11001 is an unknown quantity at this point
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?
coder11001 is offline   Reply With Quote
Old 12-14-2011, 10:29 PM   PM User | #8
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,178
Thanks: 59
Thanked 3,995 Times in 3,964 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 12-15-2011, 06:30 PM   PM User | #9
coder11001
New Coder

 
Join Date: Dec 2011
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
coder11001 is an unknown quantity at this point
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
coder11001 is offline   Reply With Quote
Old 12-15-2011, 07:14 PM   PM User | #10
guelphdad
Super Moderator


 
guelphdad's Avatar
 
Join Date: Mar 2006
Location: St. Catharines, Ontario Canada
Posts: 2,629
Thanks: 4
Thanked 147 Times in 138 Posts
guelphdad will become famous soon enoughguelphdad will become famous soon enough
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.
guelphdad is offline   Reply With Quote
Old 12-15-2011, 07:55 PM   PM User | #11
coder11001
New Coder

 
Join Date: Dec 2011
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
coder11001 is an unknown quantity at this point
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.
coder11001 is offline   Reply With Quote
Old 12-15-2011, 09:54 PM   PM User | #12
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,178
Thanks: 59
Thanked 3,995 Times in 3,964 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 12-16-2011, 11:46 AM   PM User | #13
coder11001
New Coder

 
Join Date: Dec 2011
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
coder11001 is an unknown quantity at this point
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
coder11001 is offline   Reply With Quote
Reply

Bookmarks

Tags
int, integer, mysql, php, plus one]

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:51 PM.


Advertisement
Log in to turn off these ads.