0

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.

Kenny
  • 3
  • 5
  • you can't attach a PHP code to a javascript onclick event. instead the onclick can trigger an ajax call to run the update – Mohamed Yasin Nov 29 '16 at 07:06
  • you can follow : http://stackoverflow.com/a/37270558/7063928 – Subhra Jyoti Lahiri Nov 29 '16 at 07:09
  • that is not how this works: **all** the PHP is executed **before** anything is sent to the browser, and **all** the javascript is executed **after** everything is sent to the browser. you should read some tutorials about how webservers work. and you shouldn't keep on using `mysql`-functions, they are deprecated and in PHP7 removed. use `mysqli` or `PDO` – Franz Gleichmann Nov 29 '16 at 07:13
  • Thanks for the replies. Indeed I knew nothing about this previously and now I've learnt a lot more in these hours compared to last few days. – Kenny Nov 29 '16 at 09:14

3 Answers3

3

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.

e4c5
  • 52,766
  • 11
  • 101
  • 134
  • PHP is a server side language. That means it is executed once, just before return HTML to client. JavaScript is here a client side language. That means it is executed by the browser (not by the server). In your case, PHP execute the query "UPDATE ...", and then return html. What you want, as said by @e4c5, is make an Ajax request to do this. An AJAX request will send an HTTP request to your server, and your server will execute update query. – Anthony Nov 29 '16 at 07:11
  • thank you @AnthonyB for the comment. e4c6 is the Caro-Kann while my preferencce is for the Sicilian – e4c5 Nov 29 '16 at 07:13
  • I did'nt understand chess reference, but I fixed my comment. You could add example of AJAX request (perhaps using jQuery) with the server side code, in order to your answer be more useful. – Anthony Nov 29 '16 at 07:18
1

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.

Anthony
  • 2,014
  • 2
  • 19
  • 29
  • I appreciate this so much. But there an issue at line data: { – Kenny Nov 29 '16 at 07:47
  • Indeed I fixed it. There were errors because of parenthesis and a coma in data. Now that should work. – Anthony Nov 29 '16 at 07:50
  • good answer +1 from me for including an ajax sample and for the warning about mysql_* – e4c5 Nov 29 '16 at 07:55
  • Thanks @e4c5! I was inspired by your answer. – Anthony Nov 29 '16 at 07:56
  • Your code is quite clear but I still don't manage to get it to work. I've been rechecking but when I click on button just nothing happen. – Kenny Nov 29 '16 at 08:05
  • This code attaches some JavaScript to all elements that have HTML class "button-to-click". These buttons have an attribute data-id that you filled with you database id. Once you click on the button, an AJAX request is done (check in Network area of you browser, press f12 for that). The ajax request should call youraction.php with the given id. I'll improve the code a little bit. – Anthony Nov 29 '16 at 08:09
  • Millions thumb up for AnthonyB. Good explaination between codes as well. I've read up the links you given and now I'm working on the 'new' PDO and mysqli. Thanks for the resources! – Kenny Nov 29 '16 at 08:48
  • Thank @Kenny! I usually use PDO because it's object oriented, but mysqli_* functions work well to! – Anthony Nov 29 '16 at 08:50
0

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");
?>" 
Indrasis Datta
  • 8,692
  • 2
  • 14
  • 32
  • Using a link is not a bad idea, instead of using an AJAX request. But it cause a reloading of the page. If this is not a problem, you can use it. But some times, a reloading is a problem. – Anthony Nov 29 '16 at 07:20