Skip to content

Instantly share code, notes, and snippets.

@RuSPanzer
Created September 22, 2021 13:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RuSPanzer/fc54aa49e7303895f800c056efd8f632 to your computer and use it in GitHub Desktop.
Save RuSPanzer/fc54aa49e7303895f800c056efd8f632 to your computer and use it in GitHub Desktop.
mysql + php review
<?php
/**
+---------------+
| users |
+---------------+
| id int, AI, PK|
| balance int |
+---------------+
+---------------+
| items |
+---------------+
| id int, AI, PK|
| cost int |
+---------------+
*/
function checkoutAction($userId)
{
$list = count($_POST['list']) > 0 ? $_POST['list'] : array();
$user = mysql_fetch_assoc(mysql_query("SELECT `balance` FROM `users` WHERE `id` = $user_id"));
foreach($list as $item){
$row = mysql_fetch_assoc(mysql_query("SELECT cost FROM `items` WHERE `id`={$item['id']}"));
$user['balance'] -= $row['cost'];
mysql_query("UPDATE `users` SET `balance` = {$user['balance']} WHERE `id`={$item['id']}");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment