如何将文本区域保存到SQL?
这是html:
<form method="post" action="text-handler.php">
<textarea name="content" style="width:100%;cursor:text;"></textarea>
</form>
和php:
<?php
session_start();
$user_name = $_SESSION['user_name'];
require_once("config/db.php");
require_once("config/db_connection.php");
$newtext = $_POST['content'];
$sql = mysqli_query($db_connection, "INSERT INTO 'texts' ('text', 'user') VALUES ($newtext, $user_name)");
?>
请记住,标识符引号是反引号,而不是单引号:
在您的代码中:
INTO 'texts' ('text', 'user')
^ ^ ^ ^ ^ ^ // use backticks not single quotes
这个查询是不安全的,并且直接在语句中注入用户输入,并且容易注入sql。
最好使用预先准备好的语句:
session_start();
$user_name = $_SESSION['user_name'];
require_once("config/db.php");
require_once("config/db_connection.php");
$newtext = $_POST['content'];
$sql = 'INSERT INTO `texts` (`text`, `user`) VALUES (?, ?)';
$insert = $db_connection->prepare($sql);
$insert->bind_param('ss', $newtext, $user_name);
$insert->execute();
if($insert->affected_rows > 0) {
echo 'insert complete';
}
我只是在使用这种代码这是html和/////////
这是控制部分公共函数insert_projects_to_db($data){
return $this->db->insert('projects', $data);
}
数据库中用于发送的函数=将数据固定到数据库$pdatea['description']=$this->input->post('description');