开发者

php script seems to be ignoring if statements?

开发者 https://www.devze.com 2023-04-08 19:07 出处:网络
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 catc

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 wa开发者_运维问答y 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:

$result = mysql_query("SELECT * FROM sessionrequests......");

if ($result) { // Query executed successfully
if (mysql_num_rows($result) > 0) { // Matches returned
$row = mysql_fetch_assoc($result);
$sessionstatus = $row['sessionstatus'];
$sessionid = $row['id'];

// Rest of your code

}
}

mysql_close($con);


Your query is only grabbing data WHERE coach='$user' AND sessionstatus=''. $sessionstatus is always "" so if($sessionstatus != "") { die("no"); } is never called.

I still don’t understand the problem though. It’s only gathering requests that have not been received and marking the first such request as received. Isn’t that what you want it to do? What exactly is the problem?

0

精彩评论

暂无评论...
验证码 换一张
取 消