This file contains the code for search form:
search.php
<?php
session_start();
?>
<html>
<head>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
</head>
<body>
<?php
echo"WELCOME ".strtoupper($_SESSION['user']);
?>
<form method="get" action="searched.php">
<label for="ques"></label>
<input type="text" name="title" id="title" placeholder="Search...">
<button type="submit" name="search"><i class="fa fa-search"></i></button>
</form>
<form method="post" action="question.php">
<button type="submit" name="ask_ques">Ask a Question</button>
</form>
</body>
</html>
This file gets the input from search bar and displays the title with question and answer if any. It also contains a comment box to answer the question:
searched.php
<?php
session_start();
?>
<html>
<head>
</head>
<body>
<?php
$conn=new mysqli("localhost","khushank","sethi","q&a");
if($conn->connect_error){
echo "unable to connect";
}
if($_SERVER['REQUEST_METHOD']=='GET'){
if(isset($_GET['search'])){
$title=$_GET['title'];
$qsel=" SELECT title,qemail,ques FROM question WHERE title='$title' ";
if($qresult=$conn->query($qsel)){
if($qresult->num_rows==0){
header('location:question.php');
}
else{
while($qres=$qresult->fetch_assoc()){
echo "<strong>".ucfirst($qres['title'])."</strong><br><br>";
echo $qres['qemail'];
?>
<textarea cols="65" id="qdes"><?php echo $qres['ques']; ?></textarea><br><br>
<?php
$asel=" SELECT answer.aemail,answer.ans FROM question JOIN answer ON question.ques=answer.ques ";
if($aresult=$conn->query($asel)){
if($aresult->num_rows>0){
while($ares=$aresult->fetch_assoc()){
echo"Answer:";
?>
<textarea cols="65" id="ades"><?php echo $ares['ans']; ?></textarea><br><br>
<?php
}
}
?>
<form method="get" action="insertA.php?$ques='$qres['ques']'">
<label for="ans"><?php
echo $_SESSION['user'];
?></label>
<textarea cols="90" name="ans" placeholder="Your Answer"></textarea>
<input type="submit" name="comment" value="submit">
</form>
<?php
}
else{
echo "answer not selected";
}
}
}
}
else{
echo"not selected";
}
}
}
$conn->close();
?>
</body>
</html>
In this file answer is stored using GET method, but unable to get inserted into the database:
insert.php
<?php
require 'searched.php';
$conn=new mysqli("localhost","khushank","sethi","q&a");
if($conn->connect_error){
echo "unable to connect";
}
echo"connected";
if($_SERVER['REQUEST_METHOD']=='GET'){
if(isset($_GET['comment'])){
$ans=mysql_real_escape_string($_GET['ans']);
$username=$_SESSION['user'];
//$ques=$_GET['$ques'];
$insa=" INSERT INTO answer(aemail,ans) VALUES('$username','$ans') " ;
if($conn->query($insa)){
echo"inserted";
echo"<script type='text/javascript'>".'alert("your answer is posted successfully");
</script>';
}
else{
echo"not inserted";
}
}
}
else{
echo"1";
}
$conn->close();
?>
I'm unable to insert the values stored in $ans
.