jquery - 无法使用jQuery将记录插入数据库

标签 jquery mysql sql

我正在尝试使用 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!";
    }
}

`

最佳答案

你的参数名称不匹配

    //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);
    });

关于jquery - 无法使用jQuery将记录插入数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18394065/

相关文章:

javascript - ColorBox jQuery 帮助请求

javascript - jquery - 将引用 dom 元素的变量与选择器混合

mysql - 在 mysql 中有 match 和 group_concat

mysql - 使用GNU Parallel在shell脚本中并行执行多条sql语句

mysql - 使用的表类型不支持子查询上的 FULLTEXT 索引

mysql - sql:使用单个查询在另一列中选择具有相同列值的行

MySQL 查询 : Aggregation at two different level

sql - 使用嵌套查询选择随机数据?

javascript - 如何将 div 背景与其内容的底部对齐

javascript - 无法使用 jQuery 从下拉列表中获取选定的值