如何在MYSQL数据库中插入布尔值?

时间:2019-01-16 03:50:28

标签: php mysqli boolean

当我尝试将布尔值插入MySQL数据库的tasks表中时,它会产生致命错误。

严重错误:mysqli_sql_exception未捕获:不正确的整数值:表示“已完成”列的第1行中的

$servername = "localhost";
$username = "root";
$password = "new_password";
$dbname = "CONTACT____DB";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 


// creating table 

$sql = "CREATE TABLE tasks (
    id INT PRIMARY KEY AUTO_INCREMENT,
    title VARCHAR(255) NOT NULL,
    completed BOOLEAN
);";
if ($conn->query($sql) === TRUE) {
    echo "table tasks created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}


// inserting records

$title="some title" ;
$completed=false ;

$sql = "INSERT INTO tasks (title, completed)
VALUES ('$title', '$completed')";

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

$conn->close();
?>

注意:在以上代码中

  

$完成

是一个变量,因此我不能像这样将其直接放入MySQL查询

  

$ sql =“将任务插入(标题,已完成)           VALUES('$ title',FALSE)“;

0 个答案:

没有答案