简体   繁体   English

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

[英]mysql_query executes automatically but not when onClick

I'm a newbie in php and I need help with this. 我是php的新手,对此我需要帮助。 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. 这让我很沮丧,因为我循环运行了这个按钮,但是每次按钮加载时,它都会自动在内部运行mysql_query,而不是单击它时。

SOLVED! 解决了! Check on AnthonyB's answer. 检查AnthonyB的答案。

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. 这是因为onclick事件通常是在浏览器中使用客户端脚本执行的,而脚本始终是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. 其中js_function是一个JavaScript,它将向页面发出ajax请求或仅导致表单提交。 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. 您正在做的是使onclick等于PHP代码片段的结果,这显然是在服务器端发生的,与用户单击按钮完全没有关系。

You should use an AJAX request, because there is no redirection nor reloading. 您应该使用AJAX请求,因为没有重定向也没有重新加载。 To use the exemple below, you must include jQuery 要使用以下示例,您必须包含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 在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. 注意,不推荐使用mysql_ *函数。 Instead, use mysqli_* functions or PDO . 相反,请使用mysqli_ *函数或PDO And you could prefere use prepared statement for security reason. 并且出于安全原因,您可能更喜欢使用准备好的语句

Please note that you're supposed to mention a Javascript function for onClick event. 请注意,您应该提到onClick事件的Javascript函数。 You can't write PHP script here. 您不能在此处编写PHP脚本。

One way to overcome this problem is create a separate PHP file which will contain the query. 解决此问题的一种方法是创建一个单独的PHP文件,其中将包含查询。

// 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");
?>" 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM