在动态表中添加mysql数据库中行的值

时间:2015-08-14 18:13:01

标签: php mysql

我有一个表格,其中包含随操作而变化的字段数量。将其简化为下表

STUDENT MATHS   PHYSICS BIOLOGY TOTAL

PAUL      89      87      65    

JOHN      56      89      54    

目的是总结每个学生在总栏目中的分数。我已经写了一个小的PHP代码,但是并没有给我总结果。我也尝试了其他不适用的变体(我可能不想发布)。请提出任何建议。

$table = 'scores';
$total = '';
$result = mysql_query("SELECT * FROM $table");

if (!$result) {
    die("Query to show fields from table failed");
}

$fields_num = mysql_num_fields($result);
//table to display student scores 
echo "<table><tr>";

for ($i=0; $i<$fields_num; $i++) {
   $field = mysql_fetch_field($result);
   echo "<th>$field->name</td>";
} 

echo "</tr>\n";

while ($row = mysql_fetch_object($result)) {
    $student_id=$row->student_id;
    echo "<tr>";
    foreach($row as $cell) {
        echo "<td> $cell </td>";
    }
    // calcuate the total score of each record
    $total =($cell + $total);
    echo "</tr>\n";
    echo 'The total = '.$total;
}

// functio to insert the total scores t the table
function tScore($table, $student_id, $total) {
    $sqlCommand = "UPDATE $table SET total='$total' WHERE student_id='$student_id' "; 
    $query = mysql_query($sqlCommand) or die ('Sorry something went wrong while inserting Subjects Scores'); 

    return $total;
    return $student_id;
}
?>

1 个答案:

答案 0 :(得分:0)

您的查询应该是这样的

angular.element(element).controller().constructor.name