简体   繁体   中英

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. The script goes though ok and say its done but does not do the 2 updates. 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

} 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. In your if statement, you have 5 instances of { and 6 instances of } . 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

Maybe your DB structure needs refining too. While inside foreach twice in the same trade isn't very efficient. Based on your description I would use the following tables:

  • user : this is for granted...
  • pokemon_gen : general information about different models.
  • pokemon_unique : every item should have a 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).
  • trade : data and info about a single trade. Which pokemon was traded from who to whom and when. FK to pokemon_unique is needed for that.

You should only need arrays to identify the unique pokemons and their current and future owners. (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).

Maybe I missed something. Hopefully not.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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