Anda di halaman 1dari 4

Membuat Aplikasi Sederhana dengan PHP dan SQL

SERVER Bag – 2
Bismillah…

Ok Kali ini Gw Coba Share Tutorial membuat aplikasi sederhana dengan php dan sql server 2000 bagian kedua >>

yaitu script login,logout dan crud (tambah,edit,hapus,view )…

kalau ketinggalan coba lihat post tentang pembuatan aplikasi sederhana dengan php dan sql server bagian pertama
Y…

langsung aja, siapkan databases >>

kemudian masukan scriptnya >>

1. form login

1 <?php
2 echo"<h3>Form Login</h3><hr><br>
3 <form method=POST action='cek_login.php'>
4 <table border=1 cellpadding=4 cellspacing=0>
5 <tr><td>Nama</td><td><input type=text name='nama'></td></tr>
<tr><td>Password</td><td><input type=password
6 name='password'></td></tr>
7 <tr><td></td><td><input type=submit value='Login'></td></tr>
8 </table>
9 </form>
";
10
?>
11
2. cek_login
1
2 <?php
3 mssql_connect("localhost","sa","sa");
4 mssql_select_db("shoping");
5
6 $nama=htmlentities($_POST['nama']);
7 $password=htmlentities($_POST['password']);
8
if(!ctype_alnum($nama) or !ctype_alnum($password)){
9 echo"Login Gagal";
10 }else{
11 $sql=mssql_query("select * from [user] where nama LIKE '$nama' and password
12 LIKE '$password'");
$data=mssql_fetch_array($sql);
13 $cek=mssql_num_rows($sql);
14 if($cek>0){
15 session_register('id');
16 session_register('nama');
17
18 $_SESSION['id']=$data['id'];
$_SESSION['nama']=$data['nama'];
19
20 header('location:sql_server.php');
21
22 }else{
23 echo"Login GagalL";
24 }
}
25
?>
26
27
3. home.php atau sql_server.php

1 <?php
2 session_start();
3 mssql_connect("localhost","sa","sa");
mssql_select_db("shoping");
4
5 $id=$_SESSION['id'];
6 $nama=$_SESSION['nama'];
7
8 echo"<h3>SELAMAT DATANG | ($nama) dan ID ANDA = $id </h3><hr><p></p>";
9
10if($_GET['menu']==''){
echo"<a href='?menu=tambah_user'><input type=submit
11value='Tambah'></a><p></p><p></p><table border=1 cellpadding=4 cellspacing=0>
12<tr
13bgcolor='#ccc'><td>Id</td><td>Nama</td><td>Email</td><td>Edit</td><td>Hapus</t
14d></tr>";
15$sql=mssql_query("select * from [user]");
while($data=mssql_fetch_array($sql)){
16echo"<tr><td>$data[id]</td><td>$data[nama]</td><td>$data[email]</td><td><a
17href='?menu=edit_user&id=$data[id]'>edit</a></td><td><a
18href=\"aksi.php?act=hapus_user&id=$data[id]\" onclick=\"return confirm('Yakin
19Mau Hapus $data[nama] ??')\">hapus</a></td></tr>";
20}
echo"</table>";
21
22}if($_GET['menu']=='edit_user'){
23$data=mssql_fetch_array(mssql_query("select * from [user] where id =
24'$_GET[id]'"));
25echo"
26<form method=POST action='aksi.php?act=edit_user'>
<input type=hidden name='id' value='$data[id]'>
27<table border=1 cellpadding=4 cellspacing=0>
28<tr><td>Id</td><td><input type=text name='id' value='$data[id]' size=1
29disabled></td></tr>
30<tr><td>Nama</td><td><input type=text name='nama'
value='$data[nama]'></td></tr>
31<tr><td>Email</td><td><input type=text name='email'
32value='$data[email]'></td></tr>
33<tr><td>Password</td><td><input type=text name='password'
34value='$data[password]'></td></tr>
35<tr><td></td><td><input type=submit value='Update'></td></tr>
</table>
36</form>
37";
38}
39
40if($_GET['menu']=='tambah_user'){
echo"
41<form method=POST action='aksi.php?act=tambah_user'>
42<table border=1 cellpadding=4 cellspacing=0>
43<tr><td>Id</td><td><input type=text name='id' size=1></td></tr>
44<tr><td>Nama</td><td><input type=text name='nama'></td></tr>
45<tr><td>Email</td><td><input type=text name='email'></td></tr>
<tr><td>Password</td><td><input type=text name='password'></td></tr>
46<tr><td></td><td><input type=submit value='Tambah'></td></tr>
47</table>
48</form>
49";
50}
51echo"<br> <a href=\"logout.php\" onclick=\"return confirm('Yakin Mau Keluar
??');\">logout</a>";
?>
4. aksi.php

1 <?php
session_start();
2 mssql_connect("localhost","sa","sa");
3 mssql_select_db("shoping");
4
5 $act=$_GET['act'];
6
7 if($act=='edit_user'){
8 mssql_query("update [user] set
nama='$_POST[nama]',email='$_POST[email]',password='$_POST[password]' where
9 id = '$_POST[id]'");
10 header('location:sql_server.php');
11 }
12
13 if($act=='tambah_user'){
mssql_query("insert into [user] (id,nama,email,password) values
14 ('$_POST[id]','$_POST[nama]','$_POST[email]','$_POST[password]') ");
15 header('location:sql_server.php');
16 }
17
18 if($act=='hapus_user'){
mssql_query("delete from [user] where id = '$_GET[id]'");
19 header('location:sql_server.php');
20 }
21
22 ?>
23
5. logout.php

1 <?php
2 session_start();
3 session_destroy();
4 header('location:form_login.php');
?>
5
dan hasil tampilannya seperti ini >>

OK selesai