2013-11-20 17 views
-1

下面是一個用於將書添加到名爲書籍的表格中的表單。查詢失敗:php網頁中的下拉菜單

我現在有一個下拉菜單,允許用戶選擇現有模塊但是當我提交會出現下面的錯誤形式...

「查詢失敗: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「連接失敗:」。 $ 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; 
     ?> 

任何想法爲什麼我得到的查詢錯誤?

回答

1

您在插入查詢中收到錯誤,因爲..所提供的列是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 
+0

對不起沒有一個$七 我從六個跳轉到八個,因爲曾經有需要其他領域,但我是個懶人,並沒有改變所有的八分到第七集 – user3010383

+0

我剛剛給了一個瘋狂的大聲笑:) –

+0

雖然因爲試圖插入8個字段而不是7而已解決了這個問題。即在Books表中不存在「關鍵字」字段。 現在有效:D – user3010383