Jump to content

Pdo foreach only grabbing first result


geatzo

Recommended Posts

So im trying todo a mass update for each row with the same tid

 

 

$getUsers = $db->prepare("SELECT * FROM offer_pokemon WHERE tid=?"); 
$getUsers->execute([$tid]); 
$users = $getUsers->fetchAll(); 
foreach ($users as $user) { 
  
    
    print_r($user);

die;
  
        $sqlll5 = "INSERT INTO user_pokemon (uid,name,level,exp,move1,move2,move3,move4,type) VALUES (?, ?,?,?,?,?,?,?,?)";
    $qq5 = $db->prepare($sqlll5);
    $qq5->execute(array($_SESSION["userid"],$user['name'],$user['level'],$user['exp'],$user['move1'],$user['move2'],$user['move3'],$user['move4'],$user['type']));               
    
} 

for some reason it will grab the 1 result but not them both. Im trying to move the results from 1 table to another. print r is showing Array ( [id] => 1453 [0] => 1453 [tid] => 1011 [1] => 1011 [oid] => 1 [2] => 1 [uid] => 8 [3] => 8 [name] => Caterpie [4] => Caterpie [exp] => 500 [5] => 500 [level] => 5 [6] => 5 [move1] => Ember [7] => Ember [move2] => Ember [8] => Ember [move3] => Ember [9] => Ember [move4] => Ember [10] => Ember [type] => Normal [11] => Normal [idofpokemon] => 24332 [12] => 24332 )

 

a user can make a offer on a pokemon the offer id is the tid so this person has offered 2 pokemon so if the user accepts i have to insert these 2 pokemon into the users account then remove them from the offer table. I have coded futher down which gives the other person the pokemon ( they can offer on 1 pokemon ) so in this case  this person has offered 2 pokemon for 1 of the other users ive coded it so it gives the 1 to this users but can;t get the for each to work to give the 2 to the other user.

 

I have off course added the die to see what it prints out.

Screenshot_10.png

Link to comment
Share on other sites

First, question: why even do this at all?

INSERT INTO user_pokemon (uid, name, level, exp, move1, move2, move3, move4, type)
SELECT (user id here), name, level, exp, move1, move2, move3, move4, type
FROM offer_pokemon
WHERE tid = (id here)

Second, question: why have two separate tables for this? Don't you think it would be easier to have one table and then just mark each row as, like, "pending" or "confirmed"?

Link to comment
Share on other sites

43 minutes ago, requinix said:

First, question: why even do this at all?

INSERT INTO user_pokemon (uid, name, level, exp, move1, move2, move3, move4, type)
SELECT (user id here), name, level, exp, move1, move2, move3, move4, type
FROM offer_pokemon
WHERE tid = (id here)

Second, question: why have two separate tables for this? Don't you think it would be easier to have one table and then just mark each row as, like, "pending" or "confirmed"?

because a person add the pokemon up for trade then other users can make offers on that pokemon. So i thought it was best to have 1 table of whats up for trade and 1 table of the offers on them pokemon.

Link to comment
Share on other sites

But it's the same Pokemon though, isn't it? Someone offers one up for trade and someone else makes an offer on it.
Eh, whatever. It makes more sense (and it's easier) to have just the one table, but you can do whatever.

Third, the whole point of a prepared statement is that it's reusable. Preparing the same statement inside a loop completely defeats the purpose of that.
Create it once ahead of time, then execute it with the different values.
Not that you need to do this at all, of course, since that single INSERT...SELECT will do everything for you all at once.

Otherwise, I see no reason why it would only do one row. Maybe there's a key constraint getting in the way? Duplicate row?

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.