1

我正在尝试使用 jQuery Ajax 和 PHP 将记录插入 MySQL 数据库。我的问题是它没有在表中插入任何行。我试过这段代码:

对于 html :

 `<script>
$(function(){
    //insert record
    $('#insert').click(function(){
        var jcomments_manager = $('#fcomments_manager').val();


        //syntax - $.post('filename', {data}, function(response){});
        $.post('data.php',{action: "insert", name:jcomments_manager},function(res){
            $('#result').html(res);
        });     
    });

    //show records
    $('#show').click(function(){
        $.post('data.php',{action: "show"},function(res){
            $('#result').html(res);
        });     
    });
});
</script>
Add a comment: <input type="text" id="fcomments_manager" />

            <button id="insert">Insert</button>

`

对于 php :

`if($_POST['action'] == 'insert'){

    $comments_manager  = mysql_real_escape_string($_POST['comments_manager']);

    $c_id = $_GET['c_id'];

    $sql   = "INSERT INTO candidate (comments_manager) 
                VALUES ('$comments_manager')
               WHERE c_id = '$c_id'
               ";
    $query = mysql_query($sql);
    if($query){
        echo "Record Inserted.";
    }else {
        echo "Something Wrong!";
    }
}

`

4

2 回答 2

0

您的参数名称不匹配

    //in server the manager parameter is read using name `comments_manager`, here you were passing it as name
    $.post('data.php',{action: "insert", comments_manager:jcomments_manager},function(res){
        $('#result').html(res);
    });
于 2013-08-23T03:14:26.380 回答
0

$comments_manager = mysql_real_escape_string($_POST['comments_manager']);

那不应该是:

$comments_manager = mysql_real_escape_string($_POST['name']);

于 2013-08-23T03:14:35.720 回答