php函数的onClick参数

时间:2016-02-01 09:23:49

标签: javascript php mysql

我编写了下面的代码,将mysql中的变量设置为1或0.但不管怎样,每当我click第一个按钮(1)时,它总是保存分配给的{0}值mysql表格中的第二个按钮。

<head>
<?php
function update_ziekenwagen($Status) {
    $servername = "localhost";
    $username = "webapp";
    $password = "password";
    $dbname = "spoed";
    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }

    $sql = "UPDATE AlgemeneVars SET value='".$Status."' WHERE id=4";    

    if ($conn->query($sql) === TRUE) {
        echo "Record updated successfully:" . $Status;
        //echo "UPDATE AlgemeneVars SET value=' " . $Status . " ' WHERE id=4";
    } else {
        echo "Error updating record: " . $conn->error;
    }

    $conn->close();}
?>
</head>
<body>
<input type="button" value="Vertr" id="Vertr" name="Vertr" onclick="document.write('<?php update_ziekenwagen(1); ?>');" />
<input type="button" value="Terug" id="Terug" name="Terug" onclick="document.write('<?php update_ziekenwagen(0); ?>');" />
</body>

1 个答案:

答案 0 :(得分:0)

<head>
<?php
function update_ziekenwagen() {
    $Status = $_POST['status'];
    $servername = "localhost";
    $username = "webapp";
    $password = "sW7HwM225PxrwbZC";
    $dbname = "spoed";
    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }

    $sql = "UPDATE AlgemeneVars SET value='".$Status."' WHERE id=4";    

    if ($conn->query($sql) === TRUE) {
        echo "Record updated successfully:" . $Status;
        //echo "UPDATE AlgemeneVars SET value=' " . $Status . " ' WHERE id=4";
    } else {
        echo "Error updating record: " . $conn->error;
    }

    $conn->close();
}
if (!empty($_POST)){
    update_ziekenwagen();
}
?>
</head>
<body>
<form>
    <input type="hidden" name="status" value="1" />
    <input type="submit" />
</form>
<form>
    <input type="hidden" name="status" value="0" />
    <input type="submit" />
</form>
</body>

尽管使用上面的代码,您仍然可以在不使用Ajax的情况下使其工作。但这完全是 NOT 安全或良好的做法。