drfqfuhej48511519 2018-06-17 07:55
浏览 144
已采纳

无法使用php在textarea输入中存储的数据库中插入答案

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.

  • 写回答

2条回答 默认 最新

  • dongmi4720 2018-06-17 08:19
    关注

    the problem is you can not write php direct into html. watch your search.php form tag. use this.

    <form method="get" action="insert.php?<?php echo "$ques='".$qres['ques']."'">
    

    and one more thing your page name is just insert.php not insertA.php

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料