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

6jjcrrmo  于 2021-06-20  发布在  Mysql
关注(0)|答案(2)|浏览(265)

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

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 .

oxcyiej7

oxcyiej71#

这是解决你问题的正确方法。虽然我没有看到您的模式,但它看起来有点错误,因为您是使用question字段加入表的,根据您的问题,question字段可能是string数据类型。因此,建议的数据库结构如下:
问题表:

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

数据库连接.php

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

把它们放在同一个目录下,就行了。希望这对你有帮助。

93ze6v8z

93ze6v8z2#

问题是不能将php直接写入html。注意search.php表单标签。用这个。

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

还有一点,您的页面名称只是insert.php,而不是inserta.php

相关问题