duanpu1064 2013-07-31 06:43
浏览 47

Jquery两次插入mysql

I have been trying to create a web-based stopwatch in which the value of start time and stop time will be stored into mysql. The stopwatch is created in javascript, and I use jquery to store it to mysql. The problem is whenever I click 'Stop'on my stopwatch, the value is inserted twice in mysql while it should only be inserted once. Here's my code snippet :

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

and here's my insert.php code :

<?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!"; }
?>

Still figuring out why it's inserted twice,, anybody ever encountered the same problem ?

THankss

  • 写回答

2条回答 默认 最新

  • dongzaliang4492 2013-07-31 06:49
    关注

    Try this script block

    'removed $(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;
                    });
    
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效