php - POST变量问题

标签 php mysql post

我目前正在处理的这段代码遇到了一个非常奇怪的问题。它是游戏的 map 编辑器,可将每个图 block 的变量发送到另一个 PHP 文件以更新 mySQL 数据库。

到目前为止, map 编辑器代码显示 map 并加载一切正常。如果直接在代码中给出变量, map 更新 (mupdate) PHP 文件会正确更新数据库。

但是,当我在文件之间将数据作为 POST 变量发送时,mupdate 文件在前 18 次左右完美接收它们,然后无法再读取。

谁能解释一下为什么会发生这种情况?

(提前为我的蹩脚编码和 map 编辑器加载整个 map 的荒谬加载时间道歉,我可能会在某个时候将其更改为 10x10 部分,但 POST var 问题仍然存在。 )

map 编辑器:

http://www.locktopia.netne.net/mapedit.php
<?php
//Database Connection
include('DBconnect.php');


//Map Defaults
$world="slums";
$mapInfo=mysql_fetch_assoc(
  mysql_query("SELECT * FROM `mapindex` WHERE `NAME` = '".$world."'")
);
$tileSet= $mapInfo['TILESET'];

//Loads Current Location
// Splits the Database Location into the co-ordinates.
$startX=$startY = 1;
list($maxX, $maxY) = split('[,]', $mapInfo['SIZE']);

//Listing tiles in directory
$tileDir = "images/tileSets/".$tileSet."/";

$tilecount = count(glob("" . $tileDir . "*.png"));
$objDir = "images/tileSets/objects/";

$Objcount = count(glob("" . $objDir . "*.png"))-1;

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <title>Map Editor</title>
</head>
<body>

//MAP EDITOR V2
<form action="mupdate.php" method="post">
<?php
  //Map and Co-Ord values
  echo('<input name="world" type="hidden" id="world" value="'.$mapInfo['NAME'].'" />');
  echo('<input name="size" type="hidden" id="size" value="'.$mapInfo['SIZE'].'" />');
?>
<table width="100%" border="1" cellpadding="0" cellspacing="0">
<?php
  echo($mapInfo['NAME']);
  echo($mapInfo['SIZE']);

  while($startY<=$maxY){
    echo("<tr>"); //BEGIN ROW
    while($startX<=$maxX){
      echo("<td>");//BEGIN CELL
      ///////////////////////CELL CONTENT///////////////////////

      //Tile Menu
      echo('Tile:<br /><select name="'.$startX.','.$startY.'-tile" id="'.$startX.','.$startY.'-tile" size="1" style="overflow:scroll;width:40px;height:20px;">');
      $possibleTile=1;
      while($possibleTile<=$tilecount){
        echo('<option value="'.$possibleTile.'" style="width:40px; height:40px; background-image: url('.$tileDir.$possibleTile.'.png);">'.$possibleTile.'</option>');
        $possibleTile++;
      }
      echo('</select><br />');

      echo('Obj LVL1:<br /><select name="'.$startX.','.$startY.'-ob1" id="'.$startX.','.$startY.'-ob1" size="1" style="overflow:scroll;width:40px;height:20px;">');
      $possibleObject=0;
      while($possibleObject<=$Objcount){
        echo('<option value="'.$possibleObject.'" style="width:40px; height:40px; background-image: url('.$objDir.$possibleObject.'.png);">'.$possibleObject.'</option>');
        $possibleObject++;
      }
      echo('</select><br />');

      echo('Obj LVL2:<br /><select name="'.$startX.','.$startY.'-ob2" id="'.$startX.','.$startY.'-ob2" size="1" style="overflow:scroll;width:40px; height:20px;">');
      $possibleObject=0;
      while($possibleObject<=$Objcount){
        echo('<option value="'.$possibleObject.'" style="width:40px; height:40px; background-image: url('.$objDir.$possibleObject.'.png);">'.$possibleObject.'</option>');
        $possibleObject++;
      }
      echo('</select>');

      echo('Buildable:<br /><select name="'.$startX.','.$startY.'-build" id="'.$startX.','.$startY.'-build" size="1" style="overflow:scroll;width:40px; height:20px;">');
      $possibleOption=0;
      while($possibleOption<=1){
        echo('<option value="'.$possibleOption.'">'.$possibleOption.'</option>');
        $possibleOption++;
      }
      echo('</select>');

      echo('Type:<br /><select name="'.$startX.','.$startY.'-type" id="'.$startX.','.$startY.'-type" size="1" style="overflow:scroll;width:40px; height:20px;">');
      echo('<option value="passable">Walkable</option>');
      echo('<option value="impassable">Blocked</option>');
      echo('<option value="teleport">Teleport</option>');
      echo('</select>');

      //////////////////////////////////////////////////////////
      echo("</td>");//END CELL
      $startX++;
    }
    echo("</tr>");//END ROW
    $startY++;
    $startX=1;
  }
