Senin, 19 Agustus 2013

RINGKASAN BELAJAR PHP

Tabel Sql :

create table if not exists `latihanprasidang` (
`id`int(12)not null auto_increment,
`nama`varchar(15)not null,
`alamat`varchar(15)not null,
`kodepos`int(5)not null,
primary key(`id`));

Index.php
<form action=input.php method=POST>
nama:<input type=text name=nama><br>
alamat:<input type=text name=alamat><br>
kode POS: <input type=text name=kodepos><br>
<input type=sumit name=goes value=simpan>



Tampil.php

<table border=1>
<tr><td>no</td><td>nama</td><td>alamat</td><td>kodepos</td><td>edit</td><td>hapus</td></tr>
<?php
include "koneksi.php";
$m=mysql_query("select * from latihanprasidang");
$no=1;
while($w=mysql_fetch_array($m)){
echo"<tr><td>$no</td>
<td>$w[nama]</td>
<td>$w[alamat]</td>
<td>$w[kodepos]</td>
<td><a href=edit.php?id=$w[id]>edit</a></td><td><a href=hapus.php?id=$w[id]>hapus</a></td>
</tr>";
$no++;
}
?>
</table>


Edit.php
<?php
include "koneksi.php";
$id = $_GET['id'];
$edit = mysql_query("SELECT * FROM latihanprasidang WHERE id='$id'");
$r = mysql_fetch_array($edit);
?>

<form method="post" action="update.php">
<input type=hidden name=id value="<?php echo $id;?>">
  <table border="1">
    <tr>
      <td>EDIT</td>
    </tr>
    <tr>
      <td>nama</td>
      <td><input name="nama" type="text" value="<?php echo $r['nama'];?>" /></td>
    </tr>
        <tr>
      <td>Alamat</td><td>        <input name="alamat" type="text" value="<?php echo $r['alamat'];?>" /></td>
    </tr>
<tr><td>kode pos</td><td><input type=text name=kodepos value="<?php echo $r['kodepos'];?>"></td>
</tr></table>
<input type=submit name=submit value=update>

Update.php
<?php 
include "koneksi.php"; 

mysql_query("update latihanprasidang set 
nama='$_POST[nama]', alamat='$_POST[alamat]', 
kodepos='$_POST[kodepos]' where id='$_POST[id]'"); 

header('location:latihanpra.php'); 

?>

hapus.php

<?php
include "koneksi.php";
mysql_query("delete from latihanprasidang where id='$_GET[id]'");
header("location:latihanpra.php");
?>

Koneksi.php
<?php
mysql_connect("localhost","root","1");
mysql_select_db("latihanpra2");
?>
- See more at: http://miswanphp.blogspot.com/2013/01/membuat-inputtampiledit-dan-hapus.html#sthash.A7luUAS7.dpuf

Latihan 6

6. Penyimpanan file yang di-Upload, dengan nama file yang tersimpan sama semuanya.
Sebelum menjalankan program dibawah ini, maka register_globals yang ada di php.ini harus di-on-kan terlebih dahulu.

a. Untuk membuat inputan, dan beri nama file : f.php
<html>
<head>
     <title>Form untuk input nama file</title>
</head>
<body>
<h1>Input nama file untuk Upload</h1>
<br>Klik Browse untuk memilih !
<form enctype="multipart/form-data" method="post"
action="f1.php">
<input type="hidden" name="MAX_FILE_SIZE" value="10000">
Nama File : <input type="file" name="file1" size="30">
<br>
<input type="submit" value="Upload">
</form>
</body>
</html>
b. Buat file untuk memproses variabel, beri nama filenya : f1.php, hasil upload bisa dilihat di direktori tempat menyimpan file php.
<html>
<head>
     <title></title>
</head>
<body>
<h1>Simpan file yang diupload</h1>
<br>
<?
if ($file1!="none") {
     copy("$file1","hasilupload.txt") or
     die ("No files");
}
else {
     die("Tidak ada file yang diupload");
}
?>
</body>
</html>
KETERANGAN :
Pada tag form_upload atribut  enctype=”multipart/form-data” harus ada. Atribut ini akan digunakan untuk memberitahu browser dan server bahwa yang dikirim bukan hanya data nama file, namun juga isi dari nama file.
Diperlukan juga sebuah variabel dengan nama MAX_FILE_SIZE dengan tipe hidden, yang harus disediakan untuk mengendalikan besar file maksimum yang diupload ke server. Variabel ini harus diletakkan sebelum input dengan type ’file’, nilai pada atribut value menunjukkan jumlah byte maksimum.

latihan 5

5. Pemrosesan Buku Tamu dengan metode : POST

a. Untuk membuat inputan, dan beri nama file : e.php
<html>
<head>
     <title>Contoh Form dengan POST</title>
</head>
<body>
<h1>Buku Tamu</h1>
Komentar dan saran sangat kami butuhkan untuk
meningkatkan kualitas situs kami.
<hr>
<form action="e1.php" method="post">
<pre>
Nama anda     : <input type="text" name="nama" size="25"
                maxlength="50">
Email address : <input type="text" name="email" size="25"
                maxlength="50">
Komentar      : <textarea name="komentar" cols="40"
                rows="5"> </textarea>
<input type="submit" value="kirim">
<input type="reset" value="ulangi">
</pre>
</form>
</body>
</html>
b. Buat file untuk memproses variabel, beri nama filenya : e1.php
<html>
<head>
     <title>Buku Tamu</title>
</head>
<body>
<?
$nama=$_POST["nama"];
$email=$_POST["email"];
$komentar=$_POST["komentar"];
?>
<h1>Data Buku Tamu </h1>
<hr>
Nama anda     : <?echo $nama?>
<br>
Email address : <?echo $email?>
<br>
Komentar      :
<textarea name="komentar" cols="40"
rows="5"><?echo $komentar?> </textarea>
<br>
</body>
</html>

latihan 4

4. Perbandingan 2 bilangan menggunakan metode : POST

a. Untuk membuat inputan, dan beri nama file : d.php
<html>
<head>
     <title>Contoh Form dengan POST</title>
</head>
<body>
<form action="d1.php" method="post">
Bil I :
<input type="text" name="bil1">
<br>
Bil II:
<input type="text" name="bil2">
<br>
<input type="submit" value="Bandingkan">
</form>
</body>
</html>
b. Buat file untuk memproses variabel, beri nama filenya : d1.php
<html>
<head>
     <title>Proses Input</title>
</head>
<body>
<?
$bil1=$_POST["bil1"];
$bil2=$_POST["bil2"];
?>
<h1>Perbandingan Bilangan </h1>
<hr>
Bil I : <?echo $bil1?>
<br>
Bil II: <?echo $bil2?>
<br>
<?
if ($bil1<$bil2){
echo "$bil1 lebih kecil dari $bil2";
}
elseif($bil1>$bil2){
echo "$bil1 lebih besar dari $bil2";
}
else{
echo "$bil1 sama dengan $bil2";
}
?>
</body>
</html>

Latihan 3

3. Proses input data dengan password menggunakan metode : POST

a. Untuk membuat inputan, dan beri nama file : c.php
<html>
<head>
     <title>Contoh Form dengan POST</title>
</head>
<body>
<form action="c1.php" method="post">
<h1>Only for authorized user</h1>
<hr>
Username : <input type="text" name="username">
<br>
Password : <input type="password" name="password">
<br>
<input type="submit" value="login">
</form>
</body>
</html>
b. Buat file untuk memproses variabel, beri nama filenya : c1.php
<html>
<head>
     <title>Proses Input</title>
</head>
<body>
<?
$username=$_POST["username"];
$password=$_POST["password"];
?>
Username : <?echo $username?>
<br>
Password : <?echo $password?>
<br>
</body>
</html>

Latihan 2

2. Proses Form menggunakan metode : POST

a. Untuk membuat inputan, dan beri nama file : b.php
<html>
<head>
   <title> Form method GET </title>
</head>
<body>
<h1>Input</h1>
<form action="b1.php" method="post">
Masukkan nama : <input type"text" name="nama" size="25">
<input type="submit" value="Proses">
</form>
</body>
</html>
b. Buat file untuk memproses variabel yang diberikan oleh file metodepost.php, beri nama filenya : b1.php
<html>
<head>
   <title> Method POST proses </title>
</head>
<body>
Data nama yang diinputkan adalah : <?echo $_POST["nama"]?>
</body>
</html>
Ket : Dengan metode POST, maka data yang dikirimkan akan disembunyikan, hal ini sangat cocok bila dipakai untuk inputan berupa password. Untuk selain password, pemakaian GET & POST tergantung pada kebutuhan. 

Latihan 1

1. Proses Form menggunakan metode : GET

a. Untuk membuat inputan, dan beri nama file : a.php
<html>
<head>
   <title> Form method GET </title>
</head>
<body>
<h1>Input</h1>
<form action="a1.php" method="get">
Masukkan nama : <input type"text" name="nama" size="25">
<input type="submit" value="Proses">
</form>
</body>
</html>

b. Buat file untuk memproses variabel yang diberikan oleh file metodeget.php, beri nama filenya : a1.php
<html>
<head>
   <title> Method GET proses </title>
</head>
<body>
Data nama yang diinputkan adalah : <?echo $_GET["nama"]?>
</body>
</html>
Ket : Dengan metode GET maka data yang dikirimkan ke file php yang berikutnya akan ditampilkan, cara ini tidak cocok bila data yang dimasukkan berupa password.