我有这种形式与ajax脚本连接,当我提交长文本结果不保存在数据库中(在posts表上,我有post_content列作为“longtext”类型)。当我提交一些简单的像“你好世界”)结果正确保存。当我用mysqli的,我已经解决了这个问题与这行代码(使用mysqli_real_escape_string):PDO插入不工作
//$insert_posts = "INSERT INTO posts (post_content,post_date,post_topic,post_by_uid) VALUES ('".mysqli_real_escape_string($conn,$_POST['val'])."',NOW(),$id,'".$_SESSION['uid']."')";
但现在有PDO,我不能。我试过这个:$conn->quote($reply)
但它没有奏效。
<script>
function saveEditorTrigger()
{
for (instance in CKEDITOR.instances) CKEDITOR.instances[instance].updateElement();
}
</script>
<script>
\t $(function() {
\t \t
$('form').on('submit', function (e) {
\t \t saveEditorTrigger();
\t \t var str = CKEDITOR.instances['reply-post'].getData();
var id = <?php echo $_GET['id']; ?>;
\t \t e.preventDefault();
$.ajax({
type: 'post',
url: 'post.php',
data:{ val : str, id : id },
success: function (data) {
alert('Your answer is submitted');
location.replace("topic.php?id=<?php echo $_GET['id']; ?>");
\t \t \t }
});
});
});
</script>
<form id="form" >
\t \t \t \t <br>
\t \t \t \t <textarea name="reply-post"></textarea>
\t \t \t \t \t \t <script>
\t \t \t \t \t \t CKEDITOR.replace("reply-post");
</script>
\t \t \t \t \t <input type="submit" name="submit" value="submit">
\t \t \t \t </form>
的post.php中的文件:
<?php
include 'dbh.php';
session_start();
$reply = $_POST['val'];
$id = $_POST['id'] ;
$insert_posts = $conn -> prepare("INSERT INTO posts (post_content,post_date,post_topic,post_by_uid) VALUES ('".$reply."',NOW(),$id,'".$_SESSION['uid']."')");
//$insert_posts = "INSERT INTO posts (post_content,post_date,post_topic,post_by_uid) VALUES ('".mysqli_real_escape_string($conn,$_POST['val'])."',NOW(),$id,'".$_SESSION['uid']."')";
$insert_posts -> execute();
?>
我的问题是什么我做错了什么?有任何想法吗?
了解有关[PDO] [制备](http://en.wikipedia.org/wiki/Prepared_statement)语句(http://php.net/manual/en/ pdo.prepared-statements.php)。您也可以避免潜在的SQL注入攻击! PDO,[非常简单](http://jayblanchard.net/demystifying_php_pdo.html)。 –
好吧,对于初学者...您没有正确使用PDO。 PDO的要点是不要将您插入到查询中的值放入您的查询中,并将它们作为数组传递给插入对象。 – Dimi
你的代码*对SQL注入*是开放的*。当你检查错误时,数据库*可能*告诉你一个语法错误。纠正SQL注入问题,这个问题可能会变得没有意义。 – David