Forum Moderators: coopster

Message Too Old, No Replies

updating a database field

         

Sarah Atkinson

12:12 am on May 26, 2006 (gmt 0)

10+ Year Member



I'm working on a form that will add a quanity to a database. I'm thinking I need to first select the qunity in the database. Create a new variable that is = to the quanity pulled + the quanity to be added then UPDATE the db with the new quanity.

This will work but is there an easier way? maybe one with only one SQL statment?

eelixduppy

1:14 am on May 26, 2006 (gmt 0)



how about this:
$query = "update table_name set quantity = (quantity+".$add_value.")";

Sarah Atkinson

3:26 am on May 28, 2006 (gmt 0)

10+ Year Member



it does not work

dreamcatcher

7:26 am on May 28, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Sarah,

eelixduppy's solution should be ok. Are you sure your variable is populating ok?

Try just running:
$query = "update table_name set quantity = (quantity+1)";

If you don`t want all rows to be updated, you may need a where clause.

$query = "update table_name set quantity = (quantity+".$add_value.") WHERE id = '1'";

etc

dc

Sarah Atkinson

11:17 pm on May 28, 2006 (gmt 0)

10+ Year Member



i just decided to do it with a select statement first then added the two together.

eelixduppy

11:42 pm on May 28, 2006 (gmt 0)



If you would like, you can post some of the code so that we can give you a working query. I don't see a point to having extra steps if they aren't necessary.