我有兩個按鈕(登錄和註銷)。當註銷活動時,如果用戶再次點擊註銷按鈕,則不應該插入或更新。登錄按鈕也一樣。如果用戶點擊活動按鈕,停止插入
現在,它會在用戶單擊活動按鈕時進行插入。我需要避免這種情況。
在此先感謝。
代碼:
<script>
function insertDateTime(obj, id)
{
$("[id^='"+obj.id+"']").css('background', '#FFFFFF').css('font-weight', 'normal').css('font-size', '14px').css('color', 'grey');
$(obj).css('background', '#D4B357').css('font-weight', 'bold').css('font-size', '15px').css('color', 'black');
if (window.XMLHttpRequest) {
xmlhttp=new XMLHttpRequest();
} else { // code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
var params = "id="+id;
xmlhttp.open("POST","insertdate.php",true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
/* xmlhttp.setRequestHeader("Content-length", params.length);
xmlhttp.setRequestHeader("Connection", "close");*/
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
//alert(xmlhttp.responseText);
}
}
xmlhttp.send(params);
}
</script>
getuser.php
<table>
<tr>
<td>
<button id="btnSave" <?php if($entry=='1'){?> style=" background:#D4B357; font-weight:bold; font-size:15px; color:black;outline:0px;"<?php }?> onclick="insertDateTime(this,'<?php echo $q;?>');"> Login</button>
</td>
<td>
<button id="btnSave" <?php if($entry=='0'){?> style=" background:#D4B357; font-weight:bold; font-size:15px;color:black;outline:0px;"<?php }?> onclick="insertDateTime(this,'<?php echo $q;?>');"> Logout</button>
</td>
</tr>
</table>
insertdate.php date_default_timezone_set( 「亞/加爾各答」); $日期=日期(「年月日」,時間());?>
<?php $time=date("H:i:s",time());
include_once "database.php";
$q = $_POST['id'];
$sql_check="select * from entries where user_id='$q' order by id desc";/*last row*/
$res_check=mysql_query($sql_check);
$row=mysql_fetch_array($res_check);
$logout_date=$row['logout_date'];
$num=mysql_num_rows($res_check);
$id=$row['id'];
if($logout_date != "" || $num=='0'){
$sql1="insert into entries(user_id,login_date,login_time,logout_date,logout_time)values('$q','$date','$time','','')";
$res1=mysql_query($sql1);
$sql="update users set entry='1' where id='$q'";
$res=mysql_query($sql);
}
else{
$sql="update entries set logout_date='$date', logout_time='$time' where id='$id'";
$res=mysql_query($sql);
$sql1="update users set entry='0' where id='$q'";
$res1=mysql_query($sql1);
}
**危險**:您正在使用[an **過時的**數據庫API](http://stackoverflow.com/q/12859942/19068),並應使用[現代替換](http:// php.net/manual/en/mysqlinfo.api.choosing.php)。你也**易受[SQL注入攻擊](http://bobby-tables.com/)**,現代的API會使[防禦]更容易(http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php)自己從。 – Quentin 2014-09-19 06:40:06
任何人請幫助 – user2372469 2014-09-19 08:45:22