Create average value

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lammspillning
Forum Newbie
Posts: 14
Joined: Fri Aug 17, 2007 10:50 am

Create average value

Post by lammspillning »

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:)

Code: Select all

<?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 . '\'
                   )';
 
?>
Last edited by Weirdan on Mon Jan 14, 2008 8:06 pm, edited 1 time in total.
Reason: [code] -> [php]
User avatar
jimthunderbird
Forum Contributor
Posts: 147
Joined: Tue Jul 04, 2006 3:59 am
Location: San Francisco, CA

Re: Create average value

Post by jimthunderbird »

Yes, I think you are on the right track, but make sure to use mysql_real_escape_string to filter the $_POST variables to prevent sql injection attack.
Post Reply