php - Fail to pass a boolean value through a SQL request
问题描述
I want to pass the values of the below form in a database set up with 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>
The following piece of php works fine when I don't pass the last variable $read. Whenever I pass $read, an error of SQL syntax is thrown. I tried multiple variant to replace 1 and 0: true, TRUE, etc with no positive result. In my database, the column "read" is set as 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;
}
?>
Would someone know what could cause this error ?
解决方案
Please try this:
$read = $letter = isset($_POST['read']) ? 1 : 0;
推荐阅读
- sql-server - SQL Server - 在特定字符之前删除字符串的左侧部分
- python - ImportError:没有名为 Levenshtein 的模块
- linux - NaCl 帮助程序在 Linux 中通过 Selenium 使用 Chrome 运行而没有沙盒错误
- git - `git pull --rebase` 会影响工作目录吗?
- javascript - 是否有一种 HTML5 方法可以使用先前在另一个电子邮件表单字段中输入的值预先填写电子邮件表单字段?
- excel - 为特定单元格插入公式
- android - Android 应用在 Google Play 上发布后,我们可以更改其使用的 Firebase 帐户吗?
- spring - 从容器部署时在打印的类路径上未检测到 Spring WebApplicationInitializer 类型
- javascript - 根据路由 Vue.JS 动态显示组件(v-show 有效但 v-if 无效)
- java - Android 数据绑定生成非 UTF-8 编码的文件