我需要php / mysql来获取3个分配标记(由用户插入),将它们加在一起然后除以3以获得平均分数。然后需要将此平均标记插入表格中的等级列。
这是代码;
$a1 = $_REQUEST['ass1'];
$a2 = $_REQUEST['ass2'];
$a3 = $_REQUEST['ass3'];
$id = $_REQUEST['id'];
$t = '3';
$sum = "'$a1' + '$a2' + '$a3'";
$average = (int)($sum / $t);
mysql_select_db("kackieco_final",$con);
$q = "UPDATE grades SET ass1=$a1, ass2=$a2, ass3=$a3, grade=$average WHERE id = $id";
答案 0 :(得分:1)
$ sum是一个包含3个数字和数学运算符的字符串。
字面上
'1' + '2' + '3'
// string(15) "'1' + '2' + '3'"
删掉引号
$sum = $a1 + $a2 + $a3;
// int(6)
你会得到你的int
答案 1 :(得分:0)
这是一个示例,在您的代码中,您将值视为字符串:
<?php
//Example values, simulate a request
$_REQUEST['id']='25';
$_REQUEST['ass1']='17';
$_REQUEST['ass2']='28';
$_REQUEST['ass3']='32';
//Get and check the values from the user
$values=array();
$values['id'] = (isset($_REQUEST['id']) && is_numeric($_REQUEST['id']))?$_REQUEST['id']:0;
$values[1] = (isset($_REQUEST['ass1']) && is_numeric($_REQUEST['ass1']))?$_REQUEST['ass1']:0;
$values[2] = (isset($_REQUEST['ass2']) && is_numeric($_REQUEST['ass2']))?$_REQUEST['ass2']:0;
$values[3] = (isset($_REQUEST['ass3']) && is_numeric($_REQUEST['ass3']))?$_REQUEST['ass3']:0;
$values['t'] = 3;
//one line add the variables together then devide by 3 and then round down or up depending on whats closest
$values['average'] = round(( ($values[1] + $values[2] + $values[3]) / $values['t'])); //26
$q = "UPDATE grades SET ass1={$values[1]}, ass2={$values[2]}, ass3={$values[3]}, grade={$values['average']} WHERE id = {$values['id']}";
echo $q;
//UPDATE grades SET ass1=17, ass2=28, ass3=32, grade=26 WHERE id = 25
?>