Note that there are some explanatory texts on larger screens.

plurals
  1. POQuery Failed: Drop down menu in php web page
    primarykey
    data
    text
    <p>Below is a form for adding a Book to a table called books. </p> <p>I currently have a drop down menu that allows a user to pick an existing module however when I submit the form the below error appears...</p> <p>"Query failed: SQLSTATE[21S01]: Insert value list does not match column list: 1136 Column count doesn't match value count at row 1 "</p> <pre><code> &lt;form action="&lt;?php echo $_SERVER['PHP_SELF'] ?&gt;" method="post"&gt; Book Code: &lt;input type="text" name="fieldOne" /&gt; Book Title: &lt;input type="text" name="fieldTwo" /&gt; Author: &lt;input type="text" name="fieldThree" /&gt; Second Author: &lt;input type="text" name="fieldFour" /&gt; Publisher: &lt;input type="text" name="fieldFive" /&gt; Publish Year: &lt;input type="year" name="fieldSix" /&gt; &lt;!-- Module Code: &lt;input type="text" name="fieldEight" /&gt;--&gt; &lt;?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."]"); ?&gt; &lt;select name="fieldEight"&gt; &lt;?php while ($row = mysql_fetch_array($result)) { echo "&lt;option value=".$row['mod_id']."&gt;".$row['mod_id']."&lt;/option&gt;"; } ?&gt; &lt;input type="submit" name="submit" value="Add/Edit Book" /&gt; </code></pre> <p>Below is the code for querying the database/inserting user inputs into Books table setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } catch (PDOException $e) { echo "Connection Failed: " . $e->getMessage(); }</p> <pre><code> try { //if the fields are set, get data from them if ((isset($_POST["fieldOne"])) &amp;&amp; (isset($_POST["fieldTwo"]))&amp;&amp; (isset($_POST["fieldThree"])) &amp;&amp; (isset($_POST["fieldFour"])) &amp;&amp; (isset($_POST["fieldFive"])) &amp;&amp; (isset($_POST["fieldSix"])) &amp;&amp; (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 "&lt;br&gt;&lt;br&gt;Please complete all the specified fields to log a new Book or to modify an existing Book's details.&lt;/br&gt;&lt;/br&gt;"; } 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-&gt;query($sql); //if a book with the associated code already exists, overwite with new data if ($check-&gt;rowCount() &gt; 0) { $overwrite = true; } //check length of module title if (strlen($two) &gt; 50) { $proceed = false; echo "&lt;br&gt;&lt;p style = \"color:blue;\"&gt; Book Title is limited to 50 characters. &lt;/p&gt;&lt;/br&gt;"; } // 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 &amp;&amp; $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-&gt;query($sql); echo "&lt;br&gt;&lt;p style = \"color:green;\"&gt;Book with Book Code $one has been editied. Please refer to the table to confirm the changes.&lt;/p&gt; &lt;/br&gt;&lt;/br&gt;"; } else if ($overwrite == false &amp;&amp; $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-&gt;query($sql); echo "&lt;br&gt;&lt;p style = \"color:green;\"&gt;Book with Book Code $one has been added.&lt;/p&gt;&lt;/br&gt;&lt;/br&gt;"; //clear the variables $one = ""; $two = ""; $three = ""; $four = ""; $five = ""; $six = ""; $eight = ""; } } //end try } catch (PDOException $e) { echo "Query failed: " . $e-&gt;getMessage(); } //close the connection $conn = null; ?&gt; </code></pre> <p>Any idea why I am getting the error for query?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload