-1

I have done that when I push button it should insert data into mysql database but it only inserts it when I reload page. Here Is the code:

<script>
function minuscredits()
{
    alert("hah");
    <?php
    mysql_query("UPDATE `users` SET credits=credits-1 WHERE user_id=".$_SESSION['user']);
    ?>
}
</script>

When I push button I get alert but not insert into database only if I reload page it inserts successfully.

2
  • you can't mix php and javaScript like that Commented Jul 28, 2016 at 21:58
  • Mixing PHP and JS doesn't work. Commented Jul 28, 2016 at 21:58

1 Answer 1

1

PHP runs on the server. It will run

<?php
    mysql_query("UPDATE `users` SET credits=credits-1 WHERE user_id=".$_SESSION['user']);
?>

when the page loads and only then.

To achieve what you want to do you need to make an ajax call.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.