There is a code:

<?php $servername = "localhost"; $username = "name"; $password = "password"; $dbname = "dbname"; $conn = mysqli_connect($servername, $username, $password, $dbname); if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } $sql = "UPDATE elitprice SET price=price*1.30 WHERE price <=10"; $sql = "UPDATE elitprice SET price=price*1.25 WHERE price FROM 11 TO 30"; $sql = "UPDATE elitprice SET price=price*1.22 WHERE price FROM 31 TO 50"; $sql = "UPDATE elitprice SET price=price*1.20 WHERE price FROM 51 TO 70"; $sql = "UPDATE elitprice SET price=price*1.18 WHERE price FROM 71 TO 80"; $sql = "UPDATE elitprice SET price=price*1.15 WHERE price FROM 81 TO 95"; $sql = "UPDATE elitprice SET price=price*1.12 WHERE price FROM 96 TO 180"; $sql = "UPDATE elitprice SET price=price*1.10 WHERE price >=181"; if (mysqli_query($conn, $sql)) { echo "Record updated successfully"; } else { echo "Error updating record: " . mysqli_error($conn); } mysqli_close($conn); ?> 

Performs only the latest margin. How to rebuild to price depending on the price?

    3 answers 3

     <?php $servername = "localhost"; $username = "name"; $password = "password"; $dbname = "dbname"; $conn = mysqli_connect($servername, $username, $password, $dbname); if(!$conn) { die("Connection failed: ".mysqli_connect_error()); } $sqls = array( "UPDATE elitprice SET price=price*1.30 WHERE price <=10", "UPDATE elitprice SET price=price*1.25 WHERE price FROM 11 TO 30", "UPDATE elitprice SET price=price*1.22 WHERE price FROM 31 TO 50", "UPDATE elitprice SET price=price*1.20 WHERE price FROM 51 TO 70", "UPDATE elitprice SET price=price*1.18 WHERE price FROM 71 TO 80", "UPDATE elitprice SET price=price*1.15 WHERE price FROM 81 TO 95", "UPDATE elitprice SET price=price*1.12 WHERE price FROM 96 TO 180", "UPDATE elitprice SET price=price*1.10 WHERE price >=181"); foreach($sqls as $sql) { if(mysqli_query($conn, $sql)) { echo "Record updated successfully"; } else { echo "Error updating record: ".mysqli_error($conn); } } mysqli_close($conn); ?> 
    • Hastened with the answer. In this case, the script marks one position several times. That is, if there was a price of 60.36, it will mark 20% to 72.432, then once again mark 18% to 85.46976 and so on, while the number falls into the requests - Torba
    • this is another question :) the first one was about sequential requests - spart
    • Blunted, I admit. It turns out I need to execute all in one request? - Torba

    Try better this:

     $sql = array(); $sql[] = "UPDATE elitprice SET price=price*1.30 WHERE price <=10"; $sql[] = "UPDATE elitprice SET price=price*1.25 WHERE price FROM 11 TO 30"; $sql[] = "UPDATE elitprice SET price=price*1.22 WHERE price FROM 31 TO 50"; $sql[] = "UPDATE elitprice SET price=price*1.20 WHERE price FROM 51 TO 70"; $sql[] = "UPDATE elitprice SET price=price*1.18 WHERE price FROM 71 TO 80"; $sql[] = "UPDATE elitprice SET price=price*1.15 WHERE price FROM 81 TO 95"; $sql[] = "UPDATE elitprice SET price=price*1.12 WHERE price FROM 96 TO 180"; $sql[] = "UPDATE elitprice SET price=price*1.10 WHERE price >=181"; if (mysqli_multi_query($conn, implode(";", $sql))) { echo "Records updated successfully"; } else { echo "Error updating records: " . mysqli_error($conn); } 

      Fine. thank

      The first option came up, I just forgot to indicate that I use mariadb, so here is the answer:

       <?php $servername = "localhost"; $username = "name"; $password = "password"; $dbname = "dbname"; $conn = mysqli_connect($servername, $username, $password, $dbname); if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } $sqls = array( "UPDATE elitprice SET price=price*1.30 WHERE price <=10", "UPDATE elitprice SET price=price*1.25 WHERE price >=11 and price <=30", "UPDATE elitprice SET price=price*1.22 WHERE price >=31 and price <=50", "UPDATE elitprice SET price=price*1.20 WHERE price >=51 and price <=70", "UPDATE elitprice SET price=price*1.18 WHERE price >=71 and price <=80", "UPDATE elitprice SET price=price*1.15 WHERE price >=81 and price <= 95", "UPDATE elitprice SET price=price*1.12 WHERE price >=96 and price <= 180", "UPDATE elitprice SET price=price*1.10 WHERE price >=181"); foreach($sqls as $sql) { if(mysqli_query($conn, $sql)) { echo "Record updated successfully"; } else { echo "Error updating record: ".mysqli_error($conn); } } mysqli_close($conn); ?> 

      Thanks to all!

      • By the way, if your prices in the database can be fractional, then you skip some lines on the borders of your conditions. For example, the price range from 30.01 to 30.99 does not fall under any condition. and so actually for each interval - Mike
      • therefore, the price column is assigned the type decimal (19,0) - Torba