Click here to Skip to main content
15,889,281 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How can I update echo $_SESSION['balance'] in real time when i make changes into the database? I am trying to build a send and recieve system but when i send an ammount it doesnt get updated in real time.. How can i acheve that?


What I have tried:

<?php
session_start();
include "db_conn.php";
if (!$conn) {
	echo "Connection failed!";
}
$a = $_SESSION['id'];
$b = $_SESSION['balance'];
$c = $_REQUEST['amount'];
$sql = "UPDATE users SET balance=balance - ($c) WHERE id=($a)";
if ($conn->query($sql) === TRUE) {
  echo "Record updated successfully";
} else {
  echo "Error updating record: " . $conn->error;
}

 ?>
Posted
Updated 31-Oct-21 0:52am
Comments
Richard Deeming 29-Oct-21 4:50am    
You've got much bigger problems to worry about!

Your code is vulnerable to SQL Injection[^]. NEVER use string concatenation/interpolation to build a SQL query. ALWAYS use a parameterized query.

PHP: SQL Injection - Manual[^]
PHP: Prepared statements and stored procedures - Manual[^]

1 solution

As other comments have stated you need to learn about SQL injection.

What you are trying to do can be achieved by using web sockets or http long polling

I would recommend that you use web sockets, using Ratchet, Ratchet is a PHP library providing developers with tools to create real time, bi-directional applications between clients and servers over Web-sockets.

Good luck.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900