【问题标题】:calculate student total marks and insert that total in another table with the id of that student together计算学生总分并将该总分与该学生的 id 一起插入到另一个表中
【发布时间】:2015-05-07 10:06:03
【问题描述】:

我正在开发一个简单的学生信息系统,现在我有 300 名学生和六个科目,所以当我想添加每个学生获得的分数时,我使用 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;
        }
    }
    }
?>

【问题讨论】:

  • 这是一个学习练习还是一个真正的项目?你大概有多少时间?你在哪方面有困难?
  • 这是一个文凭项目,我也不擅长 php,因此为什么我更喜欢 mysqli 函数,我真正需要帮助的部分是如何为每个学生一次添加所有六个科目,请帮助
  • 虽然看到您尝试过某些东西很有用,但我建议您删除所有这些代码(这似乎与您的实际问题无关)并用适当的 DDL(和/或 sqlfiddle ) 和相应的期望结果

标签: php mysql


【解决方案1】:
<!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>

【讨论】:

  • 再次编辑,如果需要更多帮助,请询问并做一个简短的例子
  • 明白了,选择框是完美的,所以我需要的不是更新查询,而是从选择框中选择一个学生,然后从那里添加六个科目,现在php脚本到一次容纳或插入所有六个科目并将它们存储在具有特定学生 ID 的数据库中是我不知道从哪里开始的事情
  • 是的,这很好!需要更新查询,因为您可以将字段留空并通过添加您输入的值来更新它们。如果您愿意,我会为您编写一个带有 cmets 等的脚本?
  • 显然你必须把你自己的 MySQL 查询值放在那里,但这符合你想要的,我建议使用这个/学习语法,直到你感到舒服并且我确定那么你将拥有知识来做你需要做的事情
【解决方案2】:

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

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

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

【讨论】:

  • 输入 1 个学生姓名,然后您将获得该学生的 id,使用它您可以一次输入所有 6 个科目的分数。
  • 我唯一不知道的是如何编写将同时插入所有六个主题的 php 脚本。
  • 如果 student id 在 db 中不存在,则将其插入为:Insert into marks (student_id,sub1,sub2,sub3,sub4,sub5,sub6) values ('1',60,70,90,50,75,80) 如果 student id 存在 那么你可以 更新或让用户不插入记录
【解决方案3】:

编辑:刚刚注意到这个问题是 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 和分数值插入此表即可。

由于这是一个学校项目,这应该足以让您完成它。 以后看看prepared statements

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-21
    • 1970-01-01
    • 2021-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-23
    相关资源
    最近更新 更多