?>
</table>
<input type="submit" name="submit" />
</form>
</body>
</html>

map 更新程序:[点击上面页面上的“提交”即可到达该页面)

 <?php
//Database Connection
include('common/connectDB.php');

//Printing the Data in the POST array
print_r($_POST);

// A Spacer for convenience's sake
echo('<br /><hr> <br />');

//Setting the Name of the map to update
$mapName=$_POST['world'];

//Getting the Max X/Y dimensions of the map (in tiles)
list($mapX, $mapY) = split('[,]', $_POST['size']);

//Setting the start points for the loops
$currX=1;
$currY=1;

//Row (Y-Axis) Update Loop
while($currY<=$mapY){

    //Column (X-Axis) Update Loop
    while($currX<=$mapX){

        //Checking for missing data on cell Type (Walkable/Blocking/Teleport)
        if(empty($_POST[$currX.','.$currY.'-type'])) {
             die("Failed on 'type': currX={$currX} - currY={$currY} - POST DATA: {$_POST[$currX.','.$currY.'-type']}");
        }

        //Checking for missing data on cell Tile (The base graphic, represented by a number which matches an image file)
        if(empty($_POST[$currX.','.$currY.'-tile'])) {
             die("Failed on 'tile': currX={$currX} - currY={$currY} - POST DATA: {$_POST[$currX.','.$currY.'-tile']}");
        }

        //mysql_query
        echo("UPDATE `maps` SET `TYPE`='".$_POST[$currX.','.$currY.'-type']."', `TILE` = '".$_POST[$currX.','.$currY.'-tile']."', `BUILD` = '".$_POST[$currX.','.$currY.'-build']."', `OBJECTS` = '".$_POST[$currX.','.$currY.'-ob1'].",".$_POST[$currX.','.$currY.'-ob2']."' WHERE  CONVERT( `maps`.`MAP` USING utf8 ) = '".$mapName."' AND CONVERT( `maps`.`XY` USING utf8 ) = '".$currX.",".$currY."'");

        //A spacer for Clarity's sake
        echo('<br />');

        //Updating the Column (X-Axis) Value
        $currX++;
    }
    echo("Row ".$currY." of ".$mapY." completed.<br>Sleeping 1 Second."); 

    //Resetting Column (X-Axis) to 1
    $currX=1;

    //Updating the Column (Y-Axis) Value
    $currY++;

    //A spacer for Clarity's sake
    echo('<br />');

    //Sleep for 1 Second. Stops the DB being overloaded with requests (as discovered with my map generator)
    //sleep(1); //uncommented in working version
}

//Closing the DB connection
mysql_close();
?>

澄清一下: 代码的输出在第 19 条条目之后无法包含 Tile、Type 和 Build 的值:

比较:

没有。 19 -

UPDATE `maps` SET `TYPE`='passable', `TILE` = '1', `BUILD` = '0', `OBJECTS` = '0,0' WHERE CONVERT( `maps`.`MAP` USING utf8 ) = 'slums' AND CONVERT( `maps`.`XY` USING utf8 ) = '19,1'

没有。 20 -

UPDATE `maps` SET `TYPE`='', `TILE` = '1', `BUILD` = '', `OBJECTS` = '0,0' WHERE CONVERT( `maps`.`MAP` USING utf8 ) = 'slums' AND CONVERT( `maps`.`XY` USING utf8 ) = '20,1'

没有。 21 -

UPDATE `maps` SET `TYPE`='', `TILE` = '', `BUILD` = '', `OBJECTS` = ',' WHERE CONVERT( `maps`.`MAP` USING utf8 ) = 'slums' AND CONVERT( `maps`.`XY` USING utf8 ) = '21,1'

附录:感谢 Phil 指出 mysql_close() 错误,我添加了您建议的代码,还添加了对另一个 var 的检查(我不能用它来检查构建,因为它使用单个 int为真或假)。 有趣的是,有一次 printr($_POST);给了我完整的数据量,但其余代码在第 20 号处失败,现在它只显示到第 20 号。 我想知道如果我在数组中创建发布值(即 $_POST[20,1][type])是否会有帮助

最佳答案

可能需要关注的事情:

  1. 脚本超时

  2. POST 大小。有限制,参见php.ini中的post_max_size

关于php - POST变量问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/985786/

相关文章:

javascript - 切换表格单元格内的数据显示

magento - 如何测试 Magento POST Controller 操作 - 使用(或不使用)EcomDev PHPUnit 模块

post - 带有 XMLHttpRequest 的 Https 请求

javascript - 如何让 chrome 自动填充异步帖子

php - 试图在 html 文本字段中显示 mysql 数据

php - 在线指纹认证

php - 保存 PHP 之前图像的唯一名称

mysql - 为什么模型没有在 Sequelize 中获取表中的所有属性

php - 使用 $searchEscaped 显示特定数据

php - 获取选项名称 php