2013-07-31 2 views
0

시작 시간과 종료 시간 값이 mysql에 저장되는 웹 기반 스톱워치를 만들려고했습니다. 스톱워치는 자바 스크립트에서 만들어지며 jquery를 사용하여 MySQL에 저장합니다. 문제는 내가 스톱워치를 클릭 할 때마다 값이 mysql에 두 번 삽입되는 반면 한 번만 삽입되어야한다는 것입니다. 내 insert.php 코드의 여기Jquery가 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; 
    }); 
    });  
} 

과 :이 두 번 삽입 왜

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

아직도 파악 ,, 사람이 지금 같은 문제가 발생 여기 내 코드는입니까?

THankss

+0

코드에서 어떤 일이 일어나고 있는지 잘 모르겠습니다. 이'stopTimers' 함수는 무엇입니까? 전화가 어디 있습니까? 왜 그것은'$ (document) .ready'를 포함합니까? 'click' 핸들러를'# stop'에 여러 번 바인드하는 이상한 일을하는 것이 가능합니다. – freakish

+0

stopTimers 함수는 스톱워치를 멈추고 mysql 데이터베이스에 데이터 (cell, machine, hour, tmin, sec, mssec 등)를 저장합니다. – placeb0

답변

0

가 '$(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() { 

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

안녕하세요,이 스크립트를 시도했지만 여전히 두 번 mysql에 삽입합니다. ( – placeb0

0

당신의 $.post 나는없이합니다 (DOM이 준비 때 직접 호출 할 수 있습니다 가정 $(document).ready()에 있기 때문에 사용자 또는 자바 스크립트의 다른 작업).

이 함수를 호출하면 stopTimer() 함수이기도하므로 두 번째로 호출 될 수 있습니다.

관련 문제