0

为什么当我在 textarea 中输入“+-1-23$%^&sdfsdf/><”但它只将“-1-23$%^”保存到数据库中?

代码 :

function postingMsg (){
        $('.error').hide();
    var messageposting2= $("textarea#messageposting").val();
        var dataString = 'messageposting2='+ messageposting2;
        $.ajax({
             type: "POST",
             url: "note-send.php",
             data: dataString,
             success: function(msg) {
         msg = parseFloat(msg)      
             }
        });
        return false;   
}

if ((isset($_POST['messageposting2'])) && (strlen($_POST['messageposting2']) > 0)) {
    $messageposting3 = $_POST['messageposting2'];   

    $sql = "UPDATE users 
            SET my_note=?
            WHERE user_id=?";
    $q = $conn->prepare($sql);
    $q->execute(array($messageposting3, $_SESSION['user_id']));

    echo "1";
} else {echo "0";}
4

1 回答 1

3

It has nothing to do with PDO or your database. You must URL-encode your string before sending it through Ajax.

var dataString = 'messageposting2='+ encodeURIComponent(messageposting2);
于 2012-08-01T15:11:39.083 回答