[英]I want to sum two columns' row and then the value insert the other column's row in the same table using php update. Is it possible?
I want to sum two columns' row and then the value insert the other column;s row in the same table using php update. 我想总结两列的行,然后值插入另一列;使用php update在同一个表中的行。 Is it possible?
可能吗?
I try many time but the updated value in all row are same. 我尝试了很多次,但所有行中的更新值都相同。 Like:
喜欢:
id-1, total-150
id-2, total-150
id-3, total-150
Now what can I do? 现在我该怎么办?
MySQL table looks like this: MySQL表看起来像这样:
My code : 我的代码 :
<?php
$conn= new mysqli("localhost", "root", "", "zidm");
$sql = "SELECT * from exam_model ";
foreach ($conn->query($sql) as $row){
$total= $row['English'] + $row['Math'];
$sql="UPDATE exam_model SET total='$total' ";
mysqli_query($conn,$sql);
}
?>
you missing WHERE clauses. 你错过了WHERE子句。
<?php
$conn= new mysqli("localhost", "root", "", "zidm");
$sql = "SELECT * from exam_model ";
foreach ($conn->query($sql) as $row){
$total= $row['English'] + $row['Math'];
$id = $row['id'];<br>
$sql="UPDATE exam_model SET total='$total' WHERE id = $id";
mysqli_query($conn,$sql);
}
?>
If you need an update on single row you should add a where clause eg: 如果您需要单行更新,则应添加where子句,例如:
<?php
$conn= new mysqli("localhost", "root", "", "zidm");
$sql = "SELECT * from exam_model ";
foreach ($conn->query($sql) as $row)
{
$total= $row['English'] + $row['Math'];
$id = $row['id'];
$sql="UPDATE exam_model SET total='$total' WHERE id = $id";
mysqli_query($conn,$sql);
}
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.