我正在尝试使用调用 PHP 页面以将信息输入表中的 html 表单更改 MySQL 中的 bool 预设。
代码如下:
添加事件.php
$all_day = mysqli_real_escape_string($con, $_POST['all_day']);
if ($all_day != '1')
{
$all_day = '0';
}
$sql="INSERT INTO db . table (title, start, end, all_day)
VALUES ('$title', '$start', '$end', '$all_day')";
html 格式:
All Day?: <input type="checkbox" name="all_day" inputValue="1">
我遇到的问题是,无论我尝试做什么,“$all_day”的值都不会从 0 变为 1。如果有人能指出这个问题,我们将不胜感激。
最佳答案
您使用了错误的属性。使用 value=""
而不是 inputValue
。
$con = new mysqli('localhost', 'username', 'password', 'database');
if(isset($_POST['submit'])) {
$all_day = isset($_POST['all_day']) ? $_POST['all_day'] : 0;
// defaults to 0 if not checked
$prepare = 'INSERT INTO `table_name` (title, start, end, all_day) VALUES (?, ?, ?, ?)';
$stmt = $con->prepare($prepare);
// assuming $title, $start, $end is declared on your end
$stmt->bind_param('sssi', $title, $start, $end, $all_day);
$stmt->execute();
if($stmt->affected_rows > 0) {
// inserted
} else {
// it did not insert
}
}
?>
<form method="POST">
<!-- inputValue? no! the correct attribute is "value" -->
All Day?: <input type="checkbox" name="all_day" value="1">
<input type="submit" name="submit" />
</form>
只是为了清理东西:
- 如果未选中该复选框,则该复选框值不会更改为零,表单提交时会发生什么,如果未选中该复选框,它将在
$_POST
中未定义。 - 所以如果勾选了,就取值,如果不勾选,就默认为
0。
关于php - 使用 html 复选框和 PHP 更改 MySQL TINYINT(1),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25276440/