我正在尝试添加来自行“‘scores”的值。
例如,如果我有6行,每行的值都是1。我希望能够回显rows = 6的->值。
+=对我不起作用:我仍然只得到值本身,例如1,2,3,4,5,6,7,但我想要它的和,比方说1+2+3+4+5+6+7=28。
谢谢
<?php include("connect.php"); ?>
<html>
<head>
<title>Score Predictions</title>
</head>
<body>
<div id = "id">
<?php
$query = "SELECT * FROM test";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)) {
$id = $row['id'];
$home = $row['home'];
$away = $row['away'];
}
?>
<?php
if (isset($_POST['submit'])) {
$x = $_POST["test"];
mysql_query("INSERT INTO test (home, away, score) VALUES ('$home', '$away', '$x')");
}
?>
<?php echo $home," - ",$away; ?>
<form method = 'post' action = 'http://albsocial.us/test/index.php'>
<select name = 'test'>
<option value = "" selected = 'selected'></option>
<option VALUE = '1'>1</option>
<option VALUE = 'X'>X</option>
<option VALUE = '2'>2</option>
</select>
<INPUT TYPE = 'submit' name = 'submit' />
</form>
<?php
$query = "SELECT * FROM test";
$result = mysql_query($query);
while($row = mysql_fetch_array($result)) {
$id = $row['id'];
$score = $row['score'];
if ($score == "1") {
echo $sum += $score - 1;
}
}
?>
</div>
</body>
</html>发布于 2013-06-13 12:13:18
$sum=0;
while($row=mysql_fetch_array($result)){
$id = $row['id'];
$score = $row['score'];
if ($score == "1"){
$sum = $sum+$score;
}
}
echo $sum;尝尝这个。它对所有$score值求和。
发布于 2013-06-13 12:14:00
您必须删除if条件并将数据库值添加到$sum变量
$sum = 0;
while($row=mysql_fetch_array($result)){
$id = $row['id'];
$score = $row['score'];
$sum += (int)$score;
}
echo $sum;发布于 2013-06-13 12:35:57
这里有几个问题,因为其他答案大多是解决的,但要明确的是:
if $score == 1似乎与你的目的无关。你是说if $id == 1,还是你只是想忽略零?(任何+0保持不变,所以你不需要这样做),,
$sum += $score-1,,
echo一次。目前,您的每个数据库行都有一个echo,这就是为什么您会看到多个数字输出。SELECT SUM(score) AS total_score FROM test或SELECT SUM(score) AS total_score FROM testhttps://stackoverflow.com/questions/17079073
复制相似问题