Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- DROP TABLE IF EXISTS alamat;
- CREATE TABLE IF NOT EXISTS alamat(
- id INT(3) ZEROFILL AUTO_INCREMENT,
- tipe INT(1),
- nama VARCHAR(50),
- alamat VARCHAR(255),
- PRIMARY KEY (tipe,id)
- ) ENGINE=MyISAM;
- */
- $dbhost='localhost';
- $dbuser='root';
- $dbpass='';
- $dbname='test';
- $db=new mysqli($dbhost,$dbuser,$dbpass,$dbname);
- if(isset($_POST['nama'])){
- $sql="INSERT INTO alamat(tipe,nama,alamat) VALUES({$_POST['tipe']},'{$_POST['nama']}','{$_POST['alamat']}')";
- $result=$db->query($sql);
- }
- $sql="SELECT * FROM alamat ORDER BY tipe,id";
- $result=$db->query($sql);
- ?>
- <!DOCTYPE html>
- <html>
- <head>
- <title>Data Alamat</title>
- </head>
- <body>
- <fieldset>
- <legend>Tambah Data</legend>
- <form method='post'>
- <table>
- <tr><td>Nama Perusahaan</td><td><input type='text' name='nama' required></td></tr>
- <tr><td>Alamat Perusahaan</td><td><input type='text' name='alamat' required></td></tr>
- <tr><td>Tipe Perusahaan</td><td>
- <select name='tipe' required>
- <option value='1'>Customer</option>
- <option value='2'>Vendor</option>
- </select>
- </td></tr>
- <tr><td colspan='2'><input type='submit' value='add'></td></tr>
- </table>
- </form>
- </fieldset>
- <fieldset>
- <legend>Daftar Alamat</legend>
- <?php
- if(!empty($result)) {
- if($result->num_rows>0){
- $code=array('1'=>array('CUS','Customer'),'2'=>array('VEN','Vendor')); //<-- kalo pengin dinamis buatin tabel tersendiri di db
- echo "<table>"
- ."<tr><th>Kode</th><th>Tipe</th><th>Nama</th><th>Alamat</th></tr>";
- while($r=$result->fetch_object()){
- echo "<tr><td>{$code[$r->tipe][0]}-{$r->id}</td><td>{$code[$r->tipe][1]}</td><td>{$r->nama}</td><td>{$r->alamat}</td></tr>";
- }
- echo "</table>";
- }
- }
- ?>
- </fieldset>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement