rghollenbeck Posted October 27, 2010 Share Posted October 27, 2010 I have this PHP file that I intend to provide the answers to only four questions in a little quiz in a "Questions" table: QuestionID, QuestionText Questions: 1 Which is NOT part of the FAT TOM acronym? 2 Of the following choices, which has a better chance of creating a foodborne illness? 3 How should food NEVER be thawed? 4 Where should pesticides be stored? The answers should go in the Answers table: "QuestionID, OptionText, CorrectAnswer" I have hundreds of questions and answers, but I need to get this small sample working first. The following code runs without triggering an error but it doesn't insert any data. <?php $link = mysql_connect('PathToMyData', 'myUsername', 'MyPassword'); // specifics removed for security if (!$link) { die('Could not connect: ' . mysql_error()); } mysql_select_db('quiz'); $sql = "INSERT INTO ANSWERS (QuestionID,OptionText,CorrectAnswer) // CorrectAnswer is type BOOL (Correct=1 Wrong Answer=0) (VALUES (1,'food',0), (1,'alkalinity',1), (1,'time',0), (1,'temperature',0), (1,'oxygen',0), (1,'moisture',0), (2,'celery sticks',0), (2,'beef jerky',0), (2,'cranberry juice',0), (2,'baked potato',1), (2,'saltine cracker',0), (3,'in the refrigerator',0), (3,'in a pot in the kitchen at room temperator',1), (3,'as a part of the cooking process',0), (3,'under cool running water',0), (4,'close to the food preparation area for easy access',0), (4,'in a locked storage area away from food',1), (4,'in the dry storage area',0), (4,'in a bin or box under the sink',0)"; $result = mysql_query($sql); // executes the query // close database mysql_close($link); ?> Then I go back to the MySQL admin area at my host's site and query up "SELECT * FROM Answers;" and nothing! It's still empty! As I said, no error was triggered, but nothing was inserted. This is discouraging. Quote Link to comment https://forums.phpfreaks.com/topic/217022-troubles-with-insert-into-query/ Share on other sites More sharing options...
kenrbnsn Posted October 27, 2010 Share Posted October 27, 2010 How do you know it's not generating an error, since you're not checking. Change <?php $result = mysql_query($sql); ?> to <?php $result = mysql_query($sql) or die("Problem with the query: $sql<br>" . mysql_error()); ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/217022-troubles-with-insert-into-query/#findComment-1127172 Share on other sites More sharing options...
rghollenbeck Posted October 27, 2010 Author Share Posted October 27, 2010 Thank you for your reply. I actually did have that in there but an error never triggered. I should have kept it in. Thanks for reminding me. I'll put that back in and run the file again. Quote Link to comment https://forums.phpfreaks.com/topic/217022-troubles-with-insert-into-query/#findComment-1127181 Share on other sites More sharing options...
mikosiko Posted October 27, 2010 Share Posted October 27, 2010 +1 to Ken's advice... you always should check for errors.... you will see that you have an error in your query syntax... remove the first "(" from this line: (VALUES (1,'food',0), and remove "; )" from the end of this line (leave the rest) (4,'in a bin or box under the sink',0)"; Quote Link to comment https://forums.phpfreaks.com/topic/217022-troubles-with-insert-into-query/#findComment-1127184 Share on other sites More sharing options...
rghollenbeck Posted October 27, 2010 Author Share Posted October 27, 2010 Thanks! A trained eye can see obvious errors. I should have seen that! I'm such a novice at MySQL! I have some experience in MS-Access SQL, and that isn't very different, but not enough to spot that simple thing that I should have seen. I'll try this now. Quote Link to comment https://forums.phpfreaks.com/topic/217022-troubles-with-insert-into-query/#findComment-1127197 Share on other sites More sharing options...
rghollenbeck Posted October 27, 2010 Author Share Posted October 27, 2010 YES!!!! That did it! Thank you very much! As I said to another helpful soul yesterday, I promise to study this solution so I can become helpful to somebody else someday instead of constantly relying on this help forum. Rich Quote Link to comment https://forums.phpfreaks.com/topic/217022-troubles-with-insert-into-query/#findComment-1127210 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.