Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/217022-troubles-with-insert-into-query/
Share on other sites

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

+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)";

 

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.

YES!!!!

 

That did it!

 

Thank you very much!

 

:D

 

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.