dongqiya9552 2011-07-04 21:01
浏览 31
已采纳

php $ _SESSION变量没有携带一个Flash游戏?

So I made this flash game and all appropriate variables are entered. If a user is logged in, it SHOULD enter the variable $_SESSION[user] into the mysql database to store the user's score. For some strange reason, it doesn't do that. What makes it even stranger is that I can echo the $_SESSION[user] variable correctly, but when thrown into a mysql DB, it returns NULL (as 0). Here's the code that does all the processing.

<?php 
session_start();
?>
<html>
<?php
include("../package.php");      //stuff for UI

echo $_SESSION[user];           //this works

if($_SESSION[user]){
mysql_query("INSERT INTO `DB`.`TABLE` (`user`,`type`,`game`,`score`) VALUES ('$_SESSION[user]', '$_POST[type]', '$_POST[g]', '$_POST[score]')");
?>
     //Congratulations message in HTML
<?php
}
else{
?>
   //Tells you to login (the 'else' statement to if($_SESSION[user])) also in HTML
<?php
}
?>

I don't see anything wrong....

  • 写回答

1条回答 默认 最新

  • dongshuo2752 2011-07-04 21:06
    关注

    Change your logic to this, for debugging purposes:

    $sql = <<<EOL
    INSERT INTO `DB`.`TABLE` (`user`,`type`,`game`,`score`)
    VALUES ('$_SESSION[user]', '$_POST[type]', '$_POST[g]', '$_POST[score]')
    EOL;
    
    $result = mysql_query($sql) or die("query: $sql
    
    " . mysql_error());
    

    That'll give you the full text of the generated query, as well as the exact error message MySQL produced. If the query succeeds, then things continue as normal. If there was an error, then you'll be told why.

    Most likely the error is due to your mis-quoting of session values:

    PHP's polite and will translate your $_SESSION[user] into $_SESSION['user'] (note the quotes). But when you do this from within a double-quoted string, all bets are off.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算