justlukeyou Posted June 4, 2012 Share Posted June 4, 2012 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."; } } } ?> Quote Link to comment Share on other sites More sharing options...
Skewled Posted June 4, 2012 Share Posted June 4, 2012 $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. Quote Link to comment Share on other sites More sharing options...
justlukeyou Posted June 4, 2012 Author Share Posted June 4, 2012 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> Quote Link to comment Share on other sites More sharing options...
justlukeyou Posted June 4, 2012 Author Share Posted June 4, 2012 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; } ?> Quote Link to comment Share on other sites More sharing options...
justlukeyou Posted June 4, 2012 Author Share Posted June 4, 2012 Bloody Hell!!!!! I got it to work. The answer now submits into discussionsanswers table. The problem is now for every answer I submit it creates a new line. How do make it fill in answer, answer1, answer2 etc? Quote Link to comment Share on other sites More sharing options...
Skewled Posted June 4, 2012 Share Posted June 4, 2012 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 Quote Link to comment Share on other sites More sharing options...
justlukeyou Posted June 4, 2012 Author Share Posted June 4, 2012 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? Quote Link to comment Share on other sites More sharing options...
Skewled Posted June 4, 2012 Share Posted June 4, 2012 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! Quote Link to comment 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.