Belajar
Membuat Form Input, Hapus, Ubah dan Simpan Data
Form Input Data
Membuat InputForm.php
<html>
<head>
<title>Input
Data</title>
</head>
<?
include_once("dbsiswa.php");
?>
<body>
<form
action="Input_Form_Saving.php" method="post" >
<table width="415"
border="1">
<tr>
<td colspan="2">Masukkan
Data Siswa </td>
</tr>
<tr>
<td width="95">Nis Siswa
</td>
<td width="304">:
<input name="TxtNis"
type="text"
size="10" maxlength="4"></td>
</tr>
<tr>
<td>Nama</td>
<td>:
<input name="TxtNama"
type="text" size="30"
maxlength="35"></td>
</tr>
<tr>
<td>Alamat</td>
<td>:
<input name="TxtAlamat"
type="text" size="30"
maxlength="60"></td>
</tr>
<tr>
<td>Kelamin</td>
<td>:
<input name="RbKelamin"
type="radio" value="P">
Pria
<input name="RbKelamin"
type="radio" value="W">
Wanita</td>
</tr>
<tr>
<td> </td>
<td><input
type="submit" name="Submit" value="Simpan"></td>
</tr>
</table>
</form>
</body>
</html>
Cara
menyambungkan dengan database
Membuat dbsiswa.php
<?php
$hostname="localhost";
$user="root";
$pass="";
$db_name="siswa";
$koneksi=mysql_connect($hostname,$user,$pass)
or
die ("Gagal Konek ke server ".mysql_error());
if($koneksi)
{
mysql_select_db($db_name,$koneksi)
or die ("Database gagal dibuka".mysql_error());
}
?>
Membuat Input_Form_Saving.php
<?php
include_once("dbsiswa.php");
$TxtNis=$_POST[TxtNis];
$TxtNama=$_POST[TxtNama];
$TxtAlamat=$_POST[TxtAlamat];
$RbKelamin=$_POST[RbKelamin];
$sql="INSERT
INTO identitas (nis,nama,alamat,kelamin)
VALUES ('$TxtNis','$TxtNama','$TxtAlamat','$RbKelamin')";
mysql_query($sql,
$koneksi)
or
die ("Gagal disimpan : ".mysql_error());
echo "Data berhasil disimpan ke database!";
?>
<html>
<body>
<br>
<br>
<a
href="Input_Form.php"> Masukkan Data Siswa Lagi...</a>
</body>
</html>
Form Hapus Data
Membuat
HapusTampil.php
<html>
<head>
<title>Akses
MySQL</title>
</head>
<body>
<table
width="719" border="1">
<tr>
<td colspan="4"><span
class="style3">TAMPIL DATA ANGGOTA </span></td>
</tr>
<tr>
<td width="72">NIS Siswa
</td>
<td
width="232">Nama</td>
<td
width="323">Alamat</td>
<td width="64"><div
align="center">Tombol</div></td>
</tr>
<?php
|
or
die ("Gagal Konek ke server ".mysql_error());
mysql_select_db("siswa",$koneksi)
or die ("Database gagal dibuka".mysql_error());
$sql = "SELECT * FROM identitas ";
$qry = mysql_query($sql, $koneksi)
or die ("Query gagal ".mysql_error());
while
($data=mysql_fetch_array($qry)) {
?>
<tr>
<td width="71"><?php
echo $data['nis'];
?></td>
<td width="234"><?php
echo $data['nama']; ?></td>
<td width="323"><?php
echo $data['alamat']; ?></td>
<td align="center"
width="63"><a href="HapusData.php?nis=<?php
echo $data['nis'];
?>"> Hapus</a></td>
</tr>
<?php } ?>
</table>
</body>
</html>
Menghapus Data
di Database
Membuat
HapusData.php
<html>
<head>
<title>Akses
MySQL</title>
</head>
<body>
<?php
$koneksi=mysql_connect("localhost","root","")
|
if
($koneksi) {
mysql_select_db("siswa",$koneksi)
or die ("Database gagal dibuka".mysql_error());
$nis = $_GET['nis'];
$sql
= "DELETE FROM identitas WHERE nis='$nis'";
$qry
= mysql_query($sql, $koneksi)
or die ("Query gagal ".mysql_error());
if ($qry) {
echo
"Data berhasil dihapus";
include
"HapusTampil.php";
}
else
echo
"Gagal Menghapus";
exit;
} --- dihilangkan
?>
</body>
</html>
Mengubah Data
Membuat
UbahTampil.php
<html>
<head>
<title>Akses
MySQL</title>
</head>
<body>
<table
width="719" border="1">
<tr>
<td colspan="4"><span
class="style3">TAMPIL DATA ANGGOTA </span></td>
</tr>
<tr>
<td width="72">NIS Siswa
</td>
<td
width="232">Nama</td>
<td
width="323">Alamat</td>
<td width="64"><div
align="center">Tombol</div></td>
</tr>
<?php
$koneksi=mysql_connect("localhost","root","")
|
mysql_select_db("siswa",$koneksi)
or die ("Database gagal dibuka".mysql_error());
$sql = "SELECT * FROM identitas ";
$qry = mysql_query($sql, $koneksi)
or die ("Query gagal ".mysql_error());
while
($data=mysql_fetch_array($qry)) {
?>
<tr>
<td width="71"><?php
echo $data['nis'];
?></td>
<td width="234"><?php
echo $data['nama']; ?></td>
<td width="323"><?php
echo $data['alamat']; ?></td>
<td align="center"
width="63"><a href="UbahForm.php?nis=<?php
echo $data['nis'];
?>"> Ubah</a></td>
</tr>
<?php } ?>
</table>
</body>
</html>
Membuat Form
Ubah Data
Membuat UbahForm.php
<?php
$koneksi=mysql_connect("localhost","root","")
|
if
($koneksi) {
mysql_select_db("siswa",$koneksi)
or die ("Database gagal dibuka".mysql_error());
$nis = $_GET['nis'];
$sql
= "SELECT * FROM identitas WHERE nis='$nis'";
$qry
= mysql_query($sql, $koneksi)
or die ("Query gagal ".mysql_error());
$identitas=
mysql_fetch_array($qry);
$nis = $identitas['nis'];
$nama= $identitas['nama'];
$alamat= $identitas['alamat'];
if ($identitas['kelamin']=="P") {
$cekp
= "checked";
$cekw = "";
}
else
{
$cekp = "";
$cekw = "checked";
}
}
?>
<html>
<head>
<title>Input
Data</title>
</head>
<body>
<form
action="UbahSimpan.php" method="post" >
<table width="415"
border="1">
<tr>
<td colspan="2">Ubah Data
Siswa </td>
</tr>
<tr>
<td width="95">Nis Siswa
</td>
<td width="304">:
<input name="TxtNis"
type="text"
value="<?php echo "$nis";
?>" size="10" maxlength="4" disabled>
<input name="IDH" type="hidden"
value="<?php echo "$nis";
?>">
</td>
</tr>
<tr>
<td>Nama</td>
<td>:
<input name="TxtNama"
type="text" size="30" value="<?php echo
"$nama"; ?>" maxlength="35" ></td>
</tr>
<tr>
<td>Alamat</td>
<td>:
<input name="TxtAlamat"
type="text" size="30" value="<?php echo
"$alamat"; ?>" maxlength="60"></td>
</tr>
<tr>
<td>Kelamin</td>
<td>:
<input name="RbKelamin"
type="radio" value="P" <?php echo "$cekp";
?>>
Pria
<input name="RbKelamin"
type="radio" value="W" <?php echo "$cekw";
?>>
Wanita</td>
</tr>
<tr>
<td> </td>
<td><input
type="submit" name="Submit"
value="Simpan"></td>
</tr>
</table>
</form>
</body>
</html>
Menyimpan data
yang sudah diubah ke Database
Membuat UbahSimpan.php
<?php
$koneksi=mysql_connect("localhost","root","")
|
if
($koneksi) {
mysql_select_db("siswa", $koneksi)
or die ("Database gagal dibuka".mysql_error());
$TxtNis
=$_POST['TxtNis'];
$TxtNama=$_POST['TxtNama'];
$TxtAlamat=$_POST['TxtAlamat'];
$RbKelamin=$_POST['RbKelamin'];
$sql = "UPDATE identitas SET
nama='$TxtNama',
alamat='$TxtAlamat',
kelamin='$RbKelamin'
WHERE nis='$IDH'";
mysql_query($sql,
$koneksi)
or die ("Gagal diubah :
".mysql_error());
echo
"Data berhasil diubah ke database";
include "UbahTampil.php";
}
?>
Tidak ada komentar:
Posting Komentar