1

HTML フォームを MySQL データベースに接続するための PHP スクリプトを作成しようとしていますが、null にできない入力が 1 つあることを除いてすべてが機能しています。フィールドにテキストを入力すると、何が間違っているのかわかりません。

これが私の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 に答える