Jump to content

Display data from database using multiple checkbox selected form


Chimp17

Recommended Posts

Hello Friends,

 

wishing you all a very happy new year nd thnx 4 wishing me the same.

 

Here is my problem!!!

 

i am trying to display information on my webpage using some chekboxes with multple selection from mysql database, but just not able to get it. All i get is the last chekbox selected data. I have used array in checbox name attribute, nd tried foreach loop also, but nothing is working. So please help me out please please please..!!!!!!

Link to comment
Share on other sites

Here is my coding:
 

<form name="search" method="post" action="find.php">
<table width="900" border="1" class="srch">
<tr class="head"><td>States</td><td>Networks</td><td>Channels</td></tr>
<tr><td>
<input type="checkbox" value="Delhi" name="state">DELHI<br />
<input type="checkbox" value="Chandigarh" name="state">Chandigarh<br />
<input type="checkbox" value="Patna" name="state">Patna<br />
<input type="checkbox" value="Kolkata" name="state">Kolkata<br />
<input type="checkbox" value="Madras" name="state">Madras<br />
<input type="checkbox" value="Mumbai" name="state">Mumbai<br /></td>
<td>
</tr>
<tr><td colspan="3" align="Right"><input type="submit" name="search" value="Search" /></td></tr>
</table>
</form>
        </div><!-- end service-->
        
        <div id="media" class="group">
            <?php
            echo "<h2>Search Results:</h2><p>";

            if(isset($_POST['search']))
            {
            $state =$_POST['state'];
            
            //If they did not enter a search term we give them an error
            if ($ntwrk == "" AND $chn== "" AND $state == "")
            {
            echo "<p>You forgot to enter a search term!!!";
            exit;
            }

            // Otherwise we connect to our Database
$con=mysql_connect('localhost', 'abcd', '1234');
if(!$con){
die('Connection error'. mysql_error());
}
mysql_select_db('mapping', $con);
            
            //Now we search for our search term, in the field the user specified
            $reslt = mysql_query("SELECT * FROM Channel WHERE State LIKE '%$state%'")
             or die(mysql_error());

echo "<table border='1' width='900' class='srchrslt'>
<tr class='head'>
<td>State</td><td>City</td><td>Network</td><td>Channel</td><td>LCN</td></tr>";
            
            //And we display the results
            while($result = mysql_fetch_array( $reslt ))
            {              
                echo "<tr>";
            
echo "<td>" . $result['State'] . " </td>";
echo "<td>" . $result['City'] . " </td>";
echo "<td>" . $result['Network'] . " </td>";
echo "<td>" . $result['Channel'] . " </td>";
echo "<td>" . $result['LCN'] . " </td>";
echo "</tr>";
  }
echo "</table>";
          
            //This counts the number or results - and if there wasn't any it gives them a     little     message explaining that
            $anymatches = mysql_num_rows($reslt);
            if ($anymatches == 0)
            {
            echo "Sorry, but we can not find an entry to match your query...<br><br>";
            }
            }
            ?>

Link to comment
Share on other sites

H I have used array in checbox name attribute,

 

No you haven't. They should be

name = "state[]"

Then

foreach ($_POST['state'] as $state) {
    $statearray[] = $mysql_real_escape_string($state);
}
$states = implode ("','", $statearray);
$sql = "SELECT * FROM channel WHERE state IN ('$states')";
Link to comment
Share on other sites

Barands solution seems fit for the answer to your problem. This is how your code should be

<form name="search" method="post" action="find.php">
<table width="900" border="1" class="srch">
<tr class="head"><td>States</td><td>Networks</td><td>Channels</td></tr>
<tr><td>
<input type="checkbox" value="Delhi" name="state[]">DELHI<br />
<input type="checkbox" value="Chandigarh" name="state[]">Chandigarh<br />
<input type="checkbox" value="Patna" name="state[]">Patna<br />
<input type="checkbox" value="Kolkata" name="state[]">Kolkata<br />
<input type="checkbox" value="Madras" name="state[]">Madras<br />
<input type="checkbox" value="Mumbai" name="state[]">Mumbai<br /></td>
<td>
</tr>
<tr><td colspan="3" align="Right"><input type="submit" name="search" value="Search" /></td></tr>
</table>
</form>
        </div><!-- end service-->
        
        <div id="media" class="group">
<?php
echo "<h2>Search Results:</h2><p>";

if(isset($_POST['search']))
{
    $state = $_POST['state'];
    
    //If they did not enter a search term we give them an error
    if ($ntwrk == "" AND $chn== "" AND !is_array($state))
    {
        echo "<p>You forgot to enter a search term!!!";
        exit;
    }

    // Otherwise we connect to our Database
    $con=mysql_connect('localhost', 'abcd', '1234');
    if(!$con){
        die('Connection error'. mysql_error());
    }
    mysql_select_db('mapping', $con);

    foreach ($_POST['state'] as $state) {
        $statearray[] = mysql_real_escape_string($state);
    }
    $states = implode ("','", $statearray);
    $sql = "SELECT * FROM channel WHERE state IN ('$states')";

    //Now we search for our search term, in the field the user specified
    $result = mysql_query($sql) or die(mysql_error());

    //This counts the number or results - and if there wasn't any it gives them a     little     message explaining that
    if (mysql_num_rows($result) == 0)
    {
        echo "Sorry, but we can not find an entry to match your query...<br><br>";
    }
    else
    {
        echo "<table border='1' width='900' class='srchrslt'>
        <tr class='head'>
        <td>State</td><td>City</td><td>Network</td><td>Channel</td><td>LCN</td></tr>";
        
        //And we display the results
        while($row = mysql_fetch_assoc( $result ))
        {              
            echo "<tr>";
                        
            echo "<td>" . $row['State'] . " </td>";
            echo "<td>" . $row['City'] . " </td>";
            echo "<td>" . $row['Network'] . " </td>";
            echo "<td>" . $row['Channel'] . " </td>";
            echo "<td>" . $row['LCN'] . " </td>";
            echo "</tr>";
        }
        echo "</table>";
    }
}
?>
Edited by Ch0cu3r
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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