php - 使用 html 复选框和 PHP 更改 MySQL TINYINT(1)

标签 php html mysql

我正在尝试使用调用 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/

相关文章:

php - javascript 函数调用不起作用

java - 从 PHP 到 Android 的 HTTP 响应字符串

javascript - 禁止在输入时删除链接或图像?

php - 如何使此表单的输出输入一个数组?

php - HTTP post 请求 objective-c

php - 访问私有(private) s3 存储桶文件

使用按钮 onclick 调用 JavaScript 函数,onload 失败

MySQL - 该做的和不该做的

MySQL - 即使连接为空也检索结果

php - 如何在 Laravel 4 中查询除一个以外的所有内容?