简体   繁体   English

PHP while 循环出错

[英]PHP while-loop gone wrong

Ok so im trying to make a simple trade script i have been trying to find the problem for weeks now i have decided to ask for help.好的,所以我正在尝试制作一个简单的交易脚本,我已经尝试了数周的时间来寻找问题,现在我决定寻求帮助。 I select some monsters from the db and also the trade info eg who the trade is from and who its going to.我 select 来自数据库的一些怪物以及交易信息,例如交易来自谁以及交易去往谁。 The script goes though ok and say its done but does not do the 2 updates.脚本运行正常,并说它已完成,但不执行 2 次更新。 It ment to grab the monsters from the db then update there owners.从数据库中获取怪物然后更新所有者。 I have session start and the db connect at the top of the page be for anyone says that is the problem我有 session 开始,页面顶部的数据库连接是任何人都说这是问题所在

} else if ( $_POST['Submit'] == 'Complete' ) {

  //// This is the bit which does the update and does not work

  $TradeID = $_POST['id'];
  $sql12 = mysql_query( "SELECT * FROM Trades WHERE ID='$TradeID'" );
  $row12 = mysql_fetch_array( $sql12 ) or die( mysql_error() );

  $unserialize11 = unserialize( $row12['MyPokemon'] );

  foreach ( $unserialize11 as $poke222 ) {
    $sql2 = mysql_query( "SELECT * FROM user_pokemon WHERE id='$poke222'" );
    while ( $row2 = mysql_fetch_array( $sql2 ) ) {
      $Update1 = mysql_query( "UPDATE user_pokemon SET  belongsto='".$row12['Me']."' WHERE id='".$row2['ID']."'" );
    }
  }
  $unserialize12 = unserialize( $row12['OtherPokemon'] );
  foreach ( $unserialize12 as $poke122 ) {
    $sql3 = mysql_query( "SELECT * FROM user_pokemon WHERE id='$poke122'" );
    while ( $row3 = mysql_fetch_array( $sql3 ) ) {
      $Update1 = mysql_query("UPDATE user_pokemon SET   belongsto='".$row12['OtherPerson']."' WHERE id='".$row3['ID']."'" );
    }
  }

  echo "You have successfully completed trade #".$TradeID."!";
}
} else if ($_GET['action'] == 'delete'){

I have just snipped the bit of code which is not working it is grabbing the monsters fine but is just not doing the update i think maybe ive got the }} in the wrong place or maybe have to many???我刚刚剪下了一些不起作用的代码,它很好地抓住了怪物,但只是没有进行更新我想也许我把 }} 放在了错误的地方或者可能有很多???

Upon cleaning up your formatting, it does appear that you have an extra semicolon, locating at the end of the if statement presented here.清理您的格式后,您似乎有一个额外的分号,位于此处显示的if语句的末尾。 In your if statement, you have 5 instances of { and 6 instances of } .在您的if语句中,您有 5 个{实例和 6 个}实例。 Removing the last } may resolve your issue.删除最后一个}可能会解决您的问题。

Keeping your code nicely formatting will reduce troubleshooting issues in the future.保持代码的良好格式将减少将来的故障排除问题。 I've removed much of the body of this code block to reveal the problem-brace:我删除了这个代码块的大部分主体以揭示问题支撑:

if ( $_POST['Submit'] == 'Complete' ) {
  /* Removed variable assignments */
  /* Removed foreach & while */
  /* Removed variable assignment */
  /* Removed foreach & while */
  /* Removed output */
}
} /* This brace shouldn't be here */

How do you know your script goes throught OK?你怎么知道你的脚本通过了? You aren't checking for success anywhere.您不会在任何地方检查是否成功。 Your success message echoes always when the submit button is pressed.按下提交按钮时,您的成功消息总是回显。 Maybe you should check for success by checking affected rows.也许您应该通过检查受影响的行来检查是否成功。 http://php.net/manual/en/function.mysql-affected-rows.php http://php.net/manual/en/function.mysql-affected-rows.php

Maybe your DB structure needs refining too.也许您的数据库结构也需要改进。 While inside foreach twice in the same trade isn't very efficient. While在同一笔交易中两次内部foreach效率不高。 Based on your description I would use the following tables:根据您的描述,我将使用下表:

  • user : this is for granted... user :这是理所当然的……
  • pokemon_gen : general information about different models. pokemon_gen :关于不同模型的一般信息。
  • pokemon_unique : every item should have a unique ID. pokemon_unique :每个项目都应该有一个唯一的 ID。 This table contains FK to owner (user) and to pokemon model. Also info about last trade date, pokemon's condition (if this is a buy and sell database) etc. could be in here (or FK to another more detailed table).此表包含所有者(用户)和口袋妖怪 model 的 FK。还有关于最后交易日期、口袋妖怪状况(如果这是买卖数据库)等的信息可以在这里(或 FK 到另一个更详细的表格)。
  • trade : data and info about a single trade. trade :关于单笔交易的数据和信息。 Which pokemon was traded from who to whom and when.哪个神奇宝贝从谁那里交易到谁以及何时交易。 FK to pokemon_unique is needed for that.为此需要 FK 到pokemon_unique

You should only need arrays to identify the unique pokemons and their current and future owners.您应该只需要 arrays 来识别独特的口袋妖怪及其当前和未来的所有者。 (Doesn't one trade need only two owners: current and the new one?). (一笔交易不是只需要两个所有者:当前所有者和新所有者吗?)。 You just update the owner in the pokemon_unique table and create a row to the trade (one row for each pokemon).您只需更新pokemon_unique表中的所有者并为trade创建一行(每个 pokemon 一行)。

Maybe I missed something.也许我错过了什么。 Hopefully not.希望不会。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM