Chimp17 Posted January 1, 2014 Share Posted January 1, 2014 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..!!!!!! Quote Link to comment https://forums.phpfreaks.com/topic/285031-display-data-from-database-using-multiple-checkbox-selected-form/ Share on other sites More sharing options...
Chimp17 Posted January 1, 2014 Author Share Posted January 1, 2014 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>"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/285031-display-data-from-database-using-multiple-checkbox-selected-form/#findComment-1463562 Share on other sites More sharing options...
Barand Posted January 2, 2014 Share Posted January 2, 2014 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')"; Quote Link to comment https://forums.phpfreaks.com/topic/285031-display-data-from-database-using-multiple-checkbox-selected-form/#findComment-1463633 Share on other sites More sharing options...
Chimp17 Posted January 3, 2014 Author Share Posted January 3, 2014 Thanks for reply as i said, had used array but it was working nd foreach also gives same reslt. nd the coding i pasted here is without array nd foreach condition. i tried ur suggestion also nd d result is same. Quote Link to comment https://forums.phpfreaks.com/topic/285031-display-data-from-database-using-multiple-checkbox-selected-form/#findComment-1463681 Share on other sites More sharing options...
Ch0cu3r Posted January 3, 2014 Share Posted January 3, 2014 (edited) 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 January 3, 2014 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/285031-display-data-from-database-using-multiple-checkbox-selected-form/#findComment-1463685 Share on other sites More sharing options...
Chimp17 Posted January 8, 2014 Author Share Posted January 8, 2014 Thanks Ch0cu3r and Barand for your help guys. Quote Link to comment https://forums.phpfreaks.com/topic/285031-display-data-from-database-using-multiple-checkbox-selected-form/#findComment-1464447 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.