Dalam
bagian kedua, ini kita akan melakukan update dan delete data dari
database. Ini akan menjadi tutorial yang singkat karena Anda sudah
mengetahui bagaimana memasukkan data ke database dari Flash. Proses
update dan delete tidak sulit. Jika masuh kesulitan, baca lagi tutorial
sebelumnya. Biasanya, salah ketik dan salah nama field di database yang
membuat Anda pusing. Jangan lupa untuk clear recen history agar
perubahan terbaru muncul di browser.
UPDATE AND DELETE
Di Flash, Anda sudah mempunyai 3 variable untuk
melakukan tugas inserting: variables, varSend, and varLoader. Sekarang
kita akan membuat 3 variabel lain yang melakukan updating job dan 3
lainnya untuk delete.Buat layer actionscript untuk skrip di bawah ini:
...
edit_btn.visible=true;hapus_btn.visible=true;tambah_btn.visible=true;submit_btn.visible=false;//removeChild(loader);
//we dont need this anymore. or you will get Error #2025
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
| ///////////////////////////////// /// the updating section ///// //////////////////////////////// var editvariables:URLVariables = new URLVariables(); var editvarSend:URLRequest = new URLRequest( "crud.php" ); editvarSend.method = URLRequestMethod.POST; editvarSend.data = editvariables; var editvarLoader:URLLoader = new URLLoader; editvarLoader.dataFormat = URLLoaderDataFormat.VARIABLES; editvarLoader.addEventListener(Event.COMPLETE, editDone); function editDone(event:Event):void { ket.text = "" ; ket2.text = "" ; img.text = "" ; gotoAndPlay(1); status_txt.text = event.target.data.kode; } edit_btn.buttonMode= true ; edit_btn.mouseChildren = false ; edit_btn.teks.text= "EDIT" ; edit_btn.addEventListener(MouseEvent.CLICK, editDo); function editDo(event:MouseEvent):void { removeChild(loader); loader.unload(); editvariables.action= "edit" ; editvariables.id = xid.text; editvariables.name = ket.text; editvariables.description = ket2.text; editvariables.image = img.text; editvarLoader.load(editvarSend); play(); } ///////////////////////////////// /// the deleting section ///// //////////////////////////////// var delvariables:URLVariables = new URLVariables(); var delvarSend:URLRequest = new URLRequest( "crud.php" ); delvarSend.method = URLRequestMethod.POST; delvarSend.data = delvariables; var delvarLoader:URLLoader = new URLLoader; delvarLoader.dataFormat = URLLoaderDataFormat.VARIABLES; delvarLoader.addEventListener(Event.COMPLETE, completeDelete); function completeDelete(event:Event):void { ket.text = "" ; ket2.text = "" ; img.text = "" ; gotoAndPlay(1); status_txt.text = event.target.data.kode; } hapus_btn.buttonMode= true ; hapus_btn.mouseChildren = false ; hapus_btn.teks.text= "DELETE" ; hapus_btn.addEventListener(MouseEvent.CLICK, deleteData); function deleteData(event:MouseEvent):void { removeChild(loader); loader.unload(); delvariables.action= "deleting" ; delvariables.id = xid.text; delvarLoader.load(delvarSend); play(); } |
Skrip updating dan deleting hampir mirip dengan fungsi inserting. Perbedaannya adalah nama-nama variable: URLVariables "action", dan data yang dikirim ke php. Jangan lupa juga untuk mengubah "crud.php".Ini modifikasi "crud.php".
crud.php:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
| $server = "localhost" ; $username = "root" ; $password = "" ; $database = "you database" ; $nameTable = "kuliner" ; if (!mysql_connect( $server , $username , $password )) { $vartoflash = 'kode=Connection failed&' ; } elseif (!mysql_select_db( $database )) { $vartoflash = 'kode=Database select failed&' ; } else { //if no error, begin the insert if ( $_POST [ 'action' ] == "adding" ) { $insertQ = "INSERT INTO $nameTable (id, nama, `desc`, gambar)VALUES (NULL, '" . addslashes ( $_POST ['name '])."' , '".addslashes($_POST[' description '])."' , '".addslashes($_POST[' image '])."' )"; //"desc" is a MySQL reserved word, should use the backtick if (!mysql_query ( $insertQ )) { $msg = mysql_error(); $vartoflash = 'kode=Query failed&msg=' . $msg ; } else { // inserted record get the original id $id = mysql_insert_id(); $vartoflash = 'kode=Success&id=' . $id . '&' ; } }; // end if adding if ( $_POST [ 'action' ] == "edit" ) { $updateQ = "UPDATE $nameTable SET nama='" . addslashes ( $_POST ['name '])."' , `desc`= '".addslashes($_POST[' description '])."' , gambar= '".addslashes($_POST[' image '])."' WHERE id= '".$_POST[' id ']."' "; if (!mysql_query ( $updateQ )) { $msg = mysql_error(); $vartoflash = 'kode=Query failed&msg=' . $msg ; } else { $id = mysql_insert_id(); $vartoflash = 'kode=Success&id=' . $id . '&' ; } }; // end edit if ( $_POST [ 'action' ] == "deleting" ) { if ( $_POST [ 'id' ] > 2) { $deleteQ = "DELETE FROM $nameTable WHERE id=" . $_POST [ 'id' ]; if (!mysql_query ( $deleteQ )) { $msg = mysql_error(); $vartoflash = 'kode=Query failed&msg=' . $msg ; } else { $vartoflash = 'kode=Success&' ; } } else { $vartoflash = 'kode=You not able to delete 2 firts records&' ; } }; //end deleting }; echo $vartoflash ; //flash will use this output. it is important to check where the problem is when something going wrong |
0 komentar:
Posting Komentar
Silahkan TInggalkan Komentar Anda di Blog Saya..
No SPAM and No PORN...
Terimakasih Telah meninggalkan Jejak Anda