Alur Koneksi php dengan database MySql

Posted by Unknown Label:




1. Buat database pegawai dengan nama tabel data_pegawai .
o   Id : primary key dan autoincrement


2. Buat kode form html , disini digunakan nama form1.html
Contoh :
<html>
<head>
<title> Form Pegawai</title>
</head>
<body>

<p><b><h3> DATA PEGAWAI</p></b></h3>
<form action="inputdata.php" method ="post">
                <table width= "571" border="0" align ="left">
                <tr>
                <td width="120"><strong>Nama</strong></td>
                <td width="326"><input type ="text" name="nama"/>
                </td>
                </tr>
                <tr>
                <td><strong> Email</strong></td>
                <td><input type = "text" name="email" />
                </td>
                </tr>       
                <td><strong> Alamat</strong></td>
                <td><input type = "text" name="alamat" />
                </td>
                </tr>
                <td><strong> No Telp </strong></td>
                <td><input type = "text" name="no telp" />
                </td>
                </tr>
</tr>
<tr>
<td>
<br>
<input type="submit" name="Simpan" value="Simpan" align="center" />
<input type="reset" name="Rest" value="Reset" align="center" />
</td>
</tr>
</table>
</body>
</html>


3. Untuk menyambungkan database yang sudah dibuat di MySQL dengan form php , dibutuhkan kode koneksi sendiri dengan  kode :
Host : nama server dari komputer kita , biasanya localhost
User : username komputer , biasanya root
Databasename , isi sesuai dengan database yang sudah kita buat *berhubungan dengan no 1*
Contoh kode : (config.php)
<?php
$host = "localhost";
$user = "root";
$pass = "";
$databasename = "pegawai";
$connection = mysql_connect($host, $user, $pass) or die ("salahan koneksi boy ! ");

mysql_select_db($databasename, $connection) or die ("Database Error");
?>


4. Jika koneksi diatas sudah berhasil, buatlah kode aplikasi untuk menghubungkan koneksi php dengan MySQL
Contoh : *inputdata.php
<?php
include "config.php";
mysql_query("INSERT INTO data_pegawai (nama, email,alamat, no_telp)

values('$_POST[nama]','$_POST[email]','$_POST[alamat]','$_POST[no_telp]')");
header('Location:lihat.php');

?>
*Isi sesuai dengan nama filed yang ada di database

5. Terakhir buat kode untuk menampilkan hasil inputan data . data ini akan bertambah secara otomatis bertambah setiap kali input di tambahkan
Kode : lihat.php
<?php
include "config.php";
$query = "SELECT*FROM data_pegawai order by id";
$rs = mysql_query($query) or die (mysql_error().''.$query);
echo"<table class = \"table\" width='700' border='1'>
                                <tr class = \"baris\">
                                                <th><align='center'>id</th>
                                                <th><align='center'>Nama</th>          
                                                <th><align='center'>Email</th>           
                                                <th><align='center'>Alamat</th>
                                                <th><align='center'>No telp</th>
                                </tr>";
                while($hasil = mysql_fetch_array($rs)):
                                                echo"<tr>
                                                <td>".$hasil['id']."</td>
                                                <td>".$hasil['nama']."</td>
                                                <td>".$hasil['email']."</td>
                                                <td>".$hasil['alamat']."</td>
                                                <td>".$hasil['no_telp']."</td>
                                </tr>";
                endwhile;
                echo"</table>";
?>

sekian , dan selamat mencoba :)

0 komentar:

Posting Komentar