php 计算学生总分并将该总分与该学生的 id 一起插入另一个表中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30097881/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 21:32:29  来源:igfitidea点击:

calculate student total marks and insert that total in another table with the id of that student together

phpmysql

提问by punji

I'm developing a simple student information system, for now i have 300 students and six subjects, so when i want to add marks obtained by each student i use html form and php script to add those marks, for each student i add marks for six subjects that is one subject at a time, so i'm asking if there is the way where by php can allow me retrieve one student and add all the marks for the six subjects at once and then take another and so on. Also i want to calculate total marks for each student and store those total in another table with respective student id so that i can know who is the first student and who is the last by using that total marks of each student.

我正在开发一个简单的学生信息系统,现在我有 300 名学生和 6 个科目,所以当我想添加每个学生获得的分数时,我使用 html 表单和 php 脚本来添加这些分数,我为每个学生添加分数六个科目,一次一个科目,所以我问是否有办法通过 php 可以让我检索一名学生并一次添加六个科目的所有分数,然后再取另一个等等。此外,我想计算每个学生的总分,并将这些总分存储在另一个带有各自学生 ID 的表中,以便我可以通过使用每个学生的总分来知道谁是第一个学生,谁是最后一个学生。

here is the way i'm doing right now

这是我现在正在做的方式

<?php error_reporting(E_ALL ^ E_NOTICE); ?>
<html>
<body>
<div>
<div>
<form action="connmarks.php" method="post">
    <table>
    <tr><td colspan="6">&nbsp;</td></tr>
    <tr><td><p>Adding Student Results</p></td></tr>
    <tr>
    <td width="9%">Student Code<?php echo $mstudentcode;?></td>
    <td width="17%"><input name="student_code" type="text" size="30" value="<?php echo $studentcode;?>" /></td></tr>
    <tr>
    <td width="10%">Subject Code<?php echo $msubjectcode;?></td>
    <td width="18%"><input name="subject_code" type="text" size="30"  value="<?php echo $subject_code;?>"/></td></tr>
    <tr>
    <td width="12%">Marks<?php echo $mmark;?></td>
    <td width="34%"><input name="mark" type="text" size="30" value="<?php echo $mark;?>"/></td>

    </tr>
      <td>&nbsp;</td>
      <td>&nbsp;
      </td>
    </tr>
     <tr><td colspan="4">&nbsp;</td></tr>
      <tr><td colspan="6">&nbsp;</td></tr>
     <tr>
    <td>&nbsp;</td><td colspan="6"><input type="submit" name="save" value="Add Marks" /></td>
     </tr>
     <tr><td colspan="6"><?php echo $sms1.$sms.$sms2;?></td></tr>
    </table>
    </form>
    </div>
    <div id="footer">Copyright <?php echo date("Y", time()); ?></div>
</div>
</body>
</html>


<?php error_reporting(E_ALL ^ E_NOTICE); ?>
<?php
session_start();
if(isset($_POST['save']))
{
//  validating student code
    if(empty($_POST['student_code']))
    {
        $mstudentcode='<font color="red"><b>**</b></font>';
    }
    else
    {
        $student_code=$_POST['student_code'];
    }
//  validation for kiswahili subject
    if(empty($_POST['subject_code']))
    {
        $msubjectcode='<font color="red"><b>**</b></font>';
    }
    else
    {
        $subject_code=$_POST['subject_code'];
    }
// validating english subject
    if(empty($_POST['mark']))
    {
        $mmark='<font color="red"><b>**</b></font>';
    }
    else
    {
        $mark=$_POST['mark'];
    }
// checking if there is any error message, if no error proceed, if there is error, display the error
//  Then exit the script and redirect at the same page
    if($mstudentcode||$msubjectcode||$mmark||$sms)
    {
        $sms1='<font color="red"><b>Error found,please check **</b></font><br/>';
        include 'addmarks.php';
        exit;
    }
// if there is no error include connection file
    if($student_code&&$subject_code&&$mark)
    {
//      include 'mysqli_connect.php';
      require_once ('../../mysqli_connect.php');
        $addmarks= "insert into result(student_code,subject_code,mark) values ('".$student_code."','".$subject_code."','".$mark."')";
        $k = mysqli_query($dbc, $addmarks);
        if ($k)
        {
       $sms1='<font color="green"><b>Student Marks Submitted Successfully</b></font><br/>';
            include 'addmarks.php';
                        exit;
        }
        else
        {
            $sms1='<font color="red"><b>Failed To Add Student Marks</b></font><br/>';
            include 'addmarks.php';
                        exit;
        }
    }
    }
