0

This is my code and i want to save the $result to the database named grade, column name: rating. Help me please. I tried inserting it to using these code:

insert into grade(subject_id,rating)VALUES('$subject_id','$row5') 

but nothings happen

<?php
    $result = mysql_query("SELECT CEILING((($prelim + $midterm + $final ) / 3.0)*100)/100 FROM grade");
    $row5 = mysql_fetch_array($result)
?>
J. Toe
  • 1
  • 1
  • 4
    How are you inserting the `INSERT INTO` statement. Moreover, please don't use `mysql_*` functions. They are deprecated and won't work with new versions of PHP. – Praveen Kumar Purushothaman Apr 06 '16 at 14:00
  • 2
    It sounds like you should start with a tutorial on writing to a database in PHP. There's no code here which performs an insert operation. – David Apr 06 '16 at 14:02
  • Why are you doing a calculation on 3 PHP variables in a sql SELECT statement. PHP does `addition` and `division` quite happily on its own – RiggsFolly Apr 06 '16 at 14:09
  • It is really UnClear what you are actually trying to achieve. Please reword you question to tell us what you are actually trying to do here. – RiggsFolly Apr 06 '16 at 14:11
  • im trying to get the average of those 3 php variable and save it to the database – J. Toe Apr 06 '16 at 14:19

2 Answers2

0

I am assuming a bit here, but if you have the PHP variables $prelim, $midterm, and $final then you do the calculation in PHP and not as an SQL SELECT.

<?php
    $rating = ceil((($prelim + $midterm + $final ) / 3.0)*100)/100;

    $result = mysql_query("INSERT into grade
                                  (subject_id,rating)
                           VALUES('$subject_id','$rating')"
                         );
    if ( $result === FALSE ) {
       echo mysql_error();
       exit;
    }
?>

However

Please dont use the mysql_ database extension, it is deprecated (gone for ever in PHP7) Especially if you are just learning PHP, spend your energies learning the PDO or mysqli_ database extensions, and here is some help to decide which to use

Community
  • 1
  • 1
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
-1

You should do something like that (I expect that $subject_id is primary key and integer).

$result = mysql_query("SELECT CEILING((($prelim + $midterm + $final ) / 3.0)*100)/100 as rating FROM grade"); $row5 = mysql_fetch_assoc($result) $sql = "insert into grade(subject_id,rating) VALUES (" + (int)$subject_id + ", '"+ mysql_real_escape_string($row5['rating']) + "');";

You should always escape SQL input to prevent SQL injection and also if you use single quotes PHP will not replace them with variable value but it use exact string like your '$subject_id'

  • Why are you doing a calculation on 3 PHP variables in a sql SELECT statement. PHP does addition and division quite happily on its own – RiggsFolly Apr 06 '16 at 14:10
  • RiggsFolly -> I just use what J. Toe provide. I don't try to improve his select statement. – user1113000 Apr 06 '16 at 14:12
  • Also why are you running `mysql_real_escape_string()` on a value you have just calculated and therefore know is safe? – RiggsFolly Apr 06 '16 at 14:16
  • RiggsFolly: And you know the source of these values from this short example of code? If don't know where are they come from, you can't trust them :-) – user1113000 Apr 08 '16 at 05:44