2017-04-10 126 views
0

我有这种形式与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(); 
 

 
?>

我的问题是什么我做错了什么?有任何想法吗?

+0

了解有关[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)。 –

+0

好吧,对于初学者...您没有正确使用PDO。 PDO的要点是不要将您插入到查询中的值放入您的查询中,并将它们作为数组传递给插入对象。 – Dimi

+0

你的代码*对SQL注入*是开放的*。当你检查错误时,数据库*可能*告诉你一个语法错误。纠正SQL注入问题,这个问题可能会变得没有意义。 – David

回答

0
<?php 
include 'dbh.php'; 
session_start(); 
$reply = $_POST['val']; 
$id = $_POST['id'] ; 
$userId=$_SESSION['uid']; 
$insert_posts = $conn -> prepare("INSERT INTO posts 
       (post_content,post_date,post_topic,post_by_uid) 
       VALUES 
       (:post_content,:post_date,:post_topic,:post_by_uid)"); 
      $insert_posts->bindParam(':post_content', $reply, PDO::PARAM_STR); 
      $insert_posts->bindParam(':post_date', NOW(), PDO::PARAM_STR); 
      $insert_posts->bindParam(':post_topic', $id, PDO::PARAM_STR); 
      $insert_posts->bindParam(':post_by_uid', $userId, PDO::PARAM_STR); 
      $insert_posts -> execute(); 
?> 

https://www.phpro.org/tutorials/Introduction-to-PHP-PDO.html可能是一个很好的资源,了解

+0

你的代码看起来不错,但根本不起作用....数据库没有记录,即使是简单的文本“你好世界”。唯一我得到的是“你的答案已提交”。 –

+0

尝试先添加静态数据,而不是从表单获取值,$ reply =“Hello”;并运行脚本 – user3127648

+0

我刚刚尝试过使用静态数据,仍然没有记录... –

0

纠正这个代码,我做它的工作。

<?php 
 
include 'dbh.php'; 
 
session_start(); 
 
$reply = $_POST['val']; 
 
$id = $_POST['id'] ; 
 
if (isset($_SESSION['uid'])){ 
 
$user=$_SESSION['uid']; 
 

 
$insert_posts = $conn -> prepare("INSERT INTO posts (post_content,post_date,post_topic,post_by_uid) VALUES (:post_content,NOW(),:post_topic,:post_by_uid)"); 
 
      $insert_posts->bindParam(':post_content', $reply); 
 
      $insert_posts->bindParam(':post_topic', $id); 
 
      $insert_posts->bindParam(':post_by_uid', $user); 
 
      $insert_posts->execute(); 
 
} 
 
?>