我一直在尝试创建一个基于 Web 的秒表,其中开始时间和停止时间的值将存储到 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!"; }
?>
还在弄清楚为什么它被插入两次,有人遇到过同样的问题吗?
谢谢