简体   繁体   中英

PDO Insert not working

I have this form connected with ajax script, when i submit long texts the results does not saving in database(on posts table, i have post_content column as "longtext" type).When i submit something simple(like "hello world") the results saving correctly. When i used to use mysqli , i had solved the issue with this line of code(using mysqli_real_escape_string ):

//$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']."')";

But now with PDO, i can't. I tried this : $conn->quote($reply) but it doesn't worked.

 <script> function saveEditorTrigger() { for ( instance in CKEDITOR.instances ) CKEDITOR.instances[instance].updateElement(); } </script> <script> $(function () { $('form').on('submit', function (e) { saveEditorTrigger(); var str = CKEDITOR.instances['reply-post'].getData(); var id = <?php echo $_GET['id']; ?>; 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']; ?>"); } }); }); }); </script> 
 <form id="form" > <br> <textarea name="reply-post"></textarea> <script> CKEDITOR.replace( "reply-post" ); </script> <input type="submit" name="submit" value="submit"> </form> 

The post.php file:

 <?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(); ?> 

My question is what am i doing wrong? Any ideas?

<?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 might be a good resource to learn

Correcting this code, i make it worked.

 <?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(); } ?> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM