Jump to content

Display Array from $_POST


hoopplaya4

Recommended Posts

Hi All,

 

Using PHP & MySQL.

 

I've been trying to work out the logic on this one for a while, but I just can't figure it out.  But, I think I'm close!!

 

I have several First and Last names in a form, which are in an Option field that is being pulled from the database.  Users have the ability to select multiple options at once.  As a result, these are being $_POST'd as an array.

 

Once the form is submitted, I'd like to echo back all the First & Last names that were posted.  Right now, it only displays one name.

 

Here's the Form:

 

<form name="filterplayer" method="post" action="<?= $_SERVER['PHP_SELF'] ?>?show=2">
    <select multiple name="players[]" id="players" size="5">
     <?php

      $sql = "SELECT usrID, usrFirst, usrLast, usrPosition";
      $sql .= " FROM tblUsers";
      $sql .= " WHERE usrActive = 1";
      $sql .= " ORDER BY usrLast";

	require("../connection.php");

   		$rs=mysql_db_query($DBname,$sql,$link);

    	if ($rs) {

     	 while ($row=mysql_fetch_array($rs)){

          print("<option value='" . $row["usrID"] . "'>");
  		  print($row["usrFirst"] . " " . $row["usrLast"] );
  		  print(" (" . $row["usrPosition"] . ")</option>\n");
  		  } // end while

   	     }
      	else {  // No Events found

 	 print("No Active players");

     mysql_close($link);  } // end else ($rs)

    ?>
     </select><br>
    <input type="submit" value="Show Availability" />
   </form>

 

 

And here is the PHP to Display those options that were Posted:

 

<?php
$pname = $_POST['players'];  // Get The Form Values
$new =implode(' and ',$pname);                            //////   IMPLODE VALUES    ////////

$sql = "SELECT usrID, usrFirst, usrLast";
      $sql .= " FROM tblUsers";
      $sql .= " WHERE usrID = $new";


	require("../connection.php");
   		$rs=mysql_db_query($DBname,$sql,$link);
if ($rs) {

  while ($row=mysql_fetch_array($rs)){

$first = $row['usrFirst'];
$last = $row['usrLast'];

$full = $first . " " . $last;

print ("<div>");
print $full;
print ("</div>");
   } //end while
   
} // end if

?>  

 

And all that it prints is: (although I'm selecting multiple values).

 

John Smith

 

 

Link to comment
Share on other sites

You are missing the close bracket to the foreach:

 

foreach($_POST['players'] as $pz){

$sql = "SELECT usrID, usrFirst, usrLast";
      $sql .= " FROM tblUsers";
      $sql .= " WHERE usrID = $pz";
       }
      require("../connection.php");
         $rs=mysql_db_query($DBname,$sql,$link);
if ($rs) {

  while ($row=mysql_fetch_array($rs)){

$first = $row['usrFirst'];
$last = $row['usrLast'];
$full = $first . " " . $last;

print ("<div>");
print $full;
print ("</div>");
   } //end while
   
} // end if
} // end foreach
?>

 

There should have been a syntax error thrown on that. but yea. Hopefully that is the issue.

Link to comment
Share on other sites

The problem is the SQL.

 

(assuming usrID is the primarykey)

SELECT * FROM `tblUsers` WHERE usrID = 1 and 2 and 3

That will only return 1 row.

 

Try

$pname = $_POST['players'];  // Get The Form Values
$new =implode(', ',$pname); 
$sql = "SELECT * FROM `tblUsers` WHERE usrID IN (" . $new . ")";

Link to comment
Share on other sites

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.