我有一个小型 PHP 应用程序,它应该允许用户在 XHTML Web 表单中输入 4 位数的年份,并返回保存在 MySQL 数据库中的所有信息,其年份低于提供的年份。年份存储在 db 列下yearPublished
。我有查询工作,我没有收到任何错误,但是没有返回任何数据。
这是用于创建数据库的 SQL 文件:
CREATE TABLE IF NOT EXISTS `books` (
`ISBN` char(13) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`title` varchar(40) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
`author_firstName` varchar(40) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
`author_lastName` varchar(40) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
`genre` varchar(35) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
`publisher` varchar(40) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
`yearPublished` varchar(4) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
`price` float DEFAULT '0.0',
PRIMARY KEY (`ISBN`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
这是执行查询的 PHP 代码:
if(isset($_POST['submitForm'])) {
$strBookPubYear = $_POST['pubYear'];
if(empty($strBookPubYear)) {
print("<p class='error'>You must enter a 4 digit publication date!</p>\n");
} else {
if(!is_numeric($strBookPubYear)) {
print("<p class='error'>The publication date must be numeric.</p>\n");
} else {
if(!(strlen($strBookPubYear) === 4)) {
print("<p class='error'>The Publication date must be exactly 4 characters long.</p>\n ");
} else {
$sqlQuery = mysql_query("SELECT yearPublished FROM books WHERE yearPublished < '$strBookPubYear'");
if($sqlQuery === false) {
print("<p class='error'>Could not execute the query, please try again.</p>\n");
} else {
注意$sqlQuery
应该获取yearPublished
低于的所有数据的变量$strBookPubYear
。我已经阅读了多个关于此的教程,但它们都使用 MySQL 'Date' 数据类型。就我而言,我使用的是文本数据类型。请不要告诉我使用mysqli,这个具体项目不需要我使用mysqli。