php - 尝试使用 PHP 将信息输入 MySQL 数据库时出现不正确的错误?

标签 php html mysql forms input

我正在尝试创建一个 PHP 脚本来将 HTML 表单连接到 MySQL 数据库,并且一切正常,除了有一个输入不能为空,而且无论我做什么,系统似乎都没有识别我何时在字段中输入文本,但我不知道自己做错了什么。

这是我的 PHP 代码:

if (empty($_POST['book_name']))
{$errors[ ] = 'You forgot to enter the book name.';
}
else    {
    $booktitle = trim($_POST['book_name']);
}

if (empty($_POST['author']))
{$errors[ ] = 'You forgot to enter the author.';
}
else    {
    $author = trim($_POST['author']);
}

if (empty($_POST['cover']))
{$errors[ ] = 'You forgot to enter the book cover image.';
 }
else    {
    $cover = trim($_POST['cover']);
}

if (empty($_POST['publisher']))
{$errors[ ] = 'You forgot to enter the publisher.';
 }
else    {
    $publisher = trim($_POST['publisher']);
}

if (empty($_POST['language_id']))
{$errors[ ] = 'You forgot to enter the book language.';
}
else    {
    $languageid = trim($_POST['language_id']);
}

if (empty($_POST['length_pages']))
{$errors[ ] = 'You forgot to enter the book length in pages.';
}
else    {
    $lengthpages = trim($_POST['length_pages']);
}

if (empty($_POST['fiction']))

{$errors[ ] = 'You forgot to enter if the book is fiction or not.';
}
else    {
    $fiction = trim($_POST['fiction']);
}

if (empty($_POST['pub_year']))
{$errors[ ] = 'You forgot to enter the year the book was published.';}

else    {
    $pubyear = trim($_POST['pub_year']);

}

    if (empty($errors)) {
require ('mysqli_connect.php');


}

$q = "INSERT INTO books(book_name, author, publisher, language_id, length_pages, cover, fiction, pub_year) VALUES
('$booktitle', '$author', '$publisher', '$languageid', '$lengthpages', '$cover', '$fiction', '$pubyear')";
$r = @mysqli_query($dbc, $q);



 if ($r) {
    echo 'Thank you! This book information has been entered into the database.';
}

 else {
    echo 'System error.';
    echo mysqli_error($dbc) . ' Query: ' . $q;
    foreach ($errors as $msg) {
        echo " - $msg<br>\n";
    }
}

?>

这是我的 HTML 代码:

<form action="register.php" method="post">
<p>Book name: <input type="text" name="book_name" size="20" maxlength="100" value="<?php if (isset($_POST['book_name'])) echo $_POST['book_name']; ?>" /></p>
<p>Author: <input type="text" name="author" size="20" maxlength="100" value="<?php if (isset($_POST['author'])) echo $_POST['author']; ?>" /></p>
<p>Publisher: <input type="text" name="publisher" size="20" maxlength="100" value="<?php if (isset($_POST['publisher'])) echo $_POST['publisher']; ?>" /></p>
<p>Language:</p>
<p>English <input type="radio" name="language_id" value="1" /></p>
<p>Spanish <input type="radio" name="language_id" value="2" /></p>
<p>French <input type="radio" name="language_id" value="3" /></p>
<p>Italian <input type="radio" name="language_id" value="4" /></p>
<p>Mandarin <input type="radio" name="language_id" value="5" /></p>
<p>Number of pages: <input type="text" name="length_pages" size="20" maxlength="100" value="<?php if (isset($_POST['length_pages'])) echo $_POST['length_pages']; ?>" /></p>
<p>Cover image file name: <input type="text" name="cover" size="20" maxlength="100" value="<?php if (isset($_POST['cover'])) echo $_POST['cover']; ?>" /></p>
<p>Is this book fiction?:</p>
<p>Yes <input type="radio" name="fiction" value="yes" /></p>
<p>No <input type="radio" name="fiction" value="no" /></p>
<p>Year Published: <input type="text" name="pub_year" size="20" maxlength="100" value="<?php if (isset($_POST['pub_year'])) echo $_POST['pub_year']; ?>" /></p>
<input type="submit" name="submit" value="submit" /></form>

无论出于何种原因,每次我尝试对其进行测试时,都会收到此错误消息:

"System error. Query: INSERT INTO books(book_name, author, publisher, language_id, length_pages, cover, fiction, pub_year) VALUES ('', 'Not Hayley Munguia', 'Random House', '2', '134', 'howtobenormal.jpg', 'no', '1938') - You forgot to enter the book name."

尽管我确实在书名字段中输入了一些内容。

我真的不知道我做错了什么,感谢所有帮助!谢谢!

最佳答案

首先检查取mysql中的语句,直接放入phpmyadmin中查看错误。其次我可以看到 var book_name 很可能是空的

关于php - 尝试使用 PHP 将信息输入 MySQL 数据库时出现不正确的错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16256185/

相关文章:

mysql - 关于索引从oracle到mysql的语句转换

php - MSExchange URL 编码

php - 尝试跨 View 共享数据 Laravel

php - RegExr 至少有 2 个字母和可选字母,但没有其他字母?

javascript - 如何在纯javascript中等到动画完成

javascript - 防止 Firefox 自动完成弹出

javascript - 刷新后从下拉菜单中保留 iframe src

mysql - 动态地将数据从.mdb导出到.mysql

php - 仅选择字段中包含 future 日期的记录

php - 每个 CakePHP 1.3 页面请求的 httpd.exe 使用率很高