-1

i have a button, i want it every time i click the button it'll subtract from my credit on database (MySQL). i tried everything that already answered on another question but still unable to solved my problem.

here's my code

<input type="submit" name="submit" value="Check" class="button_x" style="width:100px" >
<?php
if($_SERVER['REQUEST_METHOD'] == "POST" and isset($_POST['submit']))
{
    func();
}
function func()
{
    $query = "UPDATE users SET user_credit = user_credit - 2";
    $result = mysqli_query($conn, $query);
}
?>

if im not using the if function, it work! but it would subtract the credit when i click the sub menu as well. or should i add 'where' at my query?

thank you.

  • Show your form code, in particular your opening form tag. – Jonnix Oct 23 '18 at 14:45
  • 1
    and is not a php operator. it is && – Ne Ma Oct 23 '18 at 14:46
  • 1
    @NeilMasters Yes it is :) – Jonnix Oct 23 '18 at 14:52
  • @jon stirling do i really have to provide an 3v4l... oh. oh dear. – Ne Ma Oct 23 '18 at 14:58
  • @NeilMasters Please, let me. https://3v4l.org/OZ8Sf :D – Jonnix Oct 23 '18 at 15:00
  • Thats just rubbing my nose it :D – Ne Ma Oct 23 '18 at 15:00
  • Note: The object-oriented interface to `mysqli` is significantly less verbose, making code easier to read and audit, and is not easily confused with the obsolete `mysql_query` interface. Before you get too invested in the procedural style it’s worth switching over. Example: `$db = new mysqli(…)` and `$db->prepare("…")` The procedural interface is an artifact from the PHP 4 era when `mysqli` API was introduced and should not be used in new code. – tadman Oct 23 '18 at 15:13

3 Answers3

1

I think your problem is you are not actually doing a POST, your form is probably using GET.

Another thing is you don't really need to check $_SERVER['REQUEST_METHOD'] at all. Try this code:

if(isset($_GET['submit']))
{
    func();
}

If the above works, then my assumption was correct, you'll either need to use $_GET or change your form to use $_POST.

You can change your form to use $_POST by adding method="POST" to your form element around your button.

<form method="POST">
    <input type="submit" name="submit" value="Check" class="button_x" style="width:100px">
</form>

If the above doesn't work, I suspect your $_SERVER['REQUEST_METHOD'] variable is empty somehow (Could be a something with your sever), in which case, the following should work:

if(isset($_POST['submit']))
{
    func();
}

Another way you could do this is to not care if it's POST or GET, by doing it this way:

if(isset($_REQUEST['submit']))
{
    func();
}
GrumpyCrouton
  • 8,486
  • 7
  • 32
  • 71
  • @JonStirling Because it's not PHP's syntax? – Sam Oct 23 '18 at 14:48
  • @Samuel Yes it is? – Jonnix Oct 23 '18 at 14:48
  • @JonStirling Is that valid syntax? I've never seen it in practice, but now looking at the manual it appears it is correct. Could possibly be `$_SERVER['REQUEST_METHOD']` being empty maybe? – GrumpyCrouton Oct 23 '18 at 14:48
  • http://php.net/manual/en/language.operators.logical.php same as `&&` but with different precedence I think? – Jonnix Oct 23 '18 at 14:48
  • @JonStirling yes you are right... It's valid logic... My bad – Sam Oct 23 '18 at 14:49
  • The OP didn't post `form` code, so the method could be `GET` – AnTrakS Oct 23 '18 at 14:49
  • 2
    @D.Dimitrov could be... but at this point it is all but speculations. – Sam Oct 23 '18 at 14:51
  • @Samuel I agree it's all speculation, but that is really the only reasoning I can think of this would happen, unless it's an issue with `$_SERVER['REQUEST_METHOD']` being empty, but my answer would solve that as well. – GrumpyCrouton Oct 23 '18 at 14:52
0

Have you put it inside a <form> tag? Like this:

<form action="" method="post">
    <input type="submit" name="submit" value="Check" class="button_x" style="width:100px">
</form>
<?php
if($_SERVER['REQUEST_METHOD'] == "POST" && isset($_POST['submit']))
{
    func();
}
function func()
{
    $query = "UPDATE users SET user_credit = user_credit - 2";    
    $result = mysqli_query($conn, $query);
}
rbaskam
  • 749
  • 7
  • 22
0

Have you checked to make sure that the $conn variable you are using is accessible inside the function? It's difficult to tell since I can't see the rest of your code, but if $conn is created outside the function it will not be reachable from inside. A solution to this would be to either create it again inside the function or to pass it in as a parameter like such:

func($conn);

function func($conn)
{
    $query = "UPDATE users SET user_credit = user_credit - 2";
    $result = mysqli_query($conn, $query);
}

Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?

Fabian
  • 691
  • 1
  • 6
  • 13
  • 1
    I missed that part. I guess the code we see in func() is incomplete since this apparently works despite the scope issues. – Fabian Oct 24 '18 at 06:28