Answer the question
In order to leave comments, you need to log in
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'");
?>
<!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
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'";
?>
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;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question