genzedu777 Posted October 11, 2010 Share Posted October 11, 2010 Hi guys, I have an intention to allow user to select multiple district, however even though when they select more than 2 options in the district, it turns out my database is only able to capture the last value. For instance, I chose ID=1 ID=2, however the return value to my database will be only ID=2. Is there something which I have missed out? Your advice is greatly appreciated. Thank you <?php $district = $_POST['district']; if (isset($_POST['submit'])) { $dbc = mysqli_connect('localhost', '***', '***', '***') or die(mysqli_error()); $query2 = "INSERT INTO tutor_preferred_dlocation (district_id) VALUES ('$district')"; $result2 = mysqli_query($dbc, $query2) or die(mysqli_error()); } <input name="district" type="checkbox" id="1" value="1"> <label for="yishun">Yishun</label> <input name="district" type="checkbox" id="2" value="2"> <label for="angMoKio">Ang Mo Kio</label> <input name="district" type="checkbox" id="3" value="3"> <label for="yioChuKang">Yio Chu Kang</label> ?> Quote Link to comment Share on other sites More sharing options...
Pawn Posted October 11, 2010 Share Posted October 11, 2010 This assumes you want to create a table row for each selected value. <?php if (isset($_POST['submit'])) { $dbc = mysqli_connect('localhost', '***', '***', '***') or die(mysqli_error()); unset($_POST['submit']); $num_fields = count($_POST); $i = 0; while($i <= $num_fields) { $i++; if(!empty($_POST['district_'.$i])) { $sql = "INSERT INTO tutor_preferred_dlocation (district_id) VALUES ('".$_POST['district_'.$i]."')"; if(!$query = mysql_query($sql)) { echo "Error on line ".__LINE__.". ".mysql_error(); exit; } echo "ID <b>".$_POST['district_'.$i]."</b> successfully inserted. <br />\n"; } } } else { ?> <input name="district_1" type="checkbox" id="yishun" value="1"> <label for="yishun">Yishun</label> <input name="district_2" type="checkbox" id="angMoKio" value="2"> <label for="angMoKio">Ang Mo Kio</label> <input name="district_3" type="checkbox" id="yioChuKang" value="3"> <label for="yioChuKang">Yio Chu Kang</label> <?php } ?> Untested. Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted October 11, 2010 Share Posted October 11, 2010 I prefer arrays: if (isset($_POST['submit'])) { $districts = $_POST['district']; $dbc = mysqli_connect('localhost', '***', '***', '***') or die(mysqli_error()); foreach($districts as $district) { $query2 = "INSERT INTO tutor_preferred_dlocation (district_id) VALUES ('$district')"; $result2 = mysqli_query($dbc, $query2) or die(mysqli_error()); } } <input name="district[]" type="checkbox" id="1" value="1"> <label for="yishun">Yishun</label> <input name="district[]" type="checkbox" id="2" value="2"> <label for="angMoKio">Ang Mo Kio</label> <input name="district[]" type="checkbox" id="3" value="3"> <label for="yioChuKang">Yio Chu Kang</label> Quote Link to comment Share on other sites More sharing options...
genzedu777 Posted October 11, 2010 Author Share Posted October 11, 2010 Hi AbraCadaver, I received an error, not too sure if I have done it correctly. It gave out an errror for the line which is highlighted in yellow. Warning: Invalid argument supplied for foreach() in D:\inetpub\vhosts\abc.com\httpdocs\report.php on line 128 <?php $name = $_POST['name']; $email = $_POST['email']; $password = $_POST['password']; $zone = $_POST['zone']; $districts = $_POST['district']; $dob = $_POST['dob']; $category = $_POST['category']; $comments = $_POST['comment']; if (isset($_POST['submit'])) { $dbc = mysqli_connect('localhost', '***', '***', '***') or die(mysqli_error()); $query = "INSERT INTO practice_user (name, email, password, dob, category, comments) VALUES ('$name', '$email', '$password', '$dob', '$category', '$comments')"; $results1 = mysqli_query($dbc, $query) or die(mysqli_error()); foreach($districts as $district) { $query2 = "INSERT INTO tutor_preferred_dlocation (district_id) VALUES ('$district')"; $results2 = mysqli_query($dbc, $query2) or die(mysqli_error()); } echo 'Thanks for submitting the form.<br />'; mysqli_close($dbc); } ?> Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted October 11, 2010 Share Posted October 11, 2010 Did you change your inputs to an array as I showed? name="district[]" Quote Link to comment Share on other sites More sharing options...
genzedu777 Posted October 11, 2010 Author Share Posted October 11, 2010 Oh yeah, It works now. Thanks!!!! 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.