Jump to content

Recommended Posts

Just looking for insight on the most efficient way to take data in a PHP $_POST array and use it to retrieve further data from a MySQL database.

 

For example, I have a form submitted and on the next page, I end with an array of 10 values, called $_POST['players'].

 

The array looks like this, where each value is simply the ID of each player (for instance, ID 37 is Chris Johnson, ID 10 is Tony Romo, etc).

 

Array ( [0] => 37 [1] => 10 [2] => 39 [3] => 60 [4] => 161 [5] => 217 [6] => 124 [7] => 69 [8] => 136 [9] => 147 ) 

 

I just want to know the best way to take that array and use the ID #s (and only those ID #s) to get the full data set for each player from MySQL (Let's just say I have a single table called "players" that has fields of playerid, name, team, position).

 

I would like a final output of data similar to this...

 

Chris Johnson, Tennessee Titans, RB

Tony Romo, Dallas Cowboys, QB

and so on.

 

I'm very shaky with arrays and I'm sure it's cake to cycle through those specific ten IDs, but I can't figure it out.

 

I THINK I'd have a MySQL statement that essentially says the following...

 

SELECT name, team, position
FROM players
WHERE playerid = (and this is where I am stuck since I don't know how to get those 10 IDs here)

 

Cool, thank you.  I just started doing it and I'm getting an "invalid argument passed" error when I try to implode the $_POST['players'] array...

 

$playerids = implode(",",$_POST['players']);

 

Should that work, or is a $_POST array, not a true array (like maybe I have to do something to the posted array first???)

 

If $_POST['players'] is the array that you indicated in the first post, that would work.

 

Have you confirmed what $_POST['players'] actually contains? Is this code inside your form processing code that has detected that the form has been submitted?

Does anyone know if the IN () comparison and/or implode automatically eliminate duplicates?  I was going to use array_unique to take out any duplicates in my array, but I tested first and the duplicates were removed without even needing array_unique.

 

Just curious.

The IN() comparison will match the row(s) where the comparison is TRUE. WHERE playerid IN(1,1) is the same as WHERE playerid = 1 OR playerid = 1 and it only returns the one single row with playerid = 1.

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.