problem reciving check box values using post method (1 Viewer)

tony007

Registered User.
Local time
Today, 09:17
Joined
Jun 30, 2005
Messages
53
Hi all i got a form with many checkboxes and i want to pass checkbox varaibles to a new page using post method but the problem is that each time the first variable get pasted .Beleow u see part of php code that recives the passed variables. When i run this script by selecting 3 check boxes which has values :1 first check box, 2 second checkbox and 3 3th checbox. i get this

select filename,title from wimpy where id=1

instead of

select filename,title from wimpy where id=1 OR id=2 OR id=3

I be happy if an expert help me fix this problem and be able to pass all values of check box to second page and be able to buil query such :
select filename,title from wimpy where id=1 OR id=2 OR id=3
Thanks

part of check box form code:
Code:
<form method="POST" action="./playlist.php"  name="mp3Play">
...
...
...
playlist.php code
Code:
<?php

//echo $_POST['id']; 

$user = "root";
$pw = "";
$db = "mp3sversion5";
$mysql_access = mysql_connect("localhost", $user, $pw);
mysql_select_db($db, $mysql_access);


[B]if (isset($_POST['Id'])) {
    $fragments = explode(',',$_POST['Id']);
    $loopCount = sizeof($fragments);
    for ($loop = 0; $loop < $loopCount; $loop++) {
        if ($where != '') $where .= ' OR';
        $where .= ' id='.$fragments[$loop].' ';
    }[/B]}
$query = "select filename,title from wimpy where $where";

[B]echo $query;[/B]

[B]$result = mysql_query($query);[/B]
 

Kodo

"The Shoe"
Local time
Today, 12:17
Joined
Jan 20, 2004
Messages
707
if the checkboxes have the same name ("POSTID") then they are posted as a concatenated string with comma's as the delimeter, like so. 1,3,6,29,93 and so on and so on. If MySQL supports the "IN" keyword (and I'm sure it does), you won't need to do that, just do:
$query = "select filename,title from wimpy where id IN($_POST['Id'])";
 

tony007

Registered User.
Local time
Today, 09:17
Joined
Jun 30, 2005
Messages
53
Kodo said:
if the checkboxes have the same name ("POSTID") then they are posted as a concatenated string with comma's as the delimeter, like so. 1,3,6,29,93 and so on and so on. If MySQL supports the "IN" keyword (and I'm sure it does), you won't need to do that, just do:
$query = "select filename,title from wimpy where id IN($_POST['Id'])";

Many thanks for u reply i tried your suggestion and i got the following error:

Code:
Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in file.php on line 19

I be happy if u help me fix this problem.Thanks

Code:
<?php



$user = "root";
$pw = "";
$db = "test2";
$mysql_access = mysql_connect("localhost", $user, $pw);
mysql_select_db($db, $mysql_access);

[B]$query = "select * from files where id IN($_POST['Id'])";[/B] ===>line 19

echo $query;

 $result = mysql_query($query) or die("Error with MySQL Statement! ".mysql_error()."<br>\nThe query was ".$query);

?>

<div align="center">
  <center>
  <table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="70%" id="AutoNumber1">

	<form action="/Members/AddToPlayList.php" method=post >
	<input name="go" type="hidden" value="yes">
    <tr>
      <td width="20%" height="23">Add to Playlist:
      </td>
      <td width="50%" height="23">
      	<select size="1" name="List">
      		
      		<option value="2187">wow</option>
      		
      	</select>
      </td>
    </tr>
    
    <tr>
      <td colspan=2 height="1">
      		<table border="1">
 				 <tr>
    				<td width="3%"> </td>
   					<td width="50%"><b>Song Name</b></td>
    				<td width="20%"><b>Album</b></td>
    				<td width="10%"><b>Language</b></td>
    				<td width="5%"><b>Hits</b></td>
  				</tr>

<?
[B]while($row = mysql_fetch_assoc($result))
{[/B]
    //echo "<br>path : {$row['filename']} <br>" .
      //   "title :{$row['title']} <br>";


?>
 <tr>
    				<td width="3%"><input type="checkbox" name="id" value="<?=strval( $row['id'] );?>" checked></td>
   					<td width="50%"><?=$title ;?></b> </td>
    				<td width="20%"><?=strval( $row['album'] );?> </td>
    				<td width="10%">D </td>
    				<td width="5%">33 </td>
  				</tr>

<?
  

}



?>



	  
      		</table>
      </td>
    </tr>
    
    
    <tr>
      <td colspan=2 height="27">
      <p align="center">
      <input type="submit" value="Add this/these Songs to my PlayList" name="B1">
      </td>
    </tr>
    </form>
  </table>
  </center>
</div>
</body>

checkbox code;

Code:
<input
                        type="checkbox" name="Id[]" value='<?=intval( $row['id'] );?>' /></td>
 

Kodo

"The Shoe"
Local time
Today, 12:17
Joined
Jan 20, 2004
Messages
707
PHP isn't my language so I'm taking a pot shot at the syntax...

$query = "select * from files where id IN(" + $_POST['Id']+ ")";
 

tony007

Registered User.
Local time
Today, 09:17
Joined
Jun 30, 2005
Messages
53
Kodo said:
PHP isn't my language so I'm taking a pot shot at the syntax...

$query = "select * from files where id IN(" + $_POST['Id']+ ")";


Code:
Fatal error: Unsupported operand types in file.php on line 18


and pointing to the same line where the query is !!:-(
 

Kodo

"The Shoe"
Local time
Today, 12:17
Joined
Jan 20, 2004
Messages
707
ok.. I had the syntax really close.. try this
$query = "SELECT * fROM files WHERE id IN(".$_POST['id'].")";
 

Users who are viewing this thread

Top Bottom