View Full Version : problem reciving check box values using post method


tony007
02-19-2006, 10:36 PM
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:

<form method="POST" action="./playlist.php" name="mp3Play">
...
...
...


playlist.php code


<?php

//echo $_POST['id'];

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


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].' ';
}}
$query = "select filename,title from wimpy where $where";

echo $query;

$result = mysql_query($query);

Kodo
02-20-2006, 02:48 AM
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
02-20-2006, 09:28 AM
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:

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

<?php



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

$query = "select * from files where id IN($_POST['Id'])"; ===>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%">&nbsp;</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>

<?
while($row = mysql_fetch_assoc($result))
{
//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>&nbsp;</td>
<td width="20%"><?=strval( $row['album'] );?>&nbsp;</td>
<td width="10%">D&nbsp;</td>
<td width="5%">33&nbsp;</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;

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

Kodo
02-20-2006, 11:37 AM
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
02-20-2006, 02:59 PM
PHP isn't my language so I'm taking a pot shot at the syntax...

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


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


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

Kodo
02-20-2006, 04:23 PM
ok.. I had the syntax really close.. try this
$query = "SELECT * fROM files WHERE id IN(".$_POST['id'].")";