blacktiger786 Posted November 12, 2013 Share Posted November 12, 2013 i have code which select country from html and its get multiply country in array i implement in php code to database save but not work please tell me what i do this is code <span class="st-labeltext">Countries</span> <label class="margin-right10"><input type="radio" id="r_all" name="countries" value="allc" class="uniform" /> All Countries</label> <label class="margin-right10"><input type="radio" id="r_selected" name="countries" value="selectedc" class="uniform"/> Selected Countries</label> <div id="clist_div" class="simplebox cgrid540-right" style="display:none;"> <div style="padding:5px"></div> <div class="simplebox cgrid200-left"> <p style="text-align:center;"><b>Excluded Countries</b></p> <select size="10" name="excludedcountries" style="width:200px; height:160px;" onDblClick="moveSelectedOptions(this.form['excludedcountries'],this.form['selectedcountries[]'])" multiple ><option value='AF'>Afghanistan</option><option value='AX'>Aland Islands</option><option value='AL'>Albania</option><option value='DZ'>Algeria</option> </div> <div class="simplebox cgrid40-left"> <input class="button-blue" type="button" name="right" value=">>" onclick="moveSelectedOptions(this.form['excludedcountries'],this.form['selectedcountries[]'])"><br/><br/> <input class="button-blue" type="button" name="left" value="<<" onclick="moveSelectedOptions(this.form['selectedcountries[]'],this.form['excludedcountries'])"> </div> <div class="simplebox cgrid200-left"> <p style="text-align:center;"><b>Selected Countries</b></p> <select size="10" name="selectedcountries[]" style="width:200px; height:160px;" onDblClick="moveSelectedOptions(this.form['selectedcountries[]'],this.form['excludedcountries'])" multiple ></select> </div> </div> <div class="clear"></div> </div> please tell me how to save selectedcountries[] array to php to mysql database save code Quote Link to comment Share on other sites More sharing options...
blacktiger786 Posted November 12, 2013 Author Share Posted November 12, 2013 if not under stand please tell me how to echo these select value from select[] <?php if(isset($_POST['select[]'])) { foreach ($_POST['select[]'] as $key => $value) { echo $value; } } ?> <form action="arraycountry.php" method="POST"> <select name="select[]" multiple="yes"> <option value="1">1 is selected</option> <option value="2">2 is selected</option> <option value="4">4 is selected</option> <input type="submit" value="click"> </select></form> Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted November 12, 2013 Share Posted November 12, 2013 if(isset($_POST['select'])) { foreach ($_POST['select'] as $key => $value) { Don't use [] with the $_POST var. Quote Link to comment Share on other sites More sharing options...
blacktiger786 Posted November 12, 2013 Author Share Posted November 12, 2013 this is my code its run file like i select to country from selected form its save it database but its save bot new rowlike1 usa2 pki want its add both on single row like1 usa,pk,ukthis is code.. <?php require 'databaseconnect.php'; if(isset($_POST['select'])) { $movie = $_POST['select']; foreach($movie AS $title) { $run="INSERT INTO `test` VALUES ('','".$title."')"; $result=mysql_query($run); echo $title; } } ?> <form action="arraycountry.php" method="POST"> <select name="select[]" multiple="yes"> <option value="usa">1 is selected</option> <option value="uk">2 is selected</option> <option value="pk">4 is selected</option> <input type="submit" value="click"> </select></form> Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted November 12, 2013 Share Posted November 12, 2013 (edited) That's probably a bad idea. You might want to read up on database design and normalization so you don't run into problems down the road. But the simple answer is this, with no foreach loop: require 'databaseconnect.php'; if(isset($_POST['select'])) { $movies = implode(',', array_map('mysql_real_escape_string', $_POST['select'])); $run = "INSERT INTO `test` VALUES ('','$movies')"; $result = mysql_query($run); } Also, get off of the mysql_* functions and use mysqli_* or PDO. Edited November 12, 2013 by AbraCadaver Quote Link to comment 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.