Enjoy an ad free experience by logging in. Not a member yet?
Register .
12-14-2011, 06:44 PM
PM User |
#1
New Coder
Join Date: Dec 2011
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
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!
12-14-2011, 06:59 PM
PM User |
#2
New Coder
Join Date: Dec 2011
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
please anyone?
12-14-2011, 07:57 PM
PM User |
#3
Supreme Master coder!
Join Date: Feb 2009
Posts: 23,178
Thanks: 59
Thanked 3,995 Times in 3,964 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.
12-14-2011, 09:00 PM
PM User |
#4
New Coder
Join Date: Dec 2011
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by
Old Pedant
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!
12-14-2011, 09:18 PM
PM User |
#5
Super Moderator
Join Date: Mar 2006
Location: St. Catharines, Ontario Canada
Posts: 2,629
Thanks: 4
Thanked 147 Times in 138 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.
12-14-2011, 09:19 PM
PM User |
#6
Supreme Master coder!
Join Date: Feb 2009
Posts: 23,178
Thanks: 59
Thanked 3,995 Times in 3,964 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.
12-14-2011, 10:00 PM
PM User |
#7
New Coder
Join Date: Dec 2011
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by
Old Pedant
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?
12-14-2011, 10:29 PM
PM User |
#8
Supreme Master coder!
Join Date: Feb 2009
Posts: 23,178
Thanks: 59
Thanked 3,995 Times in 3,964 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.
12-15-2011, 06:30 PM
PM User |
#9
New Coder
Join Date: Dec 2011
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by
Old Pedant
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
12-15-2011, 07:14 PM
PM User |
#10
Super Moderator
Join Date: Mar 2006
Location: St. Catharines, Ontario Canada
Posts: 2,629
Thanks: 4
Thanked 147 Times in 138 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.
12-15-2011, 07:55 PM
PM User |
#11
New Coder
Join Date: Dec 2011
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by
guelphdad
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 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 ";
}
?>
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-15-2011, 09:54 PM
PM User |
#12
Supreme Master coder!
Join Date: Feb 2009
Posts: 23,178
Thanks: 59
Thanked 3,995 Times in 3,964 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.
12-16-2011, 11:46 AM
PM User |
#13
New Coder
Join Date: Dec 2011
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by
Old Pedant
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
Jump To Top of Thread
Thread Tools
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
HTML code is Off
All times are GMT +1. The time now is 10:51 PM .
Advertisement
Log in to turn off these ads.