?>

回答by Kenziiee Flavius

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>

    <form method="post">
        <!-- Displays all users in database in a select option -->
        <select name="students">
            <option>Student 1</option>
            <option>Student 2</option>

            <?php
                //This code below is what you will need to use for yours to pull values out of the database(changing the values to suit yours obviously).

                // $query = "SELECT * FROM students ORDER BY student_name ASC";
                // $result = mysqli_query($conn,"$query");

                // while($row = mysqli_fetch_assoc($result)){
                //     echo "<option>" . $row['student_name'] . "<br></option>";
                // }
            ?>

        </select><br>

        <!-- All the different input fields for maths, english and science -->

        <input type="text" name="eng_grade" value="" placeholder="Enter English Grade"><br>
        <input type="text" name="math_grade" value="" placeholder="Enter Maths Grade"><br>
        <input type="text" name="science_grade" value="" placeholder="Enter Science Grade"><br>
        <input type="submit" name="submit" value="Submit">
    </form>
    <?php 

        //If submit is pressed
        if (isset($_POST['submit'])) {

            //this gets the value of the student name from the select box and stores it as $student
            $student = $_POST['students'];

            //These gets the values stored in the inputs above and store them in the 3 vairables
            $english = $_POST['eng_grade'];
            $maths = $_POST['math_grade'];
            $science = $_POST['science_grade'];

            //this is a mysql query that updates the data with whatever you put into the database(to add to existing tables you will have to dig abit deeper and create your
            //database with all the correct fields!
            $query = "UPDATE students SET maths_grade = '$maths', $english_grade = '$english', science_grade = '$science' WHERE student_name = '$student'";
            $update = mysqli_query($conn,  "$query"); //<-- this inserts the values into the database, changing the current #null value (null means nothing is in it)
        }

    ?>
</body>
</html>

回答by Nehal

Create 2 tables, one for student and other for marks.

创建 2 个表,一个用于学生,另一个用于分数。

In one table, just insert student name and id(unique), and in other table student all 6 subject marks i.e. 6 columns for 6 sub, 1 for student id, and other one as auto increment id column.

在一张表中,只需插入学生姓名和 id(唯一),在另一张表中插入所有 6 个主题标记,即 6 列用于 6 个子,1 列用于学生 id,另一列作为自动递增 id 列。

So using that student id you can retrieve all the subject marks of a particular student.

因此,使用该学生 ID,您可以检索特定学生的所有科目分数。

回答by abr

Edit: Just noticed this question is 3 years old w00t!?

编辑:刚刚注意到这个问题是 3 岁 w00t!?

Drafting tables would be something like

起草表格将类似于

student table:

学生表:

id   primarykey  integer
name notnullable nvarchar(255)

subject table

主题表

id   primarykey  integer
name notnullable nvarchar(255)

student_subject table

student_subject 表

 id         primarykey integer
 student_id foreignkey notnullable unique integer
 subject_id foreignkey notnullable unique integer
 mark                  notnullable        double(2,2)

E.g. Select the marks of a subject

例如选择一个主题的标记

Select student.name, subject.name, subject_student.mark from subject_student
inner join student on student.id = subject_student.student_id
inner join subjecton subject.id = subject_student.subject_id
where student_id = X; // X is the id of the student you want

Any calculation should be based on query, you don't want to store results in the database. You want data in the database and since it's volatil data (can be changed anytime), it's easier and faster to calculate whenever required. Sum, GroupBy, there are a ton of basic sql keywords that can help you. student_subject tableis the table where it combines that Many students in Many subjects with a certain mark. If you want to save a student's mark, you just Insert into this table the id's and the mark value.

任何计算都应该基于查询,您不想将结果存储在数据库中。您需要数据库中的数据,并且由于它是易失性数据(可以随时更改),因此可以在需要时更轻松、更快速地进行计算。Sum, GroupBy, 有大量基本的 sql 关键字可以帮助您。student_subject table是一张表格,它结合了许多科目的许多学生,并获得了一定的分数。如果要保存学生的分数,只需将 id 和分数值插入到此表中。

Since it's a school project, this should be suffice for you to work it out. In the future, take a look at prepared statements

既然是学校项目,这应该足够你解决了。以后,看看准备好的语句

http://php.net/manual/en/book.pdo.php

http://php.net/manual/en/book.pdo.php