PHP MySQLi - update multiple data with some formula in one process

Kharisma Wahyudin

How to update many rows in a mysqli table? Here my table (tbmath)

|id | code    |a   | b | c   |
| 1 |  1      |11 | 11 |     |
| 2 |  1      |12 | 22 |     |
| 3 |  1      |13 | 33 |     |
| 1 |  2      |11 | 11 |     |
| 2 |  2      |12 | 22 |     |
| 3 |  2      |13 | 33 |     |

... Code is for the operation rules Code 1 = a + b; Code 2 = a x b;

Im try'n to update c with code = 1 first, but i have the wrong result, hope somebody can help me to fix this code or give me the right why to solve the problem. Here my code :

<?php
 $db_host="localhost";
 $db_user="root";
 $db_pass="";
 $db_database="test";
 $con = new 
 mysqli($db_host,$db_user,$db_pass,$db_database);

 $sql=mysqli_query($con,"SELECT * FROM tbmath 
 WHERE code = 1");
 while ($a=mysqli_fetch_assoc($sql)) 
{
    $c=$a['a']+$a['b'];
    mysqli_query($con,"UPDATE `tbmath` SET `c` = '$c' 
    WHERE `code` = 1");
}
 ?>

The result is :

|id | code    | a | b  | c |
| 1 |  1      |11 | 11 |46 |
| 2 |  1      |12 | 22 |46 |
| 3 |  1      |13 | 33 |46 |
| 1 |  2      |11 | 11 |   |
| 2 |  2      |12 | 22 |   |
| 3 |  2      |13 | 33 |   |

My code just calculating the last result 13 + 33.

What's the right PHP code for my case ? I hope somebody can help me...

FGDeveloper

You must use id field param in your where criteria, not code field. Because you getting code equals 1 data set.

$db_host="localhost";
 $db_user="root";
 $db_pass="";
 $db_database="test";
 $con = new 
 mysqli($db_host,$db_user,$db_pass,$db_database);

 $sql=mysqli_query($con,"SELECT * FROM tbmath 
 WHERE code = 1");
 while ($a=mysqli_fetch_assoc($sql)) 
{
    $c=$a['a']+$a['b'];
    mysqli_query($con,"UPDATE `tbmath` SET `c` = '$c' 
    WHERE `id` = $a['id']");
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I update multiple rows in MySQL using PHP and the mysqli_query() command by passing dynamic variables?

From Dev

php mysqli update query

From Dev

More efficient - multiple SQL queries or one query and process in php?

From Dev

Multiple SQL objects in PHP/MySQLi

From Dev

how to update multiple data in one go in elastica php?

From Dev

How to update two select boxes using one mysqli query in php

From Dev

PHP not inserting data into MYSQLi

From Dev

How to update multiple data with implode in PHP

From Dev

Formula to Search for Data in One Column, then Apply Formula

From Dev

PHP (MySQLi) - Displaying Data in multiple locations

From Dev

PHP MySqli Multiple query & while in one page - help to simplifying code

From Dev

Uploading multiple images one to each row PHP and Mysqli db

From Dev

How to check one multi-dimensional array values against another multi-array and update bulk data depending on results, using PHP and MySQLi?

From Dev

How can I update multiple rows in MySQL using PHP and the mysqli_query() command by passing dynamic variables?

From Dev

php mysqli update query

From Dev

Php : Mysqli - Data Type and Where Conditions with Multiple ID

From Dev

Getting Data From Multiple MySQL Tables Using PHP and mysqli

From Dev

Copying data from one table to another using php mysqli fails

From Dev

PHP mysqli search multiple tables

From Dev

More efficient - multiple SQL queries or one query and process in php?

From Dev

Creating multiple while loops for one array in php and mysqli

From Dev

how to update multiple data in one go in elastica php?

From Dev

How to update multiple data with implode in PHP

From Dev

Formula to Search for Data in One Column, then Apply Formula

From Dev

PHP - include/require/insert some data from one file to another

From Dev

rabbitmq and php - Process multiple queues with one worker (broker)

From Dev

PHP/Mysqli check before update

From Dev

multiple mysqli delete codes on one PHP page

From Dev

PHP / MySQL / AJAX - Update multiple data

Related Related

  1. 1

    How can I update multiple rows in MySQL using PHP and the mysqli_query() command by passing dynamic variables?

  2. 2

    php mysqli update query

  3. 3

    More efficient - multiple SQL queries or one query and process in php?

  4. 4

    Multiple SQL objects in PHP/MySQLi

  5. 5

    how to update multiple data in one go in elastica php?

  6. 6

    How to update two select boxes using one mysqli query in php

  7. 7

    PHP not inserting data into MYSQLi

  8. 8

    How to update multiple data with implode in PHP

  9. 9

    Formula to Search for Data in One Column, then Apply Formula

  10. 10

    PHP (MySQLi) - Displaying Data in multiple locations

  11. 11

    PHP MySqli Multiple query & while in one page - help to simplifying code

  12. 12

    Uploading multiple images one to each row PHP and Mysqli db

  13. 13

    How to check one multi-dimensional array values against another multi-array and update bulk data depending on results, using PHP and MySQLi?

  14. 14

    How can I update multiple rows in MySQL using PHP and the mysqli_query() command by passing dynamic variables?

  15. 15

    php mysqli update query

  16. 16

    Php : Mysqli - Data Type and Where Conditions with Multiple ID

  17. 17

    Getting Data From Multiple MySQL Tables Using PHP and mysqli

  18. 18

    Copying data from one table to another using php mysqli fails

  19. 19

    PHP mysqli search multiple tables

  20. 20

    More efficient - multiple SQL queries or one query and process in php?

  21. 21

    Creating multiple while loops for one array in php and mysqli

  22. 22

    how to update multiple data in one go in elastica php?

  23. 23

    How to update multiple data with implode in PHP

  24. 24

    Formula to Search for Data in One Column, then Apply Formula

  25. 25

    PHP - include/require/insert some data from one file to another

  26. 26

    rabbitmq and php - Process multiple queues with one worker (broker)

  27. 27

    PHP/Mysqli check before update

  28. 28

    multiple mysqli delete codes on one PHP page

  29. 29

    PHP / MySQL / AJAX - Update multiple data

HotTag

Archive