mysql_query会自动执行,但不会在onClick时执行

I'm a newbie in php and I need help with this. The query runs every time on page load and not when I click my button.

<input type="button" name="button" onclick="<?php 
                  $temp_id = $row_RecStudent['stu_id'];
                  mysql_query("UPDATE tbl_studentdetail SET stu_queue = 0 WHERE stu_id = $temp_id"); ?>" value="Unqueue" />

This is so frustrated because I run this button in a loop but every time the button loads it automatically run the mysql_query inside not when I click it.

SOLVED! Check on AnthonyB's answer.

You should use an AJAX request, because there is no redirection nor reloading. To use the exemple below, you must include jQuery

<!-- data-id is the id you got before -->    
<input type="button" data-id="<?php echo $row_RecStudent['stu_id']; ?>" name="button" class="button-on-click" value="Unqueue" />

<script type="text/javascript">
    $(function() {
        //On click on this kind of button
        $(".button-on-click").on('click', function() {
            var id = $(this).attr('data-id');
            //Send AJAX request on page youraction.php?id=X where X is data-id attribute's value
            //the ID used in mysql query
            $.ajax({
                url: "youraction.php",
                method: "GET",
                data: {
                    id: id
                }
            }).done(function() {
                //Once it is done
                console.log("OK!");
            });
        });
    });
</script>

On youraction.php

<?php
//Cast $_GET['id'] to integer to prevent SQL injection.
//If there is no id given, stop
if (empty($_GET['id'])) {
    exit();
}
$id = (int)$_GET['id'];
mysql_query("UPDATE tbl_studentdetail SET stu_queue = 0 WHERE stu_id = $id");

Be careful, mysql_* functions are deprecated. Instead, use mysqli_* functions or PDO. And you could prefere use prepared statement for security reason.

The query runs every time on page load and not when I click my button.

Which is because onclick events are usually executed in the browser with client side scripting, which invariably means javascript. What you really need to have is

 <input type="button" name="button" onclick="js_function()" value="Unqueue" />

Where js_function is a javascript that will make an ajax request to the page or simply cause the form to be submitted. What you are doing is making onclick equal to the result of the PHP code fragment, which obviously happens at the server side and has absolutely nothing at all to do with the user clicking on the button.

Please note that you're supposed to mention a Javascript function for onClick event. You can't write PHP script here.

One way to overcome this problem is create a separate PHP file which will contain the query.

// page1.php

<a href="action.php?stu_id=<?php echo $row_RecStudent['stu_id'] ?>">Unqueue</a>

// action.php

<?php 
    $temp_id = $_GET['stu_id'];
    mysql_query("UPDATE tbl_studentdetail SET stu_queue = 0 WHERE stu_id = $temp_id"); 
    header("Location:page1.php");
?>"