469,613 Members | 1,195 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,613 developers. It's quick & easy.

Retrieve Array Data through List/Menu box using Mysql query

Hi ALL

I have entered some array values using checkboxes into mysql database through a form. Next iam creating a searchpage where all those cateogories inserted through checkboxes has to be retrieved using list/menu box.
When i check only a single checkbox to insert the checked category ,selecting that category through list box gives out the entire data of the user corresponding to that category.
However when i check multiple checkboxes and in the category select all those checked values to find if any user fits in all those category and hence prompt the entire data corresponding to it,the column shows only 'array' and not array values. So please anyone guide me as how should i fetch all the checked categories through the list/menu box.
Here goes the edited code:


Code to insert the array values in mysql database using checkboxes
[PHP]
<?
$state=$_POST['state'];
$state = implode(",", $state);

$query="insert into consult (state)VALUES('$state')";
mysql_query($query);
?>
<form name="form" action="abc.php" method ="post">
<input name=state[0] type="checkbox" value="Air Conditioning Consultants & Contractors">Air Conditioning Consultants & Contractors
<input name=state[1] type="checkbox" value="Arbitrators" >Arbitrators
<input name=state[2] type="checkbox" value=Builders,Contractors & Developers>Builders,Contractors & Developers
<input name=state[3] type="checkbox" value=Civil Engineers and Contactors>
Civil Engineers and Contactors
</form>
<input name="submit" type="submit" class="style40" value="Submit">

[/PHP]

Code to fetch those checked array values using list/menu box.
[PHP]
<?php
if (isset($_POST['submit']))

{
$state=$_POST['state'];
$state=explode(",",$state);
}
?>
<form name="form1" action="cons_show.php" method="post" >
<select name="state">
<option selected >Select</option>
<option value="Air Conditioning Consultants & Contractors">Air Conditioning Consultants & Contractors</option>
<option value="Arbitrators">Arbitrators</option>
<option value="Builders,Contractors & Developers">Builders,Contractors & Developers</option>
<option value="Civil Engineers and Contractors">Civil Engineers and Contractors</option>
</form>

