-1
<?php if($_SESSION['credits']<=2000) { ?>
                mysql_query("UPDATE $_SESSION['credits'] SET "$_SESSION['credits'] + $_SESSION['credits']" WHERE `id` = "$_SESSION['id']"");
<?php } ?>

I want when clicking a HTML button, while being logged in with a database ID. for it to check for me if my "credits" value (how much currency I have) if it is lower than 2000. If it is, I want it to add some currency to my account.

Is that up there wrong?

Ahmad Othman
  • 167
  • 3
  • 9
  • Use `case` statement in `SET` clause – Viki888 Dec 07 '16 at 14:30
  • anybody home here? did you leave the question? – Funk Forty Niner Dec 07 '16 at 14:45
  • echo your query, tells us what it shows. Your query doesn't make much sense really and we don't know what those session array values are. – Funk Forty Niner Dec 07 '16 at 14:46
  • 2
    ***Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php).*** [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Dec 07 '16 at 14:46
  • I see you're only responding to answers. Post your db schema and values in your edited question. If not, then stick with the people who gave you answers then. I have now left the question, good luck with this. – Funk Forty Niner Dec 07 '16 at 14:55
  • I'm new to this website, so these are comments too..? – Ahmad Othman Dec 12 '16 at 05:10

2 Answers2

0

You need to use a SQL SELECT statement to look up your 'credits' value in the database. For example: SELECT credits FROM < insert tablename here > WHERE < insert condition here >. This is called a SQL query.

Afterwards save the value that you get from the query to a PHP variable and do a comparison (in your case check if it's lower than 2000).

Then, you may use an UPDATE query (SQL) to edit the value in the database.

Example: UPDATE < insert tablename here > SET credits = < insert value here > WHERE < insert condition here >

RobPio
  • 137
  • 8
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – Jay Blanchard Dec 07 '16 at 14:53
  • link has been removed – RobPio Dec 07 '16 at 15:23
0

You have wrong SQL syntax.

UPDATE `table_name` SET `field_name`='new_value' WHERE `another_field`='$some_id';

Don't forget to escape values to prevent SQL-Injection. Best practice is to use PHP::PDO: http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers

Mowshon
  • 939
  • 9
  • 16
  • Just did inserted some stuff, but it's still not working: UPDATE `users` SET `credits`='3000' WHERE `steamid` ='76561198026027024' – Ahmad Othman Dec 07 '16 at 14:50
  • What is the datatype of the credits column? If it is an integer you have to do SET credits = 3000 (no single quotes) – RobPio Dec 07 '16 at 17:23