从收音机输入计算总分

从收音机输入计算总分

本文介绍了PHP - 存储 &从收音机输入计算总分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设计了一个基于网络的小型系统,它有一个学校评估表,可以向可以访问该系统的特定用户询问一些问题,输入将是无线电类型(1 或 2 或 3 或 4)!代码正在运行,可以将输入插入数据库,但我不知道计算总分并将其存储在数据库中的正确查询,这是当前正在运行的代码:

 <头><?phpif(!isset($_SESSION['login_user']))header("位置:index.html");?><title>问答表单</title><身体><center><form method="post" action="" ><表格样式="宽度:20%" ><tr><th>标准</th><th></th></tr><tr><th>优秀的</th><td>4 </td></tr><tr><th >好 <font size="3" ></font></th><td>3 <font size="4" ></font></td></tr><tr><th >平均 <font size="3" ></font></th><td>2 <字体大小="4" ></font></td></tr><tr><th >可怜的 <font size="3" ></font></th><td>1 </td></tr><字体大小='4'><表格样式="宽度:70%"><tr><th >学校评价<font size="4" ></font></th><tr><th >标准 <font size="4" ></font></th><th>4<font size="4" ></font></th><th>3<font size="4" ></font></th><th>2<font size="4" ></font></th><th>1<font size="4" ></font></th></tr><tr><th>您的出勤率</font></th><td><input type="radio" name="v1" value = "4" checked = "checked"/></td><td><input type="radio" name="v1" value="3"/></td><td><input type="radio" name="v1" value="2"/></td><td><input type="radio" name="v1" value="1"/></td></tr><tr><th >你的成绩 <font size="4" ></font></th><td><input type="radio" name="v2" value = "4" checked = "checked"/></td><td><input type="radio" name="v2" value="3"/></td><td><input type="radio" name="v2" value="2"/></td><td><input type="radio" name="v2" value="1"/></td></tr><tr><th >你的自制力 <font size="4" ></font></th><td><input type="radio" name="v3" value = "4" checked = "checked"/></td><td><input type="radio" name="v3" value="3"/></td><td><input type="radio" name="v3" value="2"/></td><td><input type="radio" name="v3" value="1"/></td></tr></tr><br><a href="evaE.php"><td><input type="submit" name="submit" value="Submit"><input type="reset" name="clear" value="clear" style="width: 70px"></td><?php$total = $v1+ $v2 + $v3;?></表单></中心>

</html>

我使用了这个查询,但它不起作用..有什么帮助吗?

 
解决方案

我不确定我是否理解你在问什么.您想同时存储单独的值和总和,但不想在 PHP 上计算总和?

如果是这样,你可以做一个触发器像这样:

DELIMITER $$在插入之前创建触发器`sum_values`在`Form1`每行开始设置 NEW.TOTAL = NEW.P1 + NEW.P2 + NEW.P3;完$$分隔符;

这样,您只需运行以下查询:

插入表格1(P1,P2,P3)价值观('$v1','$v2','$v3')

...并且触发器应该用总和 P1 + P2 + P3 填充总计"列.

我创建的这个触发器基本上会说:每次有人在‘Form1’中插入一些东西时,用总和 P1 + P2 + P3 填充‘TOTAL’列".

此外,您不应该使用 mysql_query.查看 mysqli 或 PDO_MySQL.mysql_query 已过时,已停产且不安全.

I have designed a small web-based system that have a school evaluation form to ask specific users who can access the system" some questions and the input will be a radio type ( 1 or 2 or 3 or 4)!the code is working and can inserts the input into the database but i don't know the correct query to calculate the total mark and store it in the database, this is currently working code below:

    <?php

   session_start();
   $Load=$_SESSION['login_user'];
   include('../connect.php');
   $sql= "Select name from student where ID='$Load'";
   $username = mysql_query($sql);
    $id=$_SESSION['login_user'];

                if (isset($_POST['submit']))

