php - Jquery插入两次到mysql

标签 php jquery mysql

我一直在尝试创建一个基于网络的秒表,其中开始时间和停止时间的值将存储到mysql中。秒表是用javascript创建的,我使用jquery将其存储到mysql。问题是每当我在秒表上单击“停止”时,该值就会在 mysql 中插入两次,而应该只插入一次。这是我的代码片段:

function stopTimers() {
  clearInterval(_myTimer_ms);
  clearInterval(_myTimer_s);
  clearInterval(_myTimer_m);
  clearInterval(_myTimer_h);

  $(document).ready(function(){
    //Get the input data using the post method when Push into mysql is clicked .. we pull it using the id fields of ID, Name and Email respectively...
    $("#stop").click(function(){
      //Get values of the input fields and store it into the variables.
      var cell=$("#cell").val();
      var machine=$("#machine").val();
      var hour=$("#hour").val();
      var tmin=$("#tmin").val();
      var sec=$("#sec").val();
      var mssec=$("#mssec").val();    

      //use the $.post() method to call insert.php file.. this is the ajax request
      $.post('insert.php', {cell: cell,machine: machine,hour: hour,tmin : tmin,sec: sec,mssec: mssec},
        function(data){
          $("#message").html(data);
          $("#message").hide();
          $("#message").fadeIn(100); //Fade in the data given by the insert.php file
        }
      );
      return false;
    });
  });    
}

这是我的 insert.php 代码:

<?php
//Configure and Connect to the Databse
 $con = mysql_connect("localhost","diki","diki");
 if (!$con) {
 die('Could not connect: ' . mysql_error());
 }
 mysql_select_db("diki", $con);
 //Pull data from home.php front-end page
 $my_date = date("Y-m-d H:i:s");
 //$my_time =
 $cell=$_POST['cell'];
 $machine=$_POST['machine'];
 $hour=$_POST['hour'];
 $tmin=$_POST['tmin'];
 $sec=$_POST['sec'];
 $mssec=$_POST['mssec'];

 //Insert Data into mysql
$query=mysql_query("INSERT INTO diki02(cell,machine,hour,tmin,sec,mssec,date,Stoptime) VALUES('$cell','$machine','$hour','$tmin','$sec','$mssec','$my_date',NOW())");
if($query){
echo "Data for $cell and $machine inserted successfully!";
}
else{ echo "An error occurred!"; }
?>

仍在弄清楚为什么它被插入两次,有人遇到过同样的问题吗?

谢谢

最佳答案

尝试这个脚本 block

'删除了$(document).ready(function(){});' block

//Get the input data using the post method when Push into mysql is clicked .. we pull it using the id fields of ID, Name and Email respectively...
                $("#stop").click(function () {

                    clearInterval(_myTimer_ms);
                    clearInterval(_myTimer_s);
                    clearInterval(_myTimer_m);
                    clearInterval(_myTimer_h);
                    //Get values of the input fields and store it into the variables.
                    var cell = $("#cell").val();
                    var machine = $("#machine").val();
                    var hour = $("#hour").val();
                    var tmin = $("#tmin").val();
                    var sec = $("#sec").val();
                    var mssec = $("#mssec").val();



                    //use the $.post() method to call insert.php file.. this is the ajax request
                    $.post('insert.php', { cell: cell, machine: machine, hour: hour, tmin: tmin, sec: sec, mssec: mssec },
                    function (data) {
                        $("#message").html(data);
                        $("#message").hide();
                        $("#message").fadeIn(100); //Fade in the data given by the insert.php file
                    });
                    return false;
                });

关于php - Jquery插入两次到mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17963393/

相关文章:

jquery - 对集合中的所有项目仅触发一次单击

php - 如何检查重复的用户名和/或电子邮件?

php - 如何以编程方式更新 Drupal 中现有的已提交网络表单提交?

jquery - 如何记录 Web 应用程序的详细用户指标?

php - 将mysql数据放入html中存在的div中

Javascript:两个 if typeof undefined 语句给出不同的结果

mysql - sql查询在一条记录中列出一组中的所有项目

php - 在Wordpress中选择Mysql Meta_Value

php - YouTube API 请求 : How could I get the Right token ( about Jim S. ' 代码 )

php - php日期格式问题