I
I
Ibrahim Tubaev2021-06-24 13:30:57
PHP
Ibrahim Tubaev, 2021-06-24 13:30:57

How to subtract a number from one user and add it to another?

I create a simple wallet (school project), put up a money transfer form, the user is required to have his card number, the amount of the transfer and the card to which he will send this amount. I threw the form as it should: the action file action.php. The connection is configured, but the MYSQL code itself does not work. From the users table in the amount column, you need to subtract the amount that the user enters ($summa is a variable from the form), and add that amount to another user. Here is the code:

<?php
 $client= $_POST['client'];
 $summa= $_POST['summa'];
 $shop= $_POST['shop'];
 
 $conn = mysqli_connect("localhost", "root", "89053511328It");

  $mysqli->query("SELECT * FROM `users`");
 $mysqli->query("UPDATE users SET amount = '',  amount = amount - '$summa' WHERE id = '$client'");
  $mysqli->query("UPDATE users SET amount = '',  amount = amount + '$summa' WHERE id = '$shop'");
 

?>


Here is the entire HTML page code (maybe there is an error?)
<!DOCTYPE html>
<html lang="ru">
<head>
    <link rel="stylesheet" type="text/css" href="style.css">
    <script src="js.js"></script>
    <meta charset="UTF-8">
    <title>Оплата MakBank</title>
    <meta name="theme-color" content="#eb2d6e">
</head>
<body>
<form method="POST" name="money" action="action.php">
<div class="background">
  <div class="container">
    <div class="screen">
      <div class="screen-header">
        <div class="screen-header-left">
          <div class="screen-header-button close"></div>
          <div class="screen-header-button maximize"></div>
          <div class="screen-header-button minimize"></div>
        </div>
        <div class="screen-header-right">
          <div class="screen-header-ellipsis"></div>
          <div class="screen-header-ellipsis"></div>
          <div class="screen-header-ellipsis"></div>
        </div>
      </div>
      <div class="screen-body">
        <div class="screen-body-item left">
          <div class="app-title">
            <span>ОПЛАТА</span>
            <span>MakBank</span>
          </div>
          <div class="app-contact">Возникли проблемы? : +7 963 140 35 65</div>
        </div>
        <div class="screen-body-item">
          <div class="app-form">
            <div class="app-form-group">
              <input class="app-form-control" placeholder="НОМЕР КАРТЫ ПОКУПАТЕЛЯ" value="" name="client">
            </div>
            <div class="app-form-group">
              <input class="app-form-control" placeholder="СУММА К ОПЛАТЕ" name="summa">
            </div>
            <div class="app-form-group">
              <input class="app-form-control" placeholder="НОМЕР КАРТЫ ПРОДАВЦА" name="shop">
            </div>
            <div class="app-form-group buttons">
              <button type="submit" class="app-form-button">ОПЛАТИТЬ</button>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>
</form>
</body>
</html>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
Nikita Reshetnyak, 2021-06-24
@ibrakhim20070618

Last time similar did nested request. That is, the current amount was pulled out by the select, roughly speaking like this

<?php
 $mysqli->query("UPDATE users SET amount = (SELECT amount FROM users WHERE id = '$client') - '$summa'";
 $mysqli->query("UPDATE users SET amount = '',  amount = (SELECT amount FROM users WHERE id = '$shop') + '$summa'";
?>

Perhaps this is not entirely correct, but at that time it didn’t start differently for me, and I didn’t search for a long time

A
Akina, 2021-06-24
@Akina

It's better to do everything in one request:

UPDATE users u1
CROSS JOIN users u2
SET u1.amount = u1.amount - $summa,
    u2.amount = u2.amount + $summa
WHERE u1.id = $client
  AND u2.id = $shop;

First, one request is easier. Even if the request itself is more complicated.
Secondly, if, for example, there is a CHECK (amount >= 0) constraint in the table structure , and the amount is such that the client's balance will go negative - in this case, none of the records will be changed.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question