I have a piece of code:

 // trainer query
  $query = "INSERT INTO ".TBL_USERS." SET username = :username, password = :password, alliance = :alliance, avatar = :avatar, gender = :gender,  usersalt = :usersalt, userid = 0, userlevel = $ulevel, email = :email, timestamp = $time, actkey = :token, ip = '$userip', regdate = $time"; 
  $stmt = $this->connection->prepare($query);
  return $stmt->execute(array(':username' => $username, ':password' => $password, ':usersalt' => $usersalt, ':alliance' => $alliance, ':avatar' => $avatar, ':gender' => $gender, ':email' => $email, ':token' => $token));

  // pokemon query
   $query2 = "INSERT INTO ".TBL_POKEMON." SET Trainer = :trainer, OTrainer = :otrainer, Pokemon = :pokemon, shiny = :shiny, Level = :level, Nlevel = :nlevel, EXP = :exp, EXPN = :expn, HP = :hp, mhp = :mhp, Gender = :gender, Item = :item, Attack1 = :attack1, Attack2 = :attack2, Attack3 = :attack3, Attack4 = :attack4"; 
  $stmt2 = $this->connection->prepare($query2);
  return $stmt2->execute(array(':trainer' => $username, ':otrainer' => $username, ':pokemon' => $starter, ':shiny' => $shiny, ':level' => 10, ':nlevel' => 11, ':exp' => 1001, ':expn' => 1331, ':hp' => $hp2, ':mhp' => $hp2, ':gender' => $gender, ':item' => None, ':attack1' => $attack1, ':attack2' => $attack2, ':attack3' => $attack3, ':attack4' => $attack4));

Now I want both queries to be submitted into the database however, only the trainer query seems to be working. If I remove the trainer query and leave the pokemon query, the pokemon query works. But if I use them both together, they don't seem to be working... am I doing something wrong?

Edit: When I used them both together, I used stmt2 and query2, and I even tried both of them as stmt and query.. so I'm really stumped.

Recommended Answers

All 2 Replies

Member Avatar for LastMitch

Now I want both queries to be submitted into the database however, only the trainer query seems to be working. If I remove the trainer query and leave the pokemon query, the pokemon query works. But if I use them both together, they don't seem to be working... am I doing something wrong?

For the pokemon query you INSERT $username and $hp2 twice at the same time.

Try to rename both of them and INSERT into a different column. Instead of $username try $username1 & $username2 and for $htp2 try $htp1 and $htp2

So your pokemon query should look like this:

// pokemon query
$query2 = "INSERT INTO ".TBL_POKEMON." SET Trainer = :trainer, OTrainer = :otrainer, Pokemon = :pokemon, shiny = :shiny, Level = :level, Nlevel = :nlevel, EXP = :exp, EXPN = :expn, HP = :hp, mhp = :mhp, Gender = :gender, Item = :item, Attack1 = :attack1, Attack2 = :attack2, Attack3 = :attack3, Attack4 = :attack4";
$stmt2 = $this->connection->prepare($query2);
return $stmt2->execute(array(':trainer' => $username1, ':otrainer' => $username2, ':pokemon' => $starter, ':shiny' => $shiny, ':level' => 10, ':nlevel' => 11, ':exp' => 1001, ':expn' => 1331, ':hp' => $hp1, ':mhp' => $hp2, ':gender' => $gender, ':item' => None, ':attack1' => $attack1, ':attack2' => $attack2, ':attack3' => $attack3, ':attack4' => $attack4));

This should work. I don't see anything interfering.

To be honest that is a very bad code using PDO, why the table names are constants ? . Why that isn’t a data worker with sql statements and functions to work with them ? Why , because you use the same PDO flow don’t you have a PDO child DB class ? . Why don’t you clarify the values with prepared statements marks '?' ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.