在一个表格中我放入一个复选框(开关),我如何在提交检查MySQL更新的开关状态后发布?谢谢
因此MySQL表不会改变。
来自复选框并提交
<form method="POST" action="process.php">
<label class="mdl-switch mdl-js-switch mdl-js-ripple-effect" for="switch-1">
<input type="checkbox" id="switch-1" class="mdl-switch__input" checked>
<span class="mdl-switch__label">Autenticazione a due fattori</span>
</label>
....
<input onclick="conferma();" class="mdl-button mdl-js-button
mdl-button--raised mdl-js-ripple-effect mdl-button--accent" type="submit"
value="Salva" name="submitBtn">
</form>
切换控制和MySQL更新(process.php)
<?php
session_start();
mysql_connect(localhost) or die(mysql_error());
mysql_select_db("*******") or die(mysql_error());
$user = $_SESSION['users'];
if(isset($_POST['submitBtn'])) { //form submission occured
if(!isset($_POST['switch-1'])){
$sql = "UPDATE `*******`.`login_users` SET `auth` = \'checked\' WHERE username = '$user'";
header("location: https://*******.php");
} else {
$sql = "UPDATE `*******`.`login_users` SET `auth` = \'unchecked\' WHERE username = '$user'";
header("location: https://*******.php");
}
}
?>
答案 0 :(得分:0)
1)修改表格
<form method="POST" action="process.php">
<label class="mdl-switch mdl-js-switch mdl-js-ripple-effect" for="switch-1">
<input type="checkbox" name="switch-1" id="switch-1" class="mdl-switch__input" checked>
<span class="mdl-switch__label">Autenticazione a due fattori</span>
</label>
....
<input type="submit" value="Salva" name="submitBtn" class="mdl-button mdl-js-button
mdl-button--raised mdl-js-ripple-effect mdl-button--accent" >
</form>
2)在同一工作目录中创建一个新的process.php
页面并添加它们。
修改强>
<?php
session_start();
$user = $_SESSION['users'];
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "YourDBName";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if(isset($_POST['submitBtn'])) { //form submission occured
if(isset($_POST['switch-1'])){
$sql = "UPDATE login_users SET auth = 'checked' WHERE username = '$user'";
} else {
$sql = "UPDATE login_users SET auth = 'no' WHERE username = '$user'";
}
if ($conn->query($sql)) {
echo "Updated successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
} else {
echo "Form Submission Error";
}
$conn->close();
?>
希望它有所帮助。