Jump to content


+ 1 to database


3 replies to this topic

#1 dEcade

    P2L Staff

  • P2L Staff
  • PipPipPipPip
  • 1,850 posts
  • Gender:Male
  • Location:Saskatoon, Saskatchewan
  • Interests:Guitar, Programming, Storm Chasing, Games (Designing and playing), Hockey, Photography

Posted 21 January 2006 - 10:00 AM

Okay I have seen a post around here about this before but I had no luck finding it. I have a more complexed way of doing this but I saw there was a really simple way where you just have a little script in the mysql query. Okay here is what I was looking for:

I need a script that would +1 to a number in the database.

Thanks.

dEcade

#2 tiki

    Young Padawan

  • Members
  • Pip
  • 259 posts
  • Gender:Male
  • Location:California

Posted 21 January 2006 - 02:56 PM

$query = mysql_query("UPDATE table SET row = $variable + 1 WHERE id = 4)";

Something like that, just change to work for you.

#3 veridicus

    Young Padawan

  • Members
  • Pip
  • 5 posts

Posted 21 January 2006 - 03:26 PM

After connecting to the database run the following query...

UPDATE your_table SET value = value + 1 WHERE id = 1

Leave out the WHERE clause to update every record in the table.

If you want a whole script you'll have to tell us what language (PHP?) and what database (MySQL?).

Edited by veridicus, 21 January 2006 - 07:34 PM.


#4 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 21 January 2006 - 06:29 PM

Just here to point a couple of things out about the previous comments (under the assumtion we're working with php/mysql).
1.
$query = mysql_query("UPDATE table SET row = $variable + 1 WHERE id = 4)";
Other then having the end quote in the wrong place (which i'm not going to hold against you, simple key stroke error :)), i believe that query would give a mysql error. When it parses $variable to what ever value it is, it will look for a column by the same name, and find out it's most likely not there.

2. UPDATE doesn't accept the FROM clause since you already define your_table in the beginning.

Other then that, if you mix the 2, you should have a working query.
http://dev.mysql.com.../en/update.html <-- More info

Edited by rc69, 21 January 2006 - 06:30 PM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users