Re: Re: checkbox into mysql using php <input type="checkbox"name="list

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

« previous php.db (#38613) next »