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):
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);
you can use: SELECT avg(vote_points) as average where something=is_something avg() gets the average of all the values in the result..