如何在mysql中添加textarea内容?

时间:2018-05-22 04:02:16

标签: php mysql

当我提交输入字段正确插入但在textarea中只有pre / pre插入而不插入textarea可以任何人帮助我

表格

 <form method="post">
    <input type="text" class="form-control" id="e" name="e">
    <textarea id="txtEditor" name="a"></textarea>   
    <input type="submit" value="Save" name="save"/>
     </form> 

PHP代码

<?php
$connect= new mysqli("localhost","root","","database") or die("ERROR:could not connect to the database!!!");

extract($_POST);
if(isset($save))
{
$msg="<pre>$a</pre>";

$query = "INSERT INTO questions (e, a) VALUES ('$e', '$msg')";
$connect->query($query);
echo "Data saved";  
}
?>

2 个答案:

答案 0 :(得分:0)

为什么使用extract()。我建议尝试这样的事情,因为这段代码可读且简单。

$connect= new mysqli("localhost","root","","database") or die("ERROR:could not connect to the database!!!");
if (isset($_POST['save'])) {
    $e=$_POST['e'];
    $msg=$_POST['a'];   
    $query = "INSERT INTO questions (e,a) VALUES ('$e','$msg')";
    if ($connect->query($query)) {
        echo 'data save';
    }
} 
  

注意:给定代码对sql注入攻击是开放的,你应该使用   准备好的声明。

答案 1 :(得分:-1)

<form method="post" action="file_name.php">
    <input type="text" class="form-control" id="e" name="e">
    <textarea id="txtEditor" name="a"></textarea>   
    <input type="submit" value="Save" name="save"/>
     </form> 

<强> file_name.php:

$connect= new mysqli("localhost","root","","database") or die("ERROR:could not connect to the database!!!");

//extract($_POST);
if(isset($_POST['save']))
{

$msg= "<pre>".$_POST['a']."</pre>";
$e = $_POST['e']; // you are not declare this
$query = "INSERT INTO questions (e, a) VALUES ('$e', '$msg')";
$insert = $connect->query($query);
if($insert == true)
{
echo "Data saved";  
}
}
  

注意:确保您的a字段应为数据库中的文本数据类型