Anda di halaman 1dari 6

3/19/2014

Multiple Insert Checkbox dengan PHP | Akhmad's Blog

Akhmad's Blog
Stop dreaming, start action!

Multiple Insert Checkbox dengan PHP


May 31, 2010 PHP, Programming checkbox, php, programming
Kemarin sempat kebingungan aku belajar PHP.Permasalahannya bukan gara-gara komputer Hang atau cuma masalah koneksi ke webserver.Aku kebingungan gimana meyimpan ke database 2 record atau lebih sekaligus menggunakan checkbox.Hampir sejam aku nyoba, eh gak berhasil juga :(. Hmm..akhirnya dapet juga inspirasi dari ilmu website.Aku gunakan konsep multiple delete dari tutorial ilmuwebsite.Nah,,,akhirnya berhasil juga.Yah, senengnya hatiku menemukan apa yang selama ini aku cari,seperti anak ayam baru nemuin induknya..haha Udah ah curhatnya Langsung aja kodingnya Buat dulu tabelnya category, kurang lebih seperti ini:

C R E A T ET A B L E` c a t e g o r y `( ` c a t _ i d `i n t ( 5 )N O TN U L La u t o _ i n c r e m e n t , ` c a t _ n a m e `v a r c h a r ( 1 0 0 )N O TN U L L , ` d e s c r i p t i o n `t e x tN O TN U L L , ` d a t e `d a t eN O TN U L L , P R I M A R YK E Y ( ` c a t _ i d ` ) )E N G I N E = I n n o D B D E F A U L TC H A R S E T = l a t i n 1A U T O _ I N C R E M E N T = 7;

Kemudian buat tabel itemnya :

http://fanjavaakhmad.wordpress.com/2010/05/31/multiple-insert-checkbox-dengan-php/

1/7

3/19/2014

Multiple Insert Checkbox dengan PHP | Akhmad's Blog

C R E A T ET A B L E` i t e m `(

` i d `i n t ( 5 )N O TN U L La u t o _ i n c r e m e n t , ` n a m e `v a r c h a r ( 1 0 0 )N O TN U L L , ` c a t e g o r y `i n t ( 5 )N O TN U L L , ` p r i c e `v a r c h a r ( 1 0 0 )N O TN U L L , ` d e s c r i p t i o n `t e x tN O TN U L L , ` d a t e `d a t e t i m eN O TN U L L , P R I M A R YK E Y ( ` i d ` ) )E N G I N E = I n n o D B D E F A U L TC H A R S E T = l a t i n 1A U T O _ I N C R E M E N T = 6;

T a p is e b e l u m n y aj a n g a nl u p am e m b u a tf i l ec o n f i g . p h py a n gb e r i s i f i l ek o n e k s ik ed a t a b a s e n y a . K a l un e l u mt a ug i m a n ab u a tk o n e k s ik e d a t a b a s e n y al i h a td i s i n i .

I n iu n t u kf o r m n y a ( s i m p a nd e n g a nc o m b o . p h p ):

< f i e l d s e t > < l e g e n d > < b > T a m b a hI t e m& a m p ;C a t e g o r y< / b > < / l e g e n d > < t a b l ew i d t h = " 1 0 0 % "b o r d e r = " 0 "c e l l s p a c i n g = " 2 " c e l l p a d d i n g = " 2 " > < t r > < t dw i d t h = " 4 9 % "h e i g h t = " 2 6 0 "v a l i g n = " t o p " > < f o r m n a m e = " f o r m 1 "m e t h o d = " p o s t "a c t i o n = " p r o s e s c o m b o . p h p " > < t a b l ew i d t h = ' 9 6 % 'b o r d e r = ' 0 'c e l l s p a c i n g = ' 2 ' c e l l p a d d i n g = ' 2 ' > < t r > < t dw i d t h = ' 2 8 % ' > < d i va l i g n = ' r i g h t ' >
http://fanjavaakhmad.wordpress.com/2010/05/31/multiple-insert-checkbox-dengan-php/ 2/7

3/19/2014

Multiple Insert Checkbox dengan PHP | Akhmad's Blog

< s t r o n g > I t e mN a m e:< / s t r o n g > < / d i v > < / t d > < t dw i d t h = ' 7 2 % ' > < i n p u tn a m e = ' n a m e ' t y p e = ' t e x t 'i d = ' n a m e 's i z e = ' 3 0 '/ > < / t d > < / t r > < t r > < t d > < d i va l i g n = ' r i g h t ' > < s t r o n g > P r i c e: < / s t r o n g > < / d i v > < / t d > < t d > < i n p u tn a m e = ' p r i c e 't y p e = ' t e x t ' i d = ' p r i c e 's i z e = ' 3 0 '/ > < / t d > < / t r > < t r > < t dv a l i g n = ' t o p ' > < d i va l i g n = ' r i g h t ' > < s t r o n g > C a t e g o r y:< / s t r o n g > < / d i v > < / t d > < t d > < ? p h p $ q u e r y=' s e l e c t*f r o m c a t e g o r yo r d e rb yc a t _ i da s c ' ; $ s q l=m y s q l _ q u e r y ( $ q u e r y ) ;

w h i l e ( $ d a t a= m y s q l _ f e t c h _ a r r a y ( $ s q l ) ) { e c h o" < d i v c l a s s = ' c a t e g o r y ' > < i n p u tn a m e = ' c a t e g o r y [ ] 't y p e = ' c h e c k b o x ' v a l u e = ' $ d a t a [ c a t _ i d ] '/ > $ d a t a [ c a t _ n a m e ]< / d i v > " ; } ? > < / t d > < / t r >
http://fanjavaakhmad.wordpress.com/2010/05/31/multiple-insert-checkbox-dengan-php/ 3/7

3/19/2014

Multiple Insert Checkbox dengan PHP | Akhmad's Blog

< t r > < t dr o w s p a n = " 2 "v a l i g n = ' t o p ' > < d i v a l i g n = ' r i g h t ' > < s t r o n g > D e s c r i p t i o n:< / s t r o n g > < / d i v > < / t d > < t d > < t e x t a r e an a m e = ' d e s c r i p t i o n 'c o l s = ' 5 0 ' r o w s = ' 4 'i d = ' d e s c r i p t i o n ' > < / t e x t a r e a > < / t d > < / t r > < t r > < t d > < i n p u tt y p e = ' s u b m i t 'n a m e = ' S u b m i t ' v a l u e = ' A d dI t e m '/ > < / t d > < / t r > < / t a b l e > < / f o r m > < b r > < / t d > < t dw i d t h = " 5 1 % "v a l i g n = " t o p " > < f o r mn a m e = " f o r m 2 " m e t h o d = " p o s t "a c t i o n = " p r o s e s c a t . p h p " > < t a b l ew i d t h = ' 9 6 % 'b o r d e r = ' 0 'c e l l s p a c i n g = ' 2 ' c e l l p a d d i n g = ' 2 ' > < t r > < t dw i d t h = ' 2 9 % ' > < d i va l i g n = ' r i g h t ' > < s t r o n g > C a t e g o r y N a m e:< / s t r o n g > < / d i v > < / t d > < t dw i d t h = ' 7 1 % ' > < i n p u tn a m e = ' c a t _ n a m e ' t y p e = ' t e x t 'i d = ' c a t _ n a m e 's i z e = ' 3 0 '/ > < / t d > < / t r > < t r >
http://fanjavaakhmad.wordpress.com/2010/05/31/multiple-insert-checkbox-dengan-php/ 4/7

3/19/2014

Multiple Insert Checkbox dengan PHP | Akhmad's Blog

< t dr o w s p a n = " 2 "v a l i g n = ' t o p ' > < d i v a l i g n = ' r i g h t ' > < s t r o n g > D e s c r i p t i o n:< / s t r o n g > < / d i v > < / t d > < t d > < t e x t a r e an a m e = ' d e s c r i p t i o n 'c o l s = ' 5 0 ' r o w s = ' 4 'i d = ' d e s c r i p t i o n ' > < / t e x t a r e a > < / t d > < / t r > < t r > < t d > < i n p u tt y p e = " s u b m i t "n a m e = " S u b m i t 2 " v a l u e = " A d dC a t e g o r y " > < / t d > < / t r > < / t a b l e > < / f o r m > < b r > < / t d > < / t r > < / t a b l e > < b r/ > < / f i e l d s e t >

Y a n gi n ia d a l a hf i l ep r o s e sf o r m n y a( s i m p a nd e n g a nn a m a p r o s e s c o m b o . p h p ):

< ? p h p i n c l u d e" c o n f i g . p h p " ;

$ c a t e g o r y $ b a n y a k

=$ _ P O S T [ c a t e g o r y ] ; =c o u n t ( $ c a t e g o r y ) ;
5/7

http://fanjavaakhmad.wordpress.com/2010/05/31/multiple-insert-checkbox-dengan-php/

3/19/2014

Multiple Insert Checkbox dengan PHP | Akhmad's Blog

$ n a m e $ p r i c e

=$ _ P O S T [ ' n a m e ' ] ; =$ _ P O S T [ ' p r i c e ' ] ;

$ d e s c r i p t i o n =$ _ P O S T [ ' d e s c r i p t i o n ' ] ;

f o r ( $ i = 0 ;$ i < $ b a n y a k ;$ i + + ) {

$ q u e r y =" i n s e r ti n t oi t e mv a l u e s( ' ' ,' $ n a m e ' , ' $ c a t e g o r y [ $ i ] ' ,' $ p r i c e ' ,' $ d e s c r i p t i o n ' ,n o w ( ) ) " ; $ s q l =m y s q l _ q u e r y ( $ q u e r y ) ; i f ( $ s q l ) { h e a d e r ( " l o c a t i o n : c o m b o b o x . p h p " ) ; }e l s e { h e a d e r ( " l o c a t i o n : c o m b o b o x . p h p " ) ; }

} ? >

S e l a m a tm e n c o b ay a

http://fanjavaakhmad.wordpress.com/2010/05/31/multiple-insert-checkbox-dengan-php/

6/7