if(!isset($cmd)) {

$state=$_POST['state'];

$query = @mysql_query("SELECT * FROM consult where state='$state'");

if (!$query)
{
exit('<p>Error retrieving entries from database!<br/>'.'Error: ' . mysql_error() . '</p>');
}

$sql = mysql_fetch_array($query);
if($sql){

echo '<table width="90%" cellspacing="2" cellpadding="4" border="1"><tr><td valign="top">

<table><tr><td ><h4 ><u> Customer Information</u> </h4></td></tr></table>';

echo '<table cellpadding="4"><tr><td ><b>Firm Name</b></td><td ><b>State</b></td><td><b>City</b></td>

<td><b>Contact</b></td><td><b>Email Address</b></td></tr>';

$firm = $sql['firmname'];

$address = $sql['adress'];

$city= $sql['city'];

$pin=$sql['pin'];

$tel=$sql['tel'];

$fax=$sql['fax'];

$email=$sql['email'];

$state=$sql['state'];

echo "<tr><td><a href='cons_details.php?cmd=edit&STATE=$state'>$fir m</a></td><td>$state</td><td>$city</td><td>$tel</td><td>$email</td>";

echo "<td>";
echo "</tr></table>";

}
else {
echo '<table ><tr><td>';


[/PHP]

This is how i have worked.but it is only useful for only a single checked data and cease to work on multiple checked data.
So if anyone ca help please do it.

Thanks and Regards
TechnoAtif
Feb 23 '08 #1
4 6187
ronverdonk
4,258 Expert 4TB
Where is variable '$cmd' set or assigned a value? Not knowing that makes it hard to follow your code. Statement
[php]$query="insert into consult (state)VALUES('$state')";[/php]
makes field state a string of states separated by comma's.

Somewhere in the 2nd piece of code you make $state an array. [php] $state=explode(",",$state);[/php]But you cannot compare a single value field state with an array $state.

So I suspect we have not seen al your code. Please show it and this time within the appropriate code tags!

Ronald
Feb 24 '08 #2
Hi

Here is the page where cmd value has been posted.Basically it shows the checked values from the chexkboxes...but does not shows the selected values from the select box as i navigate to this page through the link given in the previous page. Hopt this could help you to give me sm able solution

[PHP]<?php include "dbconnect.php";


if($_GET["cmd"]=="edit" || $_POST["cmd"]=="edit")
{
if (!isset($_POST["submit"]))
{
$city = $_GET["CITY"];

$sql = "SELECT * FROM consult where state'$state' ";
$result = mysql_query($sql);
$query = mysql_fetch_array($result);

?> [/PHP]

[HTML]<form name="form" action="cons_details.php" >

<table width="86%">
<tr><td colspan="4" class="style38"><b>Status (Tick as many as applicable):</b></td>
</tr>
<tr><td><?php echo "$query[state]<br/>" ; ?></td>
</tr> </table>
</form>[/HTML]

Thanks and Regards
Mohammad Atif
Feb 25 '08 #3
ronverdonk
4,258 Expert 4TB
You still have not shown all code. E.g. the next piece of code is odd. Upon entry you get data from field 'state' in the $_POST array and 10 lines down you show a form asking for checkboxes that fille the 'state' array. This cannot work like you have shown it.[php]<?
$state=$_POST['state'];
$state = implode(",", $state);

$query="insert into consult (state)VALUES('$state')";
mysql_query($query);
?>
<form name="form" action="abc.php" method ="post">
<input name=state[0] type="checkbox" value="Air Conditioning Consultants & Contractors">Air Conditioning Consultants & Contractors
<input name=state[1] type="checkbox" value="Arbitrators" >Arbitrators
<input name=state[2] type="checkbox" value=Builders,Contractors & Developers>Builders,Contractors & Developers
<input name=state[3] type="checkbox" value=Civil Engineers and Contactors>
Civil Engineers and Contactors
</form>
<input name="submit" type="submit" class="style40" value="Submit">
[/php]I am sure there is a mixup of all those various pieces of code and it cannot be seen how they are working together. So show us all; the code if you want a sensible answer.

You have not replied to my remark about using the state array as a single value.

Ronald
Feb 25 '08 #4
Ok..Sorry..

Coding for the first two pages are same ,next i'm giving the coding for next two pages:

In this third page the data should be shown on the click of the submit buttton of

the previous page as written above:

Third page:
===========
[PHP]<?php

include "dbconnect.php";
//error_reporting('notice');

if(!isset($cmd)) {


$state=$_POST['state'];


$query = @mysql_query("SELECT * FROM consult where state='$state'");

if (!$query)
{
exit('<p>Error retrieving entries from database!<br/>'.'Error: ' . mysql_error() . '</p>');
}

$sql = mysql_fetch_array($query);

if($sql){

echo '<table width="90%" cellspacing="2" cellpadding="4" border="1"><tr><td valign="top">

<table><tr><td ><h4 ><u> Customer Information</u> </h4></td></tr></table>';

echo '<table cellpadding="4"><tr><td ><b>Firm Name</b></td><td ><b>Category</b></td><td><b>City</b></td>

<td><b>Contact</b></td><td><b>Email Address</b></td></tr>';


//while ($sql) {



$cid = $sql['cid'];

$firm = $sql['firmname'];

$address = $sql['adress'];

$city= $sql['city'];

$pin=$sql['pin'];

$tel=$sql['tel'];

$fax=$sql['fax'];

$email=$sql['email'];

$pass=$sql['pass'];

$state=$sql['state'];

$prj1=$sql['prj1'];

$prj2=$sql['prj2'];

$prj3=$sql['prj3'];

$cprj1=$sql['cprj1'];

$cprj2=$sql['cprj2'];

$cprj3=$sql['cprj3'];

$award=$sql['award'];

$keyperson=$sql['keperson'];

$des=$sql['design'];

$time=$sql['time'];

echo "<tr><td><a href='cons_details.php?cmd=edit&STATE=$state'>$fir m</a></td><td>$state</td><td>$city</td><td>$tel</td><td>$email</td>";

echo "<td>";



echo "</tr></table>";

}
else {
echo '<table ><tr><td>';

echo "<font class=font>

<pre>

The database corresponding to your choice of city is

currently unavailable.Please try after some time to

get the better results.

Thanks</font>

</pre>";

echo '</td></tr></table>';
}

}

?>[/PHP]

================================================== ====

This last page will show the data of a particular firm after clicking on the link below the firmname:



[PHP]<?php include "dbconnect.php";
error_reporting('notice');

if($_GET["cmd"]=="edit" || $_POST["cmd"]=="edit")
{
if (!isset($_POST["submit"]))
{
$state = $_GET["state"];

$sql = "SELECT * FROM consult where state='$state' ";
$result = mysql_query($sql);
$query = mysql_fetch_array($result);
//redirect("protoshow.php");
//exit();
?> [/PHP]
[HTML]<form name="form" action="cons_details.php" method="post">
<h3><center class="style78">FOR ASSOCIATES/CONSULTANTS
</center></h3>
<table width="400" >
<!--<tr>
<td width="22%" class="style40">< ?php echo $query['cid'];?></td>
</tr>
-->
<tr>
<td width="45%" class="style38">
<b>Name of Firm:</b>
</td>
<td width="11%" class="style40"><?php echo $query['firmname'];?></td>
</tr>
<tr>
<td class="style38"><strong>Address</strong></td>
<td class="style40"><?php echo $query['adress'];?></td>
</tr>

<tr>
<td class="style38"><strong>City:</strong></td>
<td class="style40"><?php echo $query['city'];?></td>
<td width="27%" class="style38"><strong>Pin: </strong></td>
<td width="17%" class="style40"><?php echo $query['pin'];?></td>
</tr>
<tr>
<td class="style38"><strong>Tel:</strong></td>
<td class="style40"><?php echo $query['tel'];?></td>
<td class="style38"><strong>Fax: </strong></td>
<td class="style40"><?php echo $query['fax'];?></td>
</tr>
<tr><td class="style38"><strong>E-mail:</strong></td>
<td class="style40"><?php echo $query['email'];?></td>
</tr>
</table>
<table width="86%">
<tr><td colspan="4" class="style38"><b>Status (Tick as many as applicable):</b></td>
</tr>
<tr><td><?php echo "$query[state]<br/>" ; ?></td>
</tr> </table>

</form>[/HTML]
[PHP]<?php }} ?>[/PHP]

Hope this gives u some clue

Somewhere in the 2nd piece of code you make $state an array.
Code: ( php )
$state=explode(",",$state);
But you cannot compare a single value field state with an array $state.
I cannot get you...can u please elaborate
Feb 26 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

9 posts views Thread by Philip D Heady | last post: by
4 posts views Thread by JaNE | last post: by
reply views Thread by Erik Summers | last post: by
reply views Thread by Donald Tyler | last post: by
16 posts views Thread by Daniel Tonks | last post: by
12 posts views Thread by Martien van Wanrooij | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.