1

我正在尝试创建一个 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>

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

“系统错误。查询: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') - 你忘记输入书名了。”

即使我肯定在书名字段中输入了一些内容。

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

4

1 回答 1

0

首先检查mysql的语句并直接放入phpmyadmin并查看错误。第二个我可以看到很可能 var book_name 是空的

于 2013-04-27T19:51:16.907 回答