rghollenbeck Posted October 26, 2010 Share Posted October 26, 2010 I've been poking around the Internet for the answer to this without any success. This query APPEARS to run, but NOTHING GETS INSERTED. The QuestionID in the Questions table is set to auto-increment, so I only need the QuestionText data for this table. the QuestionID in the Answers table is not because there might be four or five options per question. The snippets of code under consideration is pasted below snipped from index.php: <form action="./newquestion.php" method="post"> Question Text:<br><input type="text" size=100 name="QuestionText" /><br><br> <input type="submit" value="Enter next question"/> </form> Then snipped from newquestion.php mysql_select_db(quiz); $query = mysql_query("INSERT INTO Questions VALUES ('$_POST[QuestionText]'"); $result = mysql_query($query); Thank you all. I'm sure I'll learn this pretty fast, but I still need a little help. Link to comment https://forums.phpfreaks.com/topic/216835-passing-_post-data-into-insert-into-query/ Share on other sites More sharing options...
Pikachu2000 Posted October 26, 2010 Share Posted October 26, 2010 If you aren't specifying a value for all fields in the table, you need to explicitly list the fields in the query string. INSERT INTO `table` (`field1`, `field2`) VALUES (`value1`, `value2`) Link to comment https://forums.phpfreaks.com/topic/216835-passing-_post-data-into-insert-into-query/#findComment-1126444 Share on other sites More sharing options...
rghollenbeck Posted October 26, 2010 Author Share Posted October 26, 2010 I have three records in there already which I put in there with an INSERT INTO query with only one field, QuestionText, listed. There are only two fields, OptionText, and QuestionID. QuestionID is set to auto-increment. In fact, the QuestionID for those three records defaulted to 1, 2, and3. In those three queries I hard-coded the values into the query. This time I'm trying to get the values from a form. The only field that I have any discretion over is OptionText since QuestionID is auto-incremented. I will probably have to add a boolean field to the Answers table. Correct answer=1 (true) and incorrect answer=0 (False). Link to comment https://forums.phpfreaks.com/topic/216835-passing-_post-data-into-insert-into-query/#findComment-1126453 Share on other sites More sharing options...
rghollenbeck Posted October 26, 2010 Author Share Posted October 26, 2010 Oops! I forgot to mention that I am going to try what you said. Thank you for your idea. Link to comment https://forums.phpfreaks.com/topic/216835-passing-_post-data-into-insert-into-query/#findComment-1126455 Share on other sites More sharing options...
revraz Posted October 26, 2010 Share Posted October 26, 2010 Two things that will tell you your answer: 1. Use mysql_error after your query. 2. Echo your $query to see what is actually being sent. Link to comment https://forums.phpfreaks.com/topic/216835-passing-_post-data-into-insert-into-query/#findComment-1126461 Share on other sites More sharing options...
rghollenbeck Posted October 26, 2010 Author Share Posted October 26, 2010 I took the advice of adding an echo and the mysql_error(): mysql_select_db('quiz'); $query = mysql_query("INSERT INTO Questions ('QuestionText') VALUES ('$_POST[QuestionText]'"); echo $query . "This already echoed the query variable."; // I added a little text to prove // this ran. It did, but was blank. $result = mysql_query($query); mysql_error() . "just ran mysql_error()"; //added a little text to prove that the error ran. // It did not run echo $result[0]; Thank you. This did not work. I also had the wrong field before but fixed that. Still didn't work. Here's what the result was. . . "This already echoed the query variable. " without the query data inside it. No error was triggered. Link to comment https://forums.phpfreaks.com/topic/216835-passing-_post-data-into-insert-into-query/#findComment-1126477 Share on other sites More sharing options...
Pikachu2000 Posted October 26, 2010 Share Posted October 26, 2010 Replace the entire block you posted above with this and see what happens. mysql_select_db('quiz'); $query = "INSERT INTO Questions (`QuestionText`) VALUES ('{$_POST['QuestionText']}')"; If( $result = mysql_query($query) ) { echo "The query: $query affected " . mysql_affected_rows(); } else { echo "The query: $query caused an error: " . mysql_error(); } Link to comment https://forums.phpfreaks.com/topic/216835-passing-_post-data-into-insert-into-query/#findComment-1126481 Share on other sites More sharing options...
rghollenbeck Posted October 26, 2010 Author Share Posted October 26, 2010 You did it! Thanks a million! Link to comment https://forums.phpfreaks.com/topic/216835-passing-_post-data-into-insert-into-query/#findComment-1126484 Share on other sites More sharing options...
Pikachu2000 Posted October 26, 2010 Share Posted October 26, 2010 You're welcome; glad I could help. Link to comment https://forums.phpfreaks.com/topic/216835-passing-_post-data-into-insert-into-query/#findComment-1126487 Share on other sites More sharing options...
rghollenbeck Posted October 26, 2010 Author Share Posted October 26, 2010 Anytime I get solutions like this online, I promise to study the solution so I can learn to to PHP/MySQL better. Link to comment https://forums.phpfreaks.com/topic/216835-passing-_post-data-into-insert-into-query/#findComment-1126490 Share on other sites More sharing options...
Pikachu2000 Posted October 26, 2010 Share Posted October 26, 2010 Can't really ask for more than that. Don't be afraid to post questions, regardless of whether you think they may be silly. Link to comment https://forums.phpfreaks.com/topic/216835-passing-_post-data-into-insert-into-query/#findComment-1126492 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.