1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2000
    Rep Power
    I'm trying to run a script where I take a value for new + a value for old add them; then average them; then post the average as a new value.

    $sql = "UPDATE table SET old=$old+$new, old=avg(old) WHERE id=$id";

    I am pretty sure that my problem comes in trying to create a value for $new. I've tried $new= "new" but that didn't work. I'm assuming that I have a null value somewhere in there (which is probably $new)

    How do I make $new equal a value that was entered on a previous page? Or if that isn't my problem; could someone help me figure out what is? Thanks in advance.
  2. #2
  3. No Profile Picture
    Apprentice Deity
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 1999
    Niagara Falls (On the wrong side of the gorge)
    Rep Power
    Why are you setting old twice?

    if new is the name of the form field that will be the name of the variable. The other problem is that you don't know how many values have previously been averaged in to old, so you don't have a correct divisor. The easiest way I see to do this is initialize the row with 0 for old and a field 'cnt' with a value of 0. Then you can do this:

    update table set old=old+$new, cnt=cnt+1 where id=$id

    Then to get the average at any point

    select old/cnt as avg from table where id=$id

    Notice that old does not have a $ in front of it as you are using the value of old from the table. Also, you can't use avg() the way you are trying as that function is to get the average of a column across several records and needs the group by clause set to know which records to include.

Similar Threads

  1. Replies: 1
    Last Post: October 8th, 2004, 08:07 AM
  2. Replies: 8
    Last Post: March 4th, 2003, 08:56 AM
  3. server load averages
    By ozpacific in forum PHP Development
    Replies: 1
    Last Post: March 2nd, 2003, 07:30 PM
  4. Not checking current password before updating?
    By Escape in forum PHP Development
    Replies: 5
    Last Post: August 13th, 2002, 10:13 PM
  5. Help with updating mysql tables
    By baldy in forum Beginner Programming
    Replies: 2
    Last Post: November 7th, 2001, 01:29 PM

IMN logo majestic logo threadwatch logo seochat tools logo