【问题标题】:Fail to pass a boolean value through a SQL request未能通过 SQL 请求传递布尔值
【发布时间】:2019-08-07 20:31:27
【问题描述】:

我想在使用 phpMyAdmin 设置的数据库中传递以下表单的值:

<form action="recordBook.php" method="post">
    <input type="text" name="title" placeholder="Enter the book title">
    <input type="text" name="author" placeholder="Enter the author's name">
    <input type="checkbox" name="read" value="1">
    <br><input type="submit">
</form>

当我没有传递最后一个变量 $read 时,下面的 php 可以正常工作。每当我通过 $read 时,都会引发 SQL 语法错误。我尝试了多个变体来替换 1 和 0:true、TRUE 等,但没有得到肯定的结果。在我的数据库中,“读取”列设置为 tinyint

<?php

include "db_connect.php";

$title = $_POST['title'];
$author = $_POST['author'];
$read = isset($_POST['read']) ? 1 : 0;

echo "<script>console.log('".$title.$author.$read."');</script>";
$sql=("INSERT INTO books (title,author_name, read) VALUES ('$title','$author', '$read')");

if ($mysqli->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $mysqli->error;
}
?>

有人知道可能导致此错误的原因吗?

【问题讨论】:

    标签: php sql forms phpmyadmin boolean


    【解决方案1】:

    请试试这个:

    $read = $letter = isset($_POST['read']) ? 1 : 0;
    

    【讨论】:

    • 谢谢小学生,我试过了,但我仍然遇到同样的错误,现在我确定我在查询中传递了一个数值。会不会是数据库方面出了问题?
    【解决方案2】:

    发现问题:read 是 phpMyAdmin 中的保留字,所以一直传值失败。

    在数据库中,我将“read”列替换为“already_read”,它可以正常工作。

    【讨论】:

    • 很高兴知道您自己解决了这个问题。
    猜你喜欢
    • 2014-05-28
    • 2017-08-19
    • 2014-07-26
    • 2016-08-06
    • 1970-01-01
    • 2016-12-31
    • 2023-04-09
    • 1970-01-01
    相关资源
    最近更新 更多