create average value

Discussion in 'PHP' started by lammspillning, Jan 14, 2008.

  1. #1
    I'm doing a rating function and got a little stucked on the php.
    I'm trying to send a value from flash to php, take that value and put it in a database.
    I was thinking that I should have three rows in my table: ID, votes_sum and votes_tot. So that I can calculate an average value.

    The ID should represent the specific movie or what I'm rating on, votes_sum should be a value that increases depending of the value the php recieves. And the votes_sum should count how many votes have been made on that specific ID.
    So far I only figured out something like this, but I dont really know if I'm on the right track.. (I'm not so experienced in php:)) any ideas?

    <?php 
    $DBhost = "localhost"; // Database Server 
    $DBuser = "user"; // Database User 
    $DBpass = "pass"; // Database Pass 
    $DBName = "database"; // Database Name 
    $table = "table"; // Database Table 
    
    // Recieve Variables From Flash 
    $rate_value = $_POST['valueToPHP']; 
    $name = $_POST['nameToPHP']; 
    
    // Insert the data into the mysql table 
    $sql = 'INSERT INTO ' . $table . 
    ' (`ID`, 
    `votes_summa`, 
    `votes_total` 
    ) 
    VALUES 
    ('' . $name . '',' 
    . ''' . $rate_value . '' 
    )'; 
    ?>
    
    Code (markup):

     
    lammspillning, Jan 14, 2008 IP
  2. jwlnewsome

    jwlnewsome Peon

    Messages:
    18
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    looks ok but first you need to connect to the db

    $dbh = mysql_connect($hostname, $username, $password)
    or die("Unable to connect to mysql");

    $selected = mysql_select_db($DBName,$dbh)
    or die("Could not select ".$DBName);

    $sql = 'INSERT INTO ' . $table . ' (ID, votes_summa,votes_total) VALUES ('' . $name . '',' .''' . $rate_value . '' )';


    if (mysql_query($sql)) {
    print "successfully inserted record";
    }
    else {
    print "Failed to insert record";
    }
    mysql_close($dbh);
     
    jwlnewsome, Jan 15, 2008 IP
  3. cr0cx

    cr0cx Active Member

    Messages:
    149
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    55
    #3
    you can use:

    SELECT avg(vote_points) as average where something=is_something

    avg() gets the average of all the values in the result..
     
    cr0cx, Jan 15, 2008 IP