Jump to content

Answer to Discussion Doesn't Work.


justlukeyou

Recommended Posts

Hi,

 

I have written code to start a discussion in which someone can ask a question and add an optional comment to explain the question.  The question and comment then appears on the page.  Someone else can then click to view the complete question and comment based on the ID and then submit an answer.

 

However I cant get the answer part to work yet I am using the same process as the submission of the original question.  If I have a page which is based on one singe ID number should I be using code to submit information based on that single ID.

 

 

<?php

if(isset($_POST['form_id'])){
    $answer = mysql_real_escape_string(trim($_POST['answer']));
    $error = false;
    
    if(preg_match("/[a-zA-Z0-9]{1,}$/", $answer) == 0 && !$error) {
        $error = "The question you entered must contain only letters or numbers.";
    }

if(!$error) {
        $query = mysql_query("INSERT INTO discussion `answer` VALUES ('".$answer."')");
        if($query) {
        } else {
            $error = "There was a problem with submitting your answer. Please try again.";
        }
    }
}

?>

Link to comment
Share on other sites

$query = mysql_query("INSERT INTO discussion `answer` VALUES ('".$answer."')");

 

Your only inserting the answer and nothing more.. what is your table layout for the question/answer setup. What keys are you using to tie them together?

 

Example:

 

QuestionTable                                                |                AnswerTable

id - auto inc - primary index                                                      -- same

question                                                                                      answer

datesubmit                                                                              datesubmit

submit_by                                                                                submit_by

question_id_key                                                                  answer_id_key

 

Then question_id_key and answer_id_key could pair the tables together in a join to retrieve information.

Link to comment
Share on other sites

I see,

 

What I have done now is to add an answer_process page which submits an answer ID along with the answer.

 

However, what I have done stupidly is remove the input form for the answer and now I cant get it back together.

 

I think it should be a mixture of the two.

 

<form action="answer_process.php?ID=<?php echo $ID; ?>" method="post">


<textarea id="element_1" name="answer" class="element textarea medium" value="<?php if($_POST['answer']) echo $_POST['answer']; ?>"></textarea> 

Link to comment
Share on other sites

Well Ive got that working now but the answer_process page doesn't do anything.

 

<?php

if(isset($_GET['ID'])) {
    $qid = (int)$_GET['ID'];
    if(isset($_POST['answer']) && !empty($_POST['answer'])) {
        $answer = mysql_real_escape_string(trim($_POST['answer']));
        $query = mysql_query("INSERT INTO discussion (ansid, answer) VALUES ('".$ansid."', '".$answer."')");
        if($query) {
            $message = "Your answer has been posted successfully!";
        } else {
            $message = "Your answer could not be posted. Please try again!";
        }
    } else {
        $message = "You didn't enter an answer!";
    }
} else {
    exit;
}

?>

Link to comment
Share on other sites

Awesome work!

 

It's working as intended a new line for each answer is inserted. You can then filter answers in a query to display the output...

 

$query = "SELECT answer FROM answertable WHERE answer_id =....? ORDER BY answerdate ASC LIMIT 20"; 

 

Just use your table structures in the query, and you'd have to perform a join. Double check the ASC not sure if that's the ascending one lol ;)

Link to comment
Share on other sites

Thanks,

 

Bit confused what this does.  Im terrible at joins, I have tried them a number of times before.

 

$query = "SELECT answer FROM discussionanswers WHERE aid =....? ORDER BY aid ASC LIMIT 20";

 

Can I say the question ID is 32 and display all the aid (AnswerID) which relate to ID.

 

Is what this does but without the join?

Link to comment
Share on other sites

If you want to you can do it that way, have the question in a link format that submits to an answer script that receives the information and then retrieves the answers matching the ID from the database.. that's if I understand you?

 

I was thinking you had questions with answers listed under them, it would be better to use a join rather then having 2 full query structures.

 

A join:

 

$query = "SELECT question, answer FROM questions JOIN answers ON answers.answer_ID AND questions.question_ID"

 

if the tables had the title questions and answers and both contained a field question and answer, with answer_ID and question_ID being unique and the same in both tables you can join them together and output questions with answers that match...

 

I haven't done this in a while but it's what I recall doing... hope it helps clarify and not confuse you!

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.