Javascript ajax导致mysql重复条目?

Javascript ajax导致mysql重复条目?,javascript,mysql,ajax,Javascript,Mysql,Ajax,我使用下面的ajax脚本来运行MySQL函数,并在表中插入一个条目。它工作正常,只是把条目复制了两次 <script type="text/javascript"> $(document).ready(function() { $('#support1').click(function(e) { var sel_stud = "support1"; $.ajax({ type: "POST", u

我使用下面的ajax脚本来运行MySQL函数,并在表中插入一个条目。它工作正常,只是把条目复制了两次

<script type="text/javascript">
$(document).ready(function() {
    $('#support1').click(function(e) {
        var sel_stud = "support1";
        $.ajax({
            type: "POST",
            url: "include/run_support_log.php",
            data: 'theOption=' + sel_stud,
            success: function() {
                $('#support_content').show();
            }
        });
    });
});
</script>

$(文档).ready(函数(){
$('#support1')。单击(函数(e){
var sel_stud=“support1”;
$.ajax({
类型:“POST”,
url:“include/run_support_log.php”,
数据:“选项=”+选择螺栓,
成功:函数(){
$(“#支持内容”).show();
}
});
});
});
MySQL:

<?php 
session_start();
include 'config.php';

$type = $_POST['theOption'];
if($type == "support1"){
$type = "Phone Support";
}



$random = 'S' . substr( md5(rand()), 0, 7); echo $random;


$query = "INSERT INTO supplier_log (id, reference, user_id, date, activity_type) VALUES ('', '$reference','{$_SESSION['id']}', now(), '$type')";
$result = mysql_query($query);

if($result) {
    echo 'success';
}

?>

插入id为“”的元素。
所以每个元素都有相同的id

也许你会改变

"INSERT INTO supplier_log (id, reference, user_id, date, activity_type) VALUES ('', '$reference','{$_SESSION['id']}', now(), '$type')";


并且让该id由MySQL生成

您确定该行

include 'config.php';
是不是第二次包含你的脚本


它不应该,因为它会形成一个无限循环。但请检查它以防万一。

您确定您的脚本在页面中只包含一次吗?我非常确定它包含了两次。
ajax
调用了两次吗?@Tushar no我只出现过一次ajax:(感谢您的建议,但是即使我将代码更改为您建议的代码,它仍然会复制条目谢谢您我忘记了我的include'config.php'文件包括存储ajax的jquery.php文件,这现在解决了我的问题。谢谢这是我在这个网站上为某人解决的第一个问题:)我非常受欢迎!
include 'config.php';