未定义的索引ID

时间:2013-03-26 13:37:38

标签: php html mysql

( ! ) Notice: Undefined index: id in C:\wamp\www\game\units.php on line 29
Call Stack
#   Time    Memory  Function    Location
1   0.0095  708360  {main}( )   ..\units.php:0

( ! ) Notice: Undefined index: id in C:\wamp\www\game\units.php on line 32
Call Stack
#   Time    Memory  Function    Location
1   0.0095  708360  {main}( )   ..\units.php:0
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE `stats` SET `food`='60' WHERE `id`=''' at line 1

这是我的units.php代码

units.php

4 个答案:

答案 0 :(得分:2)

在您的源代码中

$update_food = mysql_query("WHERE `stats` SET `food`='".$stats['food']."' 
WHERE `id`='".$_SESSION['uid']."'") or die(mysql_error());

转换为

WHERE `stats` SET `food` = '....' WHERE `id` = '....'

您可能需要

$update_food = mysql_query("UPDATE `stats` SET `food`='".$stats['food']."' 
WHERE `id`='".$_SESSION['uid']."'") or die(mysql_error());

(用第31行的“UPDATE”替换第一个“WHERE”)

答案 1 :(得分:1)

应该是$_SESSION['uid']而不是$_SESSION['id']吗?

在第5行,您正在检查uid,然后您也可以稍后使用uid

答案 2 :(得分:0)

这是因为你在同一个SQL脚本中有两个WHERE,正如错误所指定的那样。

  

WHERE stats SET food ='60'WHERE id = ...

我猜这应该是UPDATE stats而已。

  

UPDATE stats SET Food ='60'WHERES id = ...

mysql_query("UPDATE `stats` SET `food`='".$stats['food']."'
             WHERE `id`='".$_SESSION['id']."'") or die(mysql_error());

答案 3 :(得分:0)

我使用$ aux var:

进行了此更改
<?php
    session_start();
    include("header.php");

    if(!isset($_SESSION['uid'])) {
    echo "You must be logged in to view this page";
    }else{
    if(isset($_POST['train'])) {
    $worker = protect($_POST['worker']);
    $farmer = protect($_POST['farmer']);
    $warrior = protect($_POST['warrior']);
    $defender = protect($_POST['defender']);
    $food_needed = (10 * $worker) + (10 * $farmer) + (10 * $warrior) + (10 * $defender);
    if($worker < 0 || $farmer < 0 || $warrior < 0 || $defender < 0) {
    output("You must train a positive number of units.");
    }elseif($stats['food'] < $food_needed) {
    output("You do not have enough food.");
    }else{
    $unit['worker'] += $worker;
    $unit['farmer'] += $farmer;
    $unit['warrior'] += $warrior;
    $unit['defender'] += $defender;

    if(isset($_SESSION['id'])){ $aux = $_SESSION['id'];}else{$aux= 0;}

    $update_unit = mysql_query("UPDATE `unit` SET
    `worker`='".$unit['worker']."',
    `farmer`='".$unit['farmer']."',
    `warrior`='".$unit['warrior']."',
    `defender`='".$unit['defender']."'
    WHERE `id`='".$_SESSION['id']."'") or die(mysql_error());
    $stats['food'] -= $food_needed;
    $update_food = mysql_query("WHERE `stats` SET `food`='".$stats['food']."'
    WHERE `id`='".$aux."'") or die(mysql_error()); // here you can use $_SESSION['uid'] or NOT??
    output("You have trained your units!");
    }
    }elseif(isset($_POST['train'])) {

    }
    ?>

检查是否存在...并将$ aux替换为值(在这种情况下为零)...或者您可以在评论中使用建议,这一个:

// here you can use $_SESSION['uid']

Saludos;)