Array and select and query

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
maceved23
Forum Newbie
Posts: 17
Joined: Sun Jul 13, 2003 11:56 pm

Array and select and query

Post by maceved23 »

Hi, 8)
I'm having a problem and I don't know how to solve it.

I have two files one is a form and the other a verification.
My users can select in the list/menu multiple items, and I need to make the query only to this selectiom.

my form code is:

Code: Select all

<?
	
	include("config.php");
	viewheader();
	
				echo "<FORM ACTION='conf_sector.php' METHOD='GET' NAME='busca_sector'>";
                echo "<TABLE CELLSPACING='0' CELLPADDING='0' BORDER='0' WIDTH='375' BGCOLOR='#99B9D0'>";

                echo "  <TBODY>";
                echo "    <TR> ";
                echo "     <TD><IMG SRC='cp-bcorn.gif' WIDTH='13' HEIGHT='12'></TD>";
                echo "      <TD WIDTH='100%'></TD>";
                echo "      <TD ALIGN='right'><IMG SRC='cp-bcorn2.gif' WIDTH='13' HEIGHT='12'></TD>";
                echo "    </TR>";
                echo "    <TR> ";
                echo "      <TD COLSPAN='3' ALIGN='CENTER'><TABLE WIDTH='100%' CELLPADDING='2' CELLSPACING='1'>";
                          
                echo "          <TR> ";
                echo "            <TD VALIGN='MIDDLE' ALIGN='RIGHT' WIDTH='75'><P>Sector*:</P></TD>";
                echo "            <TD VALIGN='MIDDLE' ALIGN='LEFT'> 
                				  <SELECT MULTIPLE NAME='busca_sector'>
            					  <option value='ONE'>ONE</option>
            					  <option value='TWO'>TWO</option>            
          						  </SELECT></TD>";
                echo "          </TR>";
                echo "          </TR>";
                echo "        </TABLE>";
                     
                echo "          <INPUT name='SUBMIT' TYPE='SUBMIT' CLASS='button' VALUE='SEND'>";
                echo "          &nbsp;"; 
                echo "    </TD>";
                echo "    </TR>";
                echo "  </TBODY>";
                echo "</TABLE>";
              	echo "	</FORM>";

?>
now here is the verification code

Code: Select all

<?php 

include 'config.php'; 
db_connect(); 
viewheader(); 

$sql = "SELECT * FROM `$dbtable` WHERE `sector_emp`  LIKE '%{$_GET['busca_sector']}%' "; 
$res = mysql_query($sql) or die(mysql_error().'<p>'.$sql.'</p>'); 

if (mysql_num_rows($res) < 1) { 

echo '<p style="margin: 30 0 10 0;">EN ESTE SECTOR NO HAY USUARIOS REGISTRADOS!!!!</p>'; 
echo '<h4><a href="javascript:history.back()">Regresar</a></h4>'; 

} else { 

?> 
<h4>USER DATA</h4> 

<table width="80%" border="0" cellpadding="1" cellspacing="0" class="textfield2"> 
<tr bgcolor="#F6F6F6"> 
   <td valign="top" align="right"> 
      - <a href='menu.php'>REGRESAR</a> -  - <a href='logout.php'>SALIR</a> - 
   </td> 
</tr> 
<tr> 
   <td valign="top"> 
      <table width="100%" border="0" cellspacing="1" cellpadding="3" align="center"> 
      <tr bgcolor="#F1F1F1"> 
         <td valign="top" colspan="5"> </td> 
      </tr> 
      <tr bgcolor="#F6F6F6"> 
         <th style="text-align: left;">NAME</th> 
         <th style="text-align: left;">Email</th> 
         <th style="text-align: left;">CONSULT</th> 
         <th>Mail</th> 
         <th>Seguimiento</th> 
      </tr> 
<?php 
   while ($row = mysql_fetch_assoc($res)) { 
?> 
      <tr bgcolor="<?php echo $bgColor; ?>"> 
         <td valign="top"> 
            <b><?php echo $row['nombre']; ?> <?php echo $row['apellido1']; ?> <?php echo $row['apellido2']; ?></b> 
         </td> 
 </tr> 
<?php 
    
   } // end while 
    
?> 
      </table> 
   </td> 
</tr> 
<tr bgcolor="#F6F6F6"> 
   <td valign="top" align="right"> 
    - <a href='menu.php'>RETURN</a> -  - <a href='logout.php'>LOG OUT</a> - 
   </td> 
</tr> 
</table> 
<?php 
} // end if 
?>
I want to query my db only for the selected items (2 and more) in my form. The code is working for ony value at a time.

I really apreciate the help, 'cause it's the final step of a project.

Thanks Mario :D
[mod_edit:

Code: Select all

tags added, rewrote CapsLock-text, if I added new typos: so sorry ][/size]
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

compare the behaviour of these two forms

Code: Select all

<html>
	<body>
		<pre><?php print_r($_GET); ?></pre>
		<fieldset>
			<legend>name of select is "busca_sector"</legend>
			<form method="GET" action="<?php echo $_SERVER['PHP_SELF']; ?>" >
				<select multiple name="busca_sector">
					<option value="ONE">ONE</option>
					<option value="TWO">TWO</option>           
				</select>
				<input type="submit" />
			</form>
		</fieldset>
		<fieldset>
			<legend>name of select is "busca_sector<b>[]</b>"</legend>
			<form method="GET" action="<?php echo $_SERVER['PHP_SELF']; ?>" >
				<select multiple name="busca_sector[]">
					<option value="ONE">ONE</option>
					<option value="TWO">TWO</option>           
				</select>
				<input type="submit" />
			</form>
		</fieldset>
	</body>
</html>
they only differ in the name of the select-element. The second has [] append to busca_sector making $_GET['busca_sector'] an array containing all checked values.
$sql = "SELECT * FROM `$dbtable` WHERE `sector_emp` LIKE '%{$_GET['busca_sector']}%' ";
If the values of the option-elements exactly match the db-field value you can use

Code: Select all

// escaping special characters of each element of $_GET['busca_sector'], we don't want bogus requests to succeed
$matches = array_map('mysql_escape_string', $_GET['busca_sector']);
// creating string suitable for sql's IN (...)
$matches = "'" . join("','", $matches) . "'";
$sql = "SELECT * FROM `$dbtable` WHERE `sector_emp` IN ($matches)";
but only if there is at least one entry in $_GET['busca_sector']. Otherwise $matches will be '' (quoted empty string).
Post Reply