查询失败:在php网页下拉菜单

下面是一个表格,用于将Book添加到名为books的表格中。

我目前有一个下拉菜单,允许用户选择一个现有的模块,但是当我提交表单时出现以下错误...

“查询失败:SQLSTATE [21S01]:插入值列表与列表列表不匹配:1136列计数与第1行的值计数不匹配”

   <form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
        Book Code: <input type="text" name="fieldOne" /> 
        Book Title: <input type="text" name="fieldTwo" /> 
        Author: <input type="text" name="fieldThree" /> 
        Second Author: <input type="text" name="fieldFour" /> 
        Publisher: <input type="text" name="fieldFive" /> 
        Publish Year: <input type="year" name="fieldSix" /> 

<!--            Module Code: <input type="text" name="fieldEight" />-->
        <?php
      include_once "includes/database.inc.php";
mysql_connect($host, $mysqlusername, $mysqlpassword, $mysqldatabase) or die(mysql_error());
mysql_select_db($mysqldatabase) or die(mysql_error());

$query = "SELECT mod_id FROM modules ORDER BY mod_id DESC";
$result = mysql_query($query) or die(mysql_error()."[".$query."]");
?>

<select name="fieldEight">
<?php 
while ($row = mysql_fetch_array($result))
{
echo "<option value=".$row['mod_id'].">".$row['mod_id']."</option>";
}
?>  


        <input type="submit" name="submit" value="Add/Edit Book" />

下面是查询数据库/将用户输入插入到书籍表中的代码setAttribute(PDO :: ATTR_ERRMODE,PDO :: ERRMODE_EXCEPTION); } catch(PDOException $ e){echo“Connection Failed:”。 $ E->的getMessage(); }

        try {
            //if the fields are set, get data from them
        if  ((isset($_POST["fieldOne"])) && 
            (isset($_POST["fieldTwo"]))&& 
            (isset($_POST["fieldThree"])) && 
            (isset($_POST["fieldFour"])) && 
            (isset($_POST["fieldFive"])) && 
            (isset($_POST["fieldSix"])) && 
            (isset($_POST["fieldEight"]))                     

            ) 
       {
            $plainTextOne = strtoupper(trim($_POST["fieldOne"]));
            $plainTextTwo = ucfirst(strtolower(trim($_POST["fieldTwo"])));
            $plainTextThree = ucfirst(strtolower(trim($_POST["fieldThree"])));
            $plainTextFour = ucfirst(strtolower(trim($_POST["fieldFour"])));
            $plainTextFive = ucfirst(strtolower(trim($_POST["fieldFive"])));
            $plainTextSix = ucfirst(strtolower(trim($_POST["fieldSix"])));
            $plainTextEight = ucfirst(strtolower(trim($_POST["fieldEight"])));
                $disallow = array('"', '-', "'");
                $one = str_replace($disallow, '', $plainTextOne);
                $two = str_replace($disallow, '', $plainTextTwo);
                $three = str_replace($disallow, '', $plainTextThree);                    
                $four = str_replace($disallow, '', $plainTextFour);                    
                $five = str_replace($disallow, '', $plainTextFive);
                $six = str_replace($disallow, '', $plainTextSix);
                $eight = str_replace($disallow, '', $plainTextEight);
        } else {
                $one = "";
                $two = "";
                $three = "";
                $four = "";
                $five = "";
                $six = "";
                $eight = "";
               }
            //if the fields are not all complete, ask user to complete them
               //NOTE that user is not required to enter a second Author (Author2 can be NULL)
            if ($one === "" || $two === "" || $three === "" 
                    || $five === "" || $six === "") {
                echo "<br><br>Please complete all the specified fields to log a new Book or to   modify an existing Book's details.</br></br>";
            } else {
                //begin checks on user input
                $proceed = true;
                $overwrite = false;
                //check to see if there already is a book using this book code
                $sql = "SELECT * FROM books
                        WHERE book_id = "$one"";
                $check = $conn->query($sql);
                //if a book with the associated code already exists, overwite with new data
                if ($check->rowCount() > 0) {
                    $overwrite = true;
                }
                //check length of module title
                if (strlen($two) > 50) {
                    $proceed = false;
                    echo "<br><p style = "color:blue;"> Book Title is limited to 50 characters. </p></br>";
                }



                // if the data is valid and a book is using the given code, update the row in the book table using the user input data.     
                if ($overwrite == true && $proceed == true) {
                    $sql =
                            "UPDATE books
                     SET book_title = "$two",  author1 = "$three", author2 = "$four", publisher = "$five", pub_year = "$six", mod_id = "$eight" 
                     WHERE book_id = "$one" ";
                    $conn->query($sql);
                    echo "<br><p style = "color:green;">Book with Book Code $one has been editied. Please refer to the table to confirm the changes.</p> </br></br>";
                } else if ($overwrite == false && $proceed == true) {
                    // if the data is valid, generate the row in the module table using the user input data.                         
                    $sql =
                            "INSERT INTO books (`book_id`, `book_title`, 
                                `author1`, `author2`, `publisher`, 
                                `pub_year`, `keywords`, `mod_id`)
                    VALUES("$one", "$two", "$three", "$four", "$five", "$six" , "$eight")";
                    $conn->query($sql);
                    echo "<br><p style = "color:green;">Book with Book Code $one has been added.</p></br></br>";
                    //clear the variables
                        $one = "";
                        $two = "";
                        $three = "";
                        $four = "";
                        $five = "";
                        $six = "";
                        $eight = "";
                }
            }
            //end try
        } catch (PDOException $e) {
            echo "Query failed: " . $e->getMessage();
        }
        //close the connection
        $conn = null;
        ?>  

任何想法,为什么我得到错误的查询?


您在插入查询中遇到错误,因为..所提供的列是8,但您只为7列插入数据。

看这里

$sql = "INSERT INTO books (`book_id`, `book_title`, 
                                `author1`, `author2`, `publisher`, 
                                `pub_year`, `keywords`, `mod_id`)
                    VALUES("$one", "$two", "$three", "$four", "$five", "$six" , "$eight")";

//.....$seven is missing
链接地址: http://www.djcxy.com/p/69567.html

上一篇: Query Failed: Drop down menu in php web page

下一篇: Syntax error or access violation using PDO