Minggu, 20 Mei 2012

cara membuat tabel pada database

MEMBUAT TABEL DI DATABASE MY-SQL
<?php
mysql_connect("localhost", "root", "root") or die(mysql_error());
mysql_select_db("test") or die(mysql_error());
mysql_query
("CREATE TABLE example (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
nama VARCHAR(21) NULL, npm INT NULL, kelas VARBIT NULL)")
or die(mysql_error());
echo "Tabel sudah dibuat";
?>


MENGINPUT DATA DI TABEL
<?php
mysql_connect("localhost", "root", "root") or die(mysql_error());
mysql_select_db("test") or die(mysql_error());
mysql_query("INSERT INTO example(nama, npm, kelas)
VALUES('alia adelina lutfiani', 10111599, ‘1ka28’)");
mysql_query("INSERT INTO example(nama, npm, kelas)
VALUES('friandini', 1815115395, ‘1ka28’)");
echo "Data sudah dimasukkan";
?>

MENAMPILKAN DATA PADA DATABASE
<?php
mysql_connect("localhost", "root", "root") or die(mysql_error());
mysql_select_db("test") or die(mysql_error());
$hasil = mysql_query("SELECT * FROM example")
or die(mysql_error());
while ($record = mysql_fetch_array($hasil))
{
echo "Nama : ".$record['nama']. "<br>";
echo "NPM : ".$record['npm']. "<br><br>";
echo "Kelas : ".$record['kelas']. "<br>"<br><br>";
}
?>

MEMBUAT FORM INPUTAN KE DATABASE
<body>
<h3>
<strong>INPUTAN DATA</strong>
</h3>
<table width="200" border="1">
<form name="form1" method="post" action="proses.php">
<tr>
<td> Nama</td>
<td> <input type="text" name="nama"> </td>
</tr>
<tr>
<td>NPM</td>
<td><input type="text" name="npm"></td>
</tr>
<tr>
<td>Kelas</td>
<td><input type="text" name="kelas"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td> <input type="submit" name="Submit"
value="Submit">
</td>
</tr>
</form>
</table>
</body>

FILE KONEKSI DAPAT DI PISAH DALAM FILE LAIN SELANJUTNYA DAPAT DI PANGGIL
<?
mysql_connect("localhost", "root", "root") or die(mysql_error());
mysql_select_db("test") or die(mysql_error());
?>

MENERIMA INPUTAN DARI FORM
<?
$nama = $_POST['nama'];
$npm = $_POST['npm'];
$kelas = $_POST['kelas'];
include "database.php";
mysql_query("INSERT INTO example(nama, npm, kelas)
VALUES('$nama', $npm, $kelas)");
echo "Data sudah dimasukkan";
?>

MENAMPILKAN DATA PADA DATABASE DALAM BENTUK TABEL
<?php
include"database.php";
$hasil = mysql_query("SELECT * FROM example")
or die(mysql_error());
echo("<table width=20% cellspacing=1 cellpadding=2 align=left bgcolor=#000000>");
echo("<tr><td bgcolor=#CCCCCC>Nama</td><td
bgcolor=#CCCCCC>npm</td></tr
bgcolor=#CCCCCC>kelas</td></tr>");
while ($record = mysql_fetch_array($hasil))
{
echo("<tr><td bgcolor=#FFFFFF>".$record['nama']."</td><td
bgcolor=#FFFFFF>".$record['npm']."</td> </tr
bgcolor=#FFFFFF>".$record['kelas']."</td> </tr>");
}
echo("</table>");
?>

Tidak ada komentar:

Posting Komentar

alia_adelinalutfiani@yahoo.com