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