Advertisement
cdsatrian

Kode Autonumber

Feb 24th, 2017
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.72 KB | None | 0 0
  1. <?php
  2. /*
  3. DROP TABLE IF EXISTS alamat;
  4. CREATE TABLE IF NOT EXISTS alamat(
  5.     id INT(3) ZEROFILL AUTO_INCREMENT,
  6.     tipe INT(1),
  7.     nama VARCHAR(50),
  8.     alamat VARCHAR(255),
  9.     PRIMARY KEY (tipe,id)
  10. ) ENGINE=MyISAM;
  11. */
  12. $dbhost='localhost';
  13. $dbuser='root';
  14. $dbpass='';
  15. $dbname='test';
  16. $db=new mysqli($dbhost,$dbuser,$dbpass,$dbname);
  17. if(isset($_POST['nama'])){
  18.     $sql="INSERT INTO alamat(tipe,nama,alamat) VALUES({$_POST['tipe']},'{$_POST['nama']}','{$_POST['alamat']}')";
  19.     $result=$db->query($sql);
  20. }
  21. $sql="SELECT * FROM alamat ORDER BY tipe,id";
  22. $result=$db->query($sql);
  23. ?>
  24. <!DOCTYPE html>
  25. <html>
  26. <head>
  27.     <title>Data Alamat</title>
  28. </head>
  29. <body>
  30. <fieldset>
  31.     <legend>Tambah Data</legend>
  32.     <form method='post'>
  33.         <table>
  34.         <tr><td>Nama Perusahaan</td><td><input type='text' name='nama' required></td></tr>
  35.         <tr><td>Alamat Perusahaan</td><td><input type='text' name='alamat' required></td></tr>
  36.         <tr><td>Tipe Perusahaan</td><td>
  37.             <select name='tipe' required>
  38.                 <option value='1'>Customer</option>
  39.                 <option value='2'>Vendor</option>
  40.             </select>
  41.         </td></tr>
  42.         <tr><td colspan='2'><input type='submit' value='add'></td></tr>
  43.         </table>
  44.     </form>
  45. </fieldset>
  46. <fieldset>
  47.     <legend>Daftar Alamat</legend>
  48.     <?php
  49.     if(!empty($result)) {
  50.         if($result->num_rows>0){
  51.             $code=array('1'=>array('CUS','Customer'),'2'=>array('VEN','Vendor')); //<-- kalo pengin dinamis buatin tabel tersendiri di db
  52.             echo "<table>"
  53.                 ."<tr><th>Kode</th><th>Tipe</th><th>Nama</th><th>Alamat</th></tr>";
  54.             while($r=$result->fetch_object()){
  55.                 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>";
  56.             }
  57.             echo "</table>";
  58.         }
  59.     }
  60.     ?>
  61. </fieldset>
  62. </body>
  63. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement