php - 如何使用文本区域将这些数据存储在数据库中?

标签 php html mysql

我收到此错误消息:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Hello World'; ?>')' at line 1

我希望存储以下数据:

<?php echo 'Hello World'; ?>

这是我的代码:

    if(isset($_POST['submit'])){
    $post = $_POST['post'];


    $tqs = "INSERT INTO ttn03 (post) VALUES ('$post')";

    $tqr = mysqli_query($dbc, $tqs) or die(mysqli_error($dbc));

    echo "The data has gotten successfully added.";
}

使用这两个对我来说不起作用:

htmlspecialchars();
htmlentities();

这是 HTML 表单:

    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
    <textarea name="post" value=""></textarea>
    <br/>
    <input type="submit" name="submit"/>
</form>

数据库排序规则已启用:

utf8_unicode_ci

这在我的标题中:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

有什么建议吗?

最佳答案

代码应该是这样的:

// first of all, make sure 'post' is set - don't let PHP guessing what to when it's not set.
if(isset($_POST['post'])){
    $post = $_POST['post'];
}else{
    $post = '';
}

// this is your SQL QUERY. Never, ever, ever, throw data in sql queries
$tqs = $mysqli->prepare('INSERT INTO ttn03 (post) VALUES (?)');

// this is how to pass data to the statement
$statement->bind_param('s', $post);

// execute the statement and prints errors
if(!$statement->execute()){
    die('Error '. $mysqli->errno .': '. $mysqli->error);
}

// close statement
$statement->close();
<小时/>

您可以在这里找到一些优秀的文档和示例:

http://www.sanwebe.com/2013/03/basic-php-mysqli-usage

关于php - 如何使用文本区域将这些数据存储在数据库中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26191557/

相关文章:

php - AJAX + PHP 数据搜索

javascript - Chrome : Uncaught (in promise) DOMException: Failed to load because no supported source was found

javascript - textarea 属性 wrap=hard 不适用于粘贴的文本,有什么解决方法吗?

mysql - MySQL 插入时表级锁锁定什么?

PHP 搜索查询精确单词匹配 CONCAT

PHP 插入过滤器并将其打印在图像上

php - XAMPP发送邮件

php - 如何使用mysql和php计算列值的总和?

javascript - 为什么 .getElementById ("name_val"+id).innerHTML 为空?

php - 重复更新 - 不起作用