Jump to content

Troubles with INSERT INTO query


rghollenbeck

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.