转换varchar值时,PHP查询返回'转换失败'

时间:2017-03-31 13:23:21

标签: php jquery sql sql-server

基本上我通过Ajax请求将一些数据发送到php脚本,该脚本应该运行查询来更新数据库中的表。

然而,它一直在采取文本和注意ID我想没有问题: enter image description here 所以我知道它发送我想要的数据,但表格没有更新(它没有单词"附加文本"来自我发送的数据)

现在在网络选项卡下的firefox Developer窗口中,在预览下我有: enter image description here 这似乎是抛出一个SQL错误?

这是从请求运行的php脚本:

<?php include 'connectionDetails.php'; ?>

<?php


if (isset($_POST['noteid1'], $_POST['notetext1'])) 
{

    $noteid2 = $_POST['noteid1'];
    $notetext2 = $_POST['notetext1'];

    $stmt = "UPDATE Notes SET Note = ? WHERE NoteID = ?";
    $params = array($noteid2, $notetext2);

    $stmt = sqlsrv_query($conn, $stmt, $params);

    if ($stmt === false) 
    {
        die( print_r(sqlsrv_errors(), true));
    }
}
else
{
    echo "No Data";

}


?>

这是请求本身:

function submitNoteText()
{
    var noteid = <?php if(isset($_POST['noteid'])){ echo $_POST['noteid'];} ?>;
    var notetext = $("#ta1").val();

    if(noteid == ''||notetext == '')
    {
        alert("NoteID or Text is blank");
    }
    else
    {   
        $.ajax({
            type: "POST",
            url: "submitNoteText.php",
            dataType: 'json',
            data: {noteid1: noteid, notetext1: notetext},
            cache: false,
            success: function(result){
                alert("Success");
            }
        });
    }
    return false;
};

所以我猜它没有更新表的原因是那个错误,所以我的问题是我如何安全地在php中编写查询而不会出现错误?

1 个答案:

答案 0 :(得分:0)

我认为您已忘记Note列中的单引号。试试这个:

$noteid2 = $_POST['noteid1'];
$notetext2 = $_POST['notetext1'];

$stmt = "UPDATE Notes SET Note = '$notetext2' WHERE NoteID = ?";