Jump to content

passing $_POST data into INSERT INTO query


rghollenbeck

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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.