Jump to content

[SOLVED] Problems with query results


cleibesouza

Recommended Posts

Hi all.

I have a form with a series of checkboxes which when checked and submitted runs a query based on record IDs. Because I don't know how many are going to get checked and submitted I run a loop:

 
for($i = 0; $i < $totalSubmitted; $i++){
   if(strlen($_POST[check . $i])){
      //processing goes here
   }//end if
}//end for

 

This is all working fine. But to fetch the data I tried creating arrays:

The entire code is below:

for($i = 0; $i < $totalSubmitted; $i++){
      if(strlen($_POST[check . $i])){
      $and = " AND sesion_pk = " . $_POST[check . $i];
      $results[$i]  = mssql_query($someQuery);
      $row[$i]  = mssql_fetch_array($results[$i]);
	echo $someQuery . $and .  "<br>"; // this is just for debugging purposes
	echo  $row[$i]['session_name'] . "<br>";
}else{
	$totalItems = 0;
}//end if/else	
}//end for		

 

The problem is that when I echo $row[$i]['session_name'] I only get the first ID passed.

 

Any idea of what I'm missing?

 

Thanks.

 

 

Link to comment
https://forums.phpfreaks.com/topic/113767-solved-problems-with-query-results/
Share on other sites

Give your checkboxes all the same name, ending with "[]" so they are posted as an array

 

while ($row = mysql_fetch_assoc($result)) {
    echo "<input type='checkbox' name='id[]' value='{$row['id']}' />
}

 

Only checked ones are posted so to retrieve those selected

 

$idlist = join(',' , $_POST['id']);
$sql = "SELECT x,y,z FROM mytable WHERE id IN ($idlist)";

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.