dsfd3546 2013-06-18 00:32
浏览 9
已采纳

无法通过变量减去表值

I can not get an SQL update statement to subtract a variable from a table value. Here is my code:

$_SESSION_Job101=mysql_fetch_array(mysql_query("SELECT * FROM job_101 WHERE job_101.username='$_SESSION_User'"));

mysql_query("UPDATE characters SET currenergy=currenergy-$_SESSION_Job101['ecost'] WHERE username='$_SESSION_User'");

$_SESSION_Job101 is a perfectly valid result, as I pull from it on another page; I even pull the 'ecost' on said page. I also update currenergy this way in another script, except I use the number 1 instead of the variable. So I've narrowed it down to that variable.

It wouldn't matter that $_SESSION_Job101 is the result from a second table (job_101), and that query is updating to the table characters, would it?

  • 写回答

1条回答 默认 最新

  • douyanjing8287 2013-06-18 00:42
    关注

    We don't have enough information, but since you don't perform ANY error handling or validation that SQL resultset is returned, it could be an error caused by issues such as:

    • no rows returned in first query
    • some other parsing issue not directly evident

    I would propose that you use temporary strings and echo the actual SQL queries. Continue by actually testing them with MYSQL (through workbench, queryviewer, or console) in order to see where and what the error is.

    Also, it's not recommended to skip error checking and try to combine so many lines/steps into 2 lines. Imagine the first query does not return any results for example...

    Debugging:

    $query1 = "SELECT * FROM job_101 WHERE job_101.username='$_SESSION_User'";
    echo $query1."<br/>";
    $_SESSION_Job101=mysql_fetch_array(mysql_query($query1 ));
    $query2 = "UPDATE characters SET currenergy=currenergy-$_SESSION_Job101['ecost'] WHERE username='$_SESSION_User'";
    echo $query2."<br/>";
    mysql_query($query2);
    

    Update

    Based on your comment I suggest you try the following two options:

    1) Add a space between the - and $_SESSION_Job101['ecost'].

    2) If that doesn't work, change your string to:

    mysql_query("UPDATE characters SET currenergy=currenergy-".$_SESSION_Job101['ecost']." WHERE  username='".$_SESSION_User."'";`
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP