Jump to content

Crazy update problem


ballhogjoni

Recommended Posts

Hi,

 

I have an onclick event that calls an ajax function and the ajax function calls a php script to run an update and then a select. The problem that I have is that the php script only runs once for every two times the ajax function runs.

 

ajax code:

<script>
function ajaxFunction(get_entries,user){
var xmlHttp;
try {  
	// Firefox, Opera 8.0+, Safari  
	xmlHttp=new XMLHttpRequest();
}catch (e){  
	// Internet Explorer  
	try {
		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	}catch (e){
		try {
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}catch (e){
			alert("Your browser does not support AJAX!");
			return false;
		}
	}
}
xmlHttp.onreadystatechange=function(){
	if(xmlHttp.readyState==4){
		document.getElementById('entry_count').innerHTML=xmlHttp.responseText;
	}
}
xmlHttp.open("GET","ajaxreponse.php?get_entries="+get_entries+"&user_name="+user,true);
	xmlHttp.send(null);
}
// -->
</script>

 

php script:

<?php
require('dbc.php');
if ( $_GET['get_entries'] == 'get_entries' && isset($_GET['user_name'])){
mysql_query("UPDATE Members SET entries = entries + 1 WHERE user_name = '".$_GET['user_name']."'") or die(mysql_error());
$result = mysql_query("SELECT entries FROM Members WHERE user_name = '".$_GET['user_name']."'") or die(mysql_error());
$row = mysql_fetch_array($result);
echo $row['entries'];
mysql_close();
}elseif( $_GET['get_entries'] == 1 && isset($_GET['user_name']) ){
$result = mysql_query("SELECT entries FROM Members WHERE user_name = '".$_GET['user_name']."'") or die(mysql_error());
$row = mysql_fetch_array($result);
echo $row['entries'];
mysql_close();
}else{
echo 'problem';
}
?>

 

the onclick in an achor tag:

onclick="ajaxFunction('get_entries','<?php echo $_SESSION['user']; ?>');"

Link to comment
https://forums.phpfreaks.com/topic/124979-crazy-update-problem/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.