gpt4 book ai didi

javascript - mysqli_query 在函数内部自动激活?

转载 作者:行者123 更新时间:2023-12-03 00:32:25 24 4
gpt4 key购买 nike

无论出于何种原因,我的数据库都会更新,就像函数运行一样,但它从未回显任何内容。当我重新加载页面时,它会自动将名称设置为“John”,但我从未单击过该按钮。

    <?php
function a(){
$con = mysqli_connect("localhost", "example", "example", "example");
$sql = "UPDATE User SET name = 'John' WHERE name = '$username'";

mysqli_query($con, $sql);

//test to see if function fires:
echo "function executed";
}
?>

这是我的 html/javascript 代码:

    <script type="text/javascript">
function b(){
document.write("<?php a(); ?>");

//test if javascript function executes:
alert("function b() executed");
}
</script>

<button onclick="b()">Click me!</button>

我必须执行 javascript,因为我的整个页面是一个表单(为了单个保存按钮的目的),并且您不能直接让按钮执行 php 函数。

我真的很困惑为什么它没有回显,但当我重新加载页面时它确实更新了我的数据库,请帮忙。

最佳答案

当然,由于这一行,您每次加载页面时都会更新数据库

document.write("<?php a(); ?>");

每次加载页面时,您都会调用 a() 来更新数据库

为什么函数a没有回显“函数已执行”,是不是确实,但你在页面,因为它在其行中得到回响。我相信您会在页面源代码中看到它。

当 PHP 解析器解析您的脚本时,它将生成并响应此内容

document.write("function executed");

关于javascript - mysqli_query 在函数内部自动激活?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53801341/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com