Re: Re: checkbox into mysql using php <input type="checkbox"name="list
| From: | moses Woldeselassie | Date: | Sat, 19 Feb 2005 01:41:37 +0000 |
| Subject: | Re: Re: checkbox into mysql using php <input type="checkbox"name="list | ||
| References: | 1 | Groups: | php.db |
| Request: | Send a blank email to php-db+get-38613@lists.php.net to get a copy of this message | ||
Thank you. It works fine with one column, when i use the following syntax:
insert into interest(username) values($game_name);
but is it possible to use it with more than one columns: for example if a user (david) has selected football and hockey:
username | football | soccer | hockey--------------------------------------------------------------
david | football | | hockeydo i have to use implode? what do i need? kind regards m >From: Martin Norland <martin.norland@stjude.org> >Reply-To: php-db@lists.php.net >To: php-db@lists.php.net >Subject: Re: [PHP-DB] Re: checkbox into mysql using php <input type="checkbox"name="list >Date: Thu, 17 Feb 2005 08:42:19 -0600 > >Bastien Koert wrote: >>if its all going into one col, then implode the array into a string >> >>$data = implode(",",$_POST['games']); >> >>Then insert the data. Still doesn't relieve you of the need to >>validate the user data. >> >>Bastien >[snip] >No no no - you're confusing the guy further. He's taking an array >post variable, foreaching it (resulting in a string in $value) and >then using string indexes. >$value == 'football' >$value[0] == 'f' >etc. >[unsnip - more to come] >>>From: "moses Woldeselassie" <mmoses@hotmail.com> >>> >>>I did try that, but the input is going into one column or it is >>>take the last array value and put it each chart in the table. >>> >>><input type="checkbox" name="games[]" value="football" /> >>> >>>in that case, these are the value i got in mysql: f, o, o, t >>>
>>> foreach($_POST['games'] AS $game => $value) >>> $valuecount = count($_POST['games']); >>> for($i =0; $i < $valuecount; $i++)>>>$sql = mysql_query("insert into interest >>>values('$value[0]','$value[1]','$value[2]','$value[3]')"); >[snip] >what he wants is (I think): > >foreach($_POST['games'] AS $game_name) { > $sql = mysql_query("insert into interest values('$game_name');"); >} > >or something similar. The idea is that they check 3 boxes for >football, soccer, and hockey - and it inserts three rows into the >database. It seems like we want to be inserting more than just the >game though - surely the user it's associated with / etc. > >Moses - be sure to use curly brackets {} around blocks. Your above >code only runs the "$valuecount = " bit inside the foreach, which >is why you were spared it running 'squared' times (e.g. 2 games = 4 >times, 3 = 9, 4 = 16). The foreach and the for($i= code above are >both there to accomplish the same thing, but $value stores the last >seen value, so it would have looked as if you were still inside the >foreach's scope. Stick with the foreach imo, since you don't need >the counter. (you can get it back by using your foreach, the >counter is the "$game =>" part - in my example I left it out since >it's unnecessary. > >Cheers, >-- >- Martin Norland, Sys Admin / Database / Web Developer, >International Outreach x3257 >The opinion(s) contained within this email do not necessarily >represent those of St. Jude Children's Research Hospital. > >-- >PHP Database Mailing List (http://www.php.net/) >To unsubscribe, visit: http://www.php.net/unsub.php >