计算学生总分,并将该总分插入另一个表中,并将该学生的ID一起

时间:2015-05-07 10:06:04

标签: php mysql

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

这是我现在正在做的方式

<?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;
        }
    }
    }
?>

3 个答案:

答案 0 :(得分:3)

<!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>

答案 1 :(得分:0)

创建2个表,一个用于学生,另一个用于标记。

在一个表格中,只需插入学生姓名和身份证明(唯一),在其他表格中输入所有6个科目标记,即6个列为6个子,1个为学生ID,另一个为自动增量ID列。

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

答案 2 :(得分:0)

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

起草表格就像

学生桌:

id   primarykey  integer
name notnullable nvarchar(255)

主题表

id   primarykey  integer
name notnullable nvarchar(255)

student_subject表

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

例如选择一个主题的标记

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

任何计算都应基于查询,您不想将结果存储在数据库中。您需要数据在数据库中,并且由于它是易失性数据(可以随时更改),因此在需要时可以更轻松,更快速地进行计算。 SumGroupBy,有很多基本的sql关键字可以为您提供帮助。 student_subject table是一个表格,其中将具有特定分数的许多学科的许多学生合并在一起。如果要保存学生的分数,只需在表格中插入ID和分数值即可。

由于这是一个学校项目,因此足以满足您的需要。 将来,请查看准备好的语句

  

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