Read 190 times | Created 2013-04-04 08:22:14 | Updated 2013-04-04 09:17:24 | | |

 

<?php  
/*********************************  
FILENAME  : daftar.php  
CREATE BY  : cahya dsn  
PURPOSE   : script pendaftaran
CREATE DATE : 2013-04-04 
**********************************  
#table creation  

USE `test`;  
DROP TABLE IF EXISTS `jurusan`;  
CREATE TABLE IF NOT EXISTS `jurusan` (  
 `id_jur` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT 'primary key untuk table jurusan',  
 `jurusan` varchar(30) NOT NULL COMMENT 'nama jurusan',  
 PRIMARY KEY(`id_jur`)  
) ENGINE=MyISAM COMMENT='tabel untuk menyimpan data jurusan';  

INSERT INTO `jurusan`(`id_jur`,`jurusan`) VALUES  
(NULL,'Teknik Elektro'),
(NULL,'Teknik Mesin'),
(NULL,'Teknik Seni Rupa'),
(NULL,'Teknik Busana'),
(NULL,'Teknik Boga'),
(NULL,'Teknik Kayu'),
(NULL,'Teknik Komputer');


DROP TABLE IF EXISTS `pendaftar`;  
CREATE TABLE IF NOT EXISTS `pendaftar` (  
 `id` int(1) unsigned NOT NULL AUTO_INCREMENT COMMENT 'primary key untuk table pendaftar',  
 `nama` varchar(30) NOT NULL COMMENT 'nama personel',  
 `id_jur` int(11) unsigned NOT NULL,  
 PRIMARY KEY(`id`)  
) ENGINE=MyISAM COMMENT='tabel untuk menyimpan data pendaftar';  
*/
//database configuration  
$dbhost='localhost';  
$dbuser='root';  
$dbpass='';  
$dbname='test';  
//database connection  
$db=new mysqli($dbhost,$dbuser,$dbpass,$dbname);
$sql="SELECT id_jur,jurusan FROM jurusan 
      LEFT JOIN pendaftar USING (id_jur)
      GROUP BY id_jur 
      HAVING COUNT(id)<30";
$result=$db->query($sql);  
?>
<!DOCTYPE html>  
<html>  
<head>  
<title>Pendaftaran</title> 
</head>
<body>
<h3>PENDAFTARAN</h3>
<form method="POST">
Nama <br>
<input type="text" name="nama" /><br>
Jurusan<br>
<select name="id_jur">
<?php
while($records=$result->fetch_object()){  
  echo "<option value='".$records->id_jur."'>".$records->jurusan."</option>";
}
$result->close();
?>
</select><br>
<input type="submit" name="submit" value="kirim">
</form>
<table>
<tr>
  <th>No</th><th>Nama<th><th>Jurusan</th>
<tr>
<?php
if(isset($_POST['submit'])){
  $sql="INSERT INTO pendaftar(`nama`,`id_jur`) VALUES ('{$_POST['nama']}','{$_POST['id_jur']}')";
  $result=$db->query($sql);
  $sql="SELECT nama,jurusan FROM pendaftar LEFT JOIN jurusan USING(id_jur)";
  $result=$db->query($sql);
  $i=1;
  while($records=$result->fetch_object()){ 
    echo "<tr><td>".($i++)."</td><td>{$records->nama}</td><td>{$records->jurusan}</td></tr>n";
  }
  $result->close();
  $db->close();
}
?>
</table>
</body>
</html>