{

   $v1 = $_POST['v1'];
   $v2 = $_POST['v2'];
   $v3 = $_POST['v3'];
   $total = $_POST['total'];

 mysql_query("INSERT into Form1 (P1,P2,P3,TOTAL)
 values('$v1','$v2','$v3','$total')") or die(mysql_error());
 header("Location: mark.php");
 }


?>


<html>

<head>

<?php


if(!isset($_SESSION['login_user']))

header("Location:index.html");



?>
  <title>Q&A Form</title>

</head>

<body>


    <center><form method="post" action=""  >

    <table style="width: 20%" >
        <tr>
    <th> Criteria </th>
    <th> </th>
    </tr>
    <tr>
    <th> Excellent </th>
    <td >  4 </td>
    </tr>
    <tr>
    <th > Good <font size="3" > </font></th>
    <td>  3 <font size="4" > </font></td>
    </tr>
    <tr>
    <th > Average <font size="3" > </font></th>
    <td >  2 <font size="4" > </font></td>
    </tr>
    <tr>
    <th > Poor <font size="3" > </font></th>
    <td >  1 <font size="4" > </td>
    </tr>




<font size='4'>
    <table style="width: 70%">
        <tr>
<th > School Evaluation <font size="4" > </font></th>

<tr>
<th > Criteria <font size="4" > </font></th>
            <th> 4<font size="4" > </font></th>
            <th> 3<font size="4" > </font></th>
            <th> 2<font size="4" > </font></th>
            <th> 1<font size="4" > </font></th>

        </tr>
<tr>
<th> Your attendance<font size="4" > </font></th>
<td>  <input type="radio" name ="v1" value = "4"    checked = "checked" /></td>
<td>  <input type="radio" name ="v1" value = "3"     /></td>
<td>  <input type="radio" name ="v1" value = "2"     /></td>
<td>  <input type="radio" name ="v1" value = "1"     /></td>
</tr>

<tr>
<th > Your grades  <font size="4" > </font></th>
<td>  <input type="radio" name ="v2" value = "4"    checked = "checked" /></td>
<td>  <input type="radio" name ="v2" value = "3"     /></td>
<td>  <input type="radio" name ="v2" value = "2"     /></td>
<td>  <input type="radio" name ="v2" value = "1"     /></td>
</tr>

<tr>
<th >Your self-control <font size="4" > </font></th>
<td>  <input type="radio" name ="v3" value = "4"    checked = "checked" /></td>
<td>  <input type="radio" name ="v3" value = "3"     /></td>
<td>  <input type="radio" name ="v3" value = "2"     /></td>
<td>  <input type="radio" name ="v3" value = "1"     /></td>
</tr>


        </tr>
    </table>


    <br>
    <a href="evaE.php">  <td><input type="submit" name="submit" value="Submit">

    <input type="reset" name="clear" value="clear" style="width: 70px"></td>
<?php
$total = $v1+ $v2 + $v3;

?>


 </form>
</center>
</div>


</body>
</html>

i used this query but it doesn't work out .. any help please?

 <?php
    $total = $v1+ $v2 + $v3;

    ?>
解决方案

I'm not sure I understand what you're asking. You want to store both the separate values and the sum, but you don't want to do the sum on your PHP?

If so, you could make a trigger, something like that:

DELIMITER $$
CREATE TRIGGER `sum_values` BEFORE INSERT
    ON `Form1`
    FOR EACH ROW
BEGIN
    SET NEW.TOTAL = NEW.P1 + NEW.P2 + NEW.P3;
END$$
DELIMITER ;

This way, you'd only have to run the following query:

INSERT INTO
    Form1
        (P1,P2,P3)
    VALUES
        ('$v1','$v2','$v3')

... and the trigger should fill the 'total' column with the sum P1 + P2 + P3.

This trigger I created will basically says: "everytime someone inserts something into 'Form1', fill the 'TOTAL' column with the sum P1 + P2 + P3".

Also, you should NOT be using mysql_query. Check out mysqli or PDO_MySQL. mysql_query is outdated, was discontinued and is unsafe.

这篇关于PHP - 存储 &amp;从收音机输入计算总分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 17:29