简体   繁体   中英

How do I store the result of a TOTAL in MySQL so that it can be immediately inserted back into the table

I want to enter data into a table. Using the data that was just entered, I would like to add up the values of 3 columns and then insert that number back into the table into a fourth column.

My initial code:

<?php

$post = file_get_contents('php://input');

$updatedJSONdata = json_decode($post, true);

$numOfStarsLevelOne = $updatedJSONdata["numOfStarsLevelOne"];
$numOfStarsLevelTwo = $updatedJSONdata["numOfStarsLevelTwo"];
$numOfStarsLevelThree = $updatedJSONdata["numOfStarsLevelThree"];


$db = new PDO('mysql:host=localhost;dbname=gametable', 'root', '');
$db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);

$sql = "INSERT INTO gameData(  
                        numOfStarsLevelOne, 
                        numOfStarsLevelTwo, 
                        numOfStarsLevelThree) 
                        VALUES (
                        '$numOfStarsLevelOne',
                        '$numOfStarsLevelTwo',
                        '$numOfStarsLevelThree')";

$testInsert = $db->prepare($sql);
$testInsert->execute();

My guess is I need to make a second query such as

$sqlTwo = "SELECT (numOfStarsLevelOne
+ numOfStarsLevelTwo
+ numOfStarsLevelThree) as total from gameData";

$secondInsert = $db->prepare($sqlTwo);
$secondInsert->execute();

But I'm not sure how I would be able to store the result of the second query so that it can be inserted back into gameData.

I would like to eventually be able to do another query such as

$sqlThree = "INSERT into gamedata(totalNumOfStars) values("result of second query stored as a variable")";

'i think this is what you're looking for:

$total1 = $numOfStarsLevelOne + $numOfStarsLevelTwo + $numOfStarsLevelThree;

$sql = "INSERT INTO gameData(  
                    numOfStarsLevelOne, 
                    numOfStarsLevelTwo, 
                    numOfStarsLevelThree,
                    totalNumOfStars) 
                    VALUES (
                    '$numOfStarsLevelOne',
                    '$numOfStarsLevelTwo',
                    '$numOfStarsLevelThree',
                    '$total1')";

Have you considered this?

INSERT INTO gameData(numOfStarsLevelOne, numOfStarsLevelTwo,  numOfStarsLevelThree) 
    VALUES ($numOfStarsLevelOne, $numOfStarsLevelTwo, $numOfStarsLevelThree,
            $numOfStarsLevelOne + $numOfStarsLevelTwo + $numOfStarsLevelThree
           );

Note: I removed the single quotes, because these are presumably numbers. However, you should be using parameters, not munging query strings.

If you want the value to always be up-to-date, then use a trigger. Or, create a view:

create view v_gamedata as
    select gd.*,
           (numOfStarsLevelOne + $numOfStarsLevelTwo + $numOfStarsLevelThree) as total
    from gamedata;

Or, in the most recent versions of MySQL, you can create a calculated column.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM