I have AJAX running a query every 5 seconds which checks if a user has requested a session with the coach. and if there is it pops up with a dialog. the script changes the field so that it won’t catch the same session request, but it doesn’t seem to care if the field is blank or not it just runs all the way through the php script. here’s the ajax and php code.
setInterval(function() {
$.ajax({
type: "GET",
url: "includes/checksessionrequests.php",
success: function(msg){
if(msg == "yes") {
$(\'$<div title="Request Recieved"></div>\').load(\'requestreceived.php\').dialog({modal: true, closeOnEscape: false, open: function(event, ui) { $(".ui-dialog-titlebar-close").hide(); }, height: 300, width: 480, hide: {effect: "fade", duration: 1000}, show: {effect: "fade", duration: 1000}});
}
}
});
the query
<?php
session_start();
$user = $_SESSION['username'];
include("dbcon.php");
$result = mysql_query("SELECT * FROM sessionrequests
WHERE coach='$user'
AND sessionstatus=''
ORDER BY id") or die(mysql_error());
$row = mysql_fetch_assoc($result);
$sessionstatus = $row['sessionstatus'];
$sessionid = $row['id'];
if($sessionstatus != "") {
die("no");
}
elseif($sessionstatus == "")
{
$result = mysql_query("UPDATE sessionrequests
SET sessionstatus='received'
WHERE id='$id'");
echo "yes";
}
mysql_close($con);
?>
You are not checking if the query is actually successful or if the query returns a match. If either of these scenarios happen, I believe that $sessionstatus will be set to NULL, thus skipping your if and elseif statements. I am still not too familiar with PHP’s weak typing and whether NULL evaluates to “” or not. But, it’s worth a shot. 🙂
I would try something like this: