maskme Posted June 24, 2007 Share Posted June 24, 2007 Dear All I've 15 records displaying in a page. Each record has a field called "Sports" which stores values separated by semicolon ";" "Football;Cricket;Basketball" When I'm retrieving the values, i use explode and assign each values of check box. Works perfect with No Problem. I'm storing the Record ID as a hidden field. I'm the admin and I uncheck "Football" or "Basketball" in couple of records and click on Submit and give the following code if ($action == "submit") { for ($j=0; $j < count($recordid); $j++) { if (count($sports[$j]) == 0) $sportslist = "None"; else { for ($i=0; $i < count($sports[$j]); $i++) { $sportslist .= $sports[$i].";"; } } $utpquery = "UPDATE user_sports SET sports='$sportslist' where urecord = $recordid[$j]"; $utpresult = mysql_query($utpquery) or die("Query failed 1"); } header("Location: verifyrecord.php"); exit; } Once I execute the above, I check the Database and find the ones which I had Unchecked have become in Multiples For e.g. "Basketball;Basketball;Basketball;Basketball;Basketball;" I don't know what am I doing wrong here. Could anyone give me a Hint??? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/56946-solved-multiple-checkbox-problem/ Share on other sites More sharing options...
Wildbug Posted June 24, 2007 Share Posted June 24, 2007 Use implode(';',$sports) instead of the for loop. (That shouldn't solve your problem, but it's the easier way to accomplish that part.) Can you give us print_r($_POST) for this submission? Quote Link to comment https://forums.phpfreaks.com/topic/56946-solved-multiple-checkbox-problem/#findComment-281299 Share on other sites More sharing options...
maskme Posted June 24, 2007 Author Share Posted June 24, 2007 Hi I did the Print it comes following id followed by the checkbox selection. It never adds the Basketball or Cricket though it is ticked. 12 Football; 13 Football; Football; 14 Football;Football;Football; 15 Football;Football;Football;Football; I'm going nuts over this. Quote Link to comment https://forums.phpfreaks.com/topic/56946-solved-multiple-checkbox-problem/#findComment-281320 Share on other sites More sharing options...
Wildbug Posted June 24, 2007 Share Posted June 24, 2007 That's weird. Can you give the actual print_r plus the HTML form code? Are you using the array method of gathering checkbox data? I can't see why football is repeated like that. Quote Link to comment https://forums.phpfreaks.com/topic/56946-solved-multiple-checkbox-problem/#findComment-281322 Share on other sites More sharing options...
maskme Posted June 24, 2007 Author Share Posted June 24, 2007 Hi Thanks for your prompt reply Here is the section that works with no hitch gives me all the selection as per the user had done while filling up the form initially <form name="frm1" method="post" action="sports.php?page=update"> <table cellspacing="0" cellpadding="0" border="0"> <? $squery = "select * from user_sports"; $sresult = mysql_query($squery) or die ("Query Failed"); while( $row = mysql_fetch_array( $sresult ) ) { $sports = explode(";",$row['sports']); ?> <tr> <td> <input name="recordid[]" type="hidden" value="<? echo $row['urecord'] ?>"> <input name="sports[]" type="checkbox" id="sports[]" value="Football" <? if (is_array($sports) == TRUE) { if (in_array("Football", $sports)) echo "CHECKED"; } ?>> Football<br> <input name="sports[]" type="checkbox" id="sports[]" value="Cricket" <? if (is_array($sports) == TRUE) { if (in_array("Cricket", $sports)) echo "CHECKED"; } ?>> Cricket <br> <input name="sports[]" type="checkbox" id="sports[]" value="Basketball" <? if (is_array($sports) == TRUE) { if (in_array("Basketball", $sports)) echo "CHECKED"; } ?>> Basketball </td> </tr> <? } ?> </table> </form> Here is the section which does not work, for which i need to be able to update. <? if ($page == "update") { for ($j=0; $j < count($recordid); $j++) { if (count($sports[$j]) == 0) $sportslist = "None"; else { for ($i=0; $i < count($sports[$j]); $i++) { $sportslist .= $sports[$i].";"; } } echo $recordid[$j]; echo "<br>"; echo $sportslist; echo "<br>"; //$utpquery = "UPDATE user_sports SET sports='$sportslist' where urecord = $recordid[$j]"; //$utpresult = mysql_query($utpquery) or die("Query failed 1"); } //header("Location: sports.php"); //exit; } ?> Please let me know where it is going wrong Quote Link to comment https://forums.phpfreaks.com/topic/56946-solved-multiple-checkbox-problem/#findComment-281332 Share on other sites More sharing options...
maskme Posted June 25, 2007 Author Share Posted June 25, 2007 Any help on this? Quote Link to comment https://forums.phpfreaks.com/topic/56946-solved-multiple-checkbox-problem/#findComment-281857 Share on other sites More sharing options...
redarrow Posted June 25, 2007 Share Posted June 25, 2007 this is the problam try a diffrent loop ok <?php for ($j=0; $j < count($recordid); $j++) { if (count($sports[$j]) == 0) $sportslist = "None"; else { for ($i=0; $i < count($sports[$j]); $i++) { $sportslist .= $sports[$i].";"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/56946-solved-multiple-checkbox-problem/#findComment-281861 Share on other sites More sharing options...
sasa Posted June 25, 2007 Share Posted June 25, 2007 1st change your form <form name="frm1" method="post" action="sports.php?page=update"> <table cellspacing="0" cellpadding="0" border="0"> <? $squery = "select * from user_sports"; $sresult = mysql_query($squery) or die ("Query Failed"); $number = 0; while( $row = mysql_fetch_array( $sresult ) ) { $sports = explode(";",$row['sports']); ?> <tr> <td> <input name="recordid[$number]" type="hidden" value="<? echo $row['urecord'] ?>"> <input name="sports[$number][]" type="checkbox" id="sports[]" value="Football" <? if (is_array($sports) == TRUE) { if (in_array("Football", $sports)) echo "CHECKED"; } ?>> Football<br> <input name="sports[$number][]" type="checkbox" id="sports[]" value="Cricket" <? if (is_array($sports) == TRUE) { if (in_array("Cricket", $sports)) echo "CHECKED"; } ?>> Cricket <br> <input name="sports[$number][]" type="checkbox" id="sports[]" value="Basketball" <? if (is_array($sports) == TRUE) { if (in_array("Basketball", $sports)) echo "CHECKED"; } ?>> Basketball </td> </tr> <? $number++;} ?> </table> </form> Quote Link to comment https://forums.phpfreaks.com/topic/56946-solved-multiple-checkbox-problem/#findComment-281884 Share on other sites More sharing options...
maskme Posted June 25, 2007 Author Share Posted June 25, 2007 Thanks Sasa, but what next, what happens once i post. I'm not good at arrays, i would appreciate, if you could guide me how do i retrieve from these multiple values??? Quote Link to comment https://forums.phpfreaks.com/topic/56946-solved-multiple-checkbox-problem/#findComment-281904 Share on other sites More sharing options...
Illusion Posted June 25, 2007 Share Posted June 25, 2007 I don't think your update query will work , unless it is something like this UPDATE user_sports SET sports='$sportslist' where urecord = '$recordid[$j]'; or UPDATE user_sports SET sports='$sportslist' where urecord = {$recordid['$j]'}; might be missing something? Quote Link to comment https://forums.phpfreaks.com/topic/56946-solved-multiple-checkbox-problem/#findComment-281911 Share on other sites More sharing options...
sasa Posted June 25, 2007 Share Posted June 25, 2007 can you insert print_r($_POST); and post the output i think thet you need somethin like this <? if ($page == "update") { for ($j=0; $j < count($recordid); $j++) { if (count($sports[$j]) == 0) $sportslist = "None"; else $sportslist = implode(';', $sports[$j]); // { // for ($i=0; $i < count($sports[$j]); $i++) // { // $sportslist .= $sports[$i].";"; // } // } echo $recordid[$j]; echo "<br>"; echo $sportslist; echo "<br>"; //$utpquery = "UPDATE user_sports SET sports='$sportslist' where urecord = $recordid[$j]"; //$utpresult = mysql_query($utpquery) or die("Query failed 1"); } //header("Location: sports.php"); //exit; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/56946-solved-multiple-checkbox-problem/#findComment-281914 Share on other sites More sharing options...
maskme Posted June 25, 2007 Author Share Posted June 25, 2007 Thanks a million Sasa, it works like a charm... Array ( [recordid] => Array ( [0] => 1 [1] => 2 ) [sports] => Array ( [0] => Array ( [0] => Cricket [1] => Basketball ) [1] => Array ( [0] => Basketball ) ) [submit] => submit ) How it works is by using Multi Dimensional Arrays. Cool I've understood how this works perfectly. Amazing stuff!!! Thanks a lot Quote Link to comment https://forums.phpfreaks.com/topic/56946-solved-multiple-checkbox-problem/#findComment-281950 Share on other sites More sharing options...
sasa Posted June 25, 2007 Share Posted June 25, 2007 Quote Link to comment https://forums.phpfreaks.com/topic/56946-solved-multiple-checkbox-problem/#findComment-281955 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.