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

时间:2018-06-17 07:55:04

标签: php html mysql

此文件包含搜索表单的代码:

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>

此文件从搜索栏获取输入,并显示带问题和答案的标题(如果有)。它还包含一个回答问题的评论框:

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>

在此文件中,使用GET方法存储答案,但无法插入数据库:

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();
?>

我无法插入$ans中存储的值。

2 个答案:

答案 0 :(得分:0)

问题是你无法直接将php写入html。看你的search.php表格标签。用这个。

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

还有一件事,你的页面名称只是insert.php而不是insertA.php

答案 1 :(得分:0)

以下是您问题的更正解决方案。虽然我没有看到你的模式,但是看起来有点不对,因为你使用问题字段加入表格,根据你的问题可能是字符串数据类型。所以这是建议的数据库结构:

问题表:

DROP TABLE IF EXISTS `question`;
CREATE TABLE IF NOT EXISTS `question` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `title` varchar(50) NOT NULL,
  `qemail` varchar(100) NOT NULL,
  `que` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=latin1;
--
-- Dumping data for table `question`
--    
INSERT INTO `question` (`id`, `title`, `qemail`, `que`) VALUES
(1, 'Physics', 'thanga@gmail.com', 'This is the quesiotn');
COMMIT;

答案表:

CREATE TABLE IF NOT EXISTS `answer` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `questionid` int(11) NOT NULL,
  `aemail` varchar(100) NOT NULL,
  `ans` text NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=7 DEFAULT CHARSET=latin1;
--
-- Dumping data for table `answer`
--
INSERT INTO `answer` (`id`, `questionid`, `aemail`, `ans`) VALUES
(1, 1, 'thanga@gmail.com', 'My answer is here'),
(2, 1, 'Khuskant', 'The new answer');

我们在答案表中添加问题ID,因为我们需要它来连接两个表。这意味着我们使用问题ID进行关系。

将search.php更改为:

<?php
session_start();
$_SESSION['user']='Khuskant'
?>
<html>
    <head></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" value="token"><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>

当您提交上述表单时,您可以转到question.php或searching.php,具体取决于数据库中搜索词的可用性。如果您希望将搜索项与存储在问题表中的确切数据进行匹配,则以下代码将执行以下操作: searched.php

<?php
session_start();
require_once "dbconnect.php";
?>
<html>
    <head>
    </head>
    <body class="content">
         <?php            
            if($_SERVER['REQUEST_METHOD']=='GET'){
                if(isset($_GET['search'])){
                    $title= filter_var($_GET['title'],FILTER_SANITIZE_STRING);
                    $stmt = $conn->prepare("SELECT id, title, qemail, que 
                             FROM question WHERE title=?");
                    $stmt->bind_param("s", $title);
                    $stmt->execute();
                    $result = $stmt->get_result();  
                    if($result->num_rows==0){
                        header('location:question.php');
                    }else{
                        while ($qres = $result->fetch_array(MYSQLI_ASSOC)) {
                            echo "<strong>".ucfirst($qres['title'])."</strong><br><br>";
                            echo $qres['qemail']; ?>
                            <textarea cols="65" id="qdes"><?php echo $qres['que']; ?></textarea>
                        <?php 
                            $qid = $qres['id'];
                            $aresult = $conn->query("SELECT answer.aemail, answer.ans 
                                                    FROM question 
                                                    LEFT JOIN answer ON question.id=answer.questionid
                                                    WHERE question.id=$qid");                           
                            if($aresult->num_rows>0){
                                 while($ares = $aresult->fetch_assoc()){                                        
                                      echo"<br>Answer:"; ?>
                                  <textarea cols="65" id="ades"><?php echo $ares['ans']; ?></textarea><br><br>           
                            <?php } } ?>
                            <form method="get" action="insert.php">
                               <label for="ans"><?php echo $_SESSION['user'];?></label>
                                <textarea cols="90" name="ans" placeholder="Your Answer"></textarea>
                                <input type="hidden" name="qid" value="<?php echo $qid;?>">
                                <input type="submit" name="comment" value="submit">
                            </form>
                    <?php 
                    }
                }
            }
        } ?>       
    </body>
</html>

如果你想要部分匹配,你必须改变这部分代码:

$stmt = $conn->prepare("SELECT id, title, qemail, que 
                        FROM question WHERE title=?");
$stmt->bind_param("s", $title);

$stmt = $conn->prepare("SELECT id, title, qemail, que 
                        FROM question WHERE title LIKE ?");
$stmt->bind_param("s", $title."%");

将insert.php更改为:

<?php
session_start();
require 'dbconnect.php';
if($_SERVER['REQUEST_METHOD']=='GET'){
    if(isset($_GET['comment'])){
        $ans=filter_var($_GET['ans'], FILTER_SANITIZE_STRING);
        $qid=filter_var($_GET['qid'], FILTER_SANITIZE_NUMBER_INT);
        $username=$_SESSION['user'];
        $insa= $conn->prepare("INSERT INTO answer(questionid, aemail, ans) VALUES(?,?,?)");
        $insa->bind_param('iss', $qid, $username, $ans);
        $insa->execute();
        if($insa->affected_rows>0){
            echo $insa->affected_rows." rows inserted";
            exit;           
        } else{
            echo"not inserted";
            exit;
        }
    }
}
else{
    echo "1";
}
$conn->close();
?>

dbconnect.php

$conn = new mysqli("localhost", "user", "pass", "testdb");
if ($conn->connect_errno) {
    echo "Failed to connect to MySQL: " . $conn->connect_error;
}

将它们全部放在同一目录中,它会起作用。希望这会对你有所帮助。