简体   繁体   English

我想总结两列的行,然后使用php update将值插入同一个表中的另一列的行。 可能吗?

[英]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表看起来像这样: 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.

相关问题 如何在MySQL表中为同一行的两列插入值 - How to insert value for two columns of same row in MySQL table 通过相同ID的其他表中另一行的值更新表中的行 - update row of table by value of other row in other table by same id SQL查询以查找某列值等于同一行中其他两列之和的行 - SQL query to find rows where a column value is equal to the sum of tow other columns in the same row 从MySQL表的一列获取字符串值,并更新同一行的另一列值 - Get string value from one column of MySQL Table and update its other column value of same row 如何插入两个表; 一个将插入 1 行,另一个将插入多行,两个表的一列具有相同的值 - how to insert into two tables; one will insert 1 row and the other one will insert multiple rows, the two tables has one column with the same value 我想将两个不同的表格行值插入到单个表格中, - I want to insert two different table row values into single table, 我希望这段代码将数据插入到数据库表的同一行 - I want this code to insert data into same row of database table 只想在另一张表中插入一行 - only want to insert one row into other table 如何使用mysqli和PHP用代表另一行ID的变量更新两列的不同行? - How to update different rows of two columns with variables which represent the id of the other row using mysqli and PHP? 将行中的3列求和总计-MySQL和PHP - Sum 3 columns in row for a total column - MySQL & PHP
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM