Source code dibawah ini adalah code untuk membuat halaman form seperti gambar diatas, saya beri nama form.php.
<html>
<title>Input Database Dari Form</title>
<body>
<form action='inputProses.php' method='post'>
<table border='0' cellpadding='3' cellspacing='3' align='center'>
<caption><strong>Daftar Mahasiswa Fakultas Teknologi Informasi</strong></caption><br/>
<tr><td colspan='3'>=======================================</td></tr>
<tr><td>Nama</td><td>:</td><td><input type='text' name='nama'></td></tr>
<tr><td>NRP</td><td>:</td><td><input type='text' name='nrp'></td></tr>
<tr><td>Jurusan</td><td>:</td><td>
<select name='jurusan'>
<option value='IT'>IT</option>
<option value='SI'>SI</option>
<option value='D3 IT'>D3 IT</option>
<option value='EXT IT'>Ext IT</option>
<option value='EXT SI'>Ext SI</option></td></tr>
<tr><td>Jenis Kelamin</td><td>:</td><td><label for='kelamin'><input type='radio' name='kelamin' value='Pria'>Pria</label><label for='kelamin'><input type='radio' name='kelamin' value='Wanita'>Wanita</label></td></tr>
<tr><td>Tahun Tahir</td><td>:</td><td>
<select name='tahun'>
<option value='1980'>1980</option>
<option value='1981'>1981</option>
<option value='1982'>1982</option>
<option value='1983'>1983</option>
<option value='1984'>1984</option>
<option value='1985'>1985</option>
<option value='1986'>1986</option>
<option value='1987'>1987</option>
<option value='1988'>1988</option>
<option value='1989'>1989</option>
<option value='1990'>1990</option>
<option value='1991'>1991</option>
<option value='1992'>1992</option>
<option value='1993'>1993</option>
<option value='1994'>1994</option>
<option value='1995'>1995</option></td></tr>
<tr><td>Alamat</td><td>:</td><td><textarea cols='20' rows='5' name='alamat'></textarea></td></tr>
<tr><td></td><td><input type='submit' name='kirim' value='kirim'></td></tr>
</table>
</form>
</body>
</html>
<title>Input Database Dari Form</title>
<body>
<form action='inputProses.php' method='post'>
<table border='0' cellpadding='3' cellspacing='3' align='center'>
<caption><strong>Daftar Mahasiswa Fakultas Teknologi Informasi</strong></caption><br/>
<tr><td colspan='3'>=======================================</td></tr>
<tr><td>Nama</td><td>:</td><td><input type='text' name='nama'></td></tr>
<tr><td>NRP</td><td>:</td><td><input type='text' name='nrp'></td></tr>
<tr><td>Jurusan</td><td>:</td><td>
<select name='jurusan'>
<option value='IT'>IT</option>
<option value='SI'>SI</option>
<option value='D3 IT'>D3 IT</option>
<option value='EXT IT'>Ext IT</option>
<option value='EXT SI'>Ext SI</option></td></tr>
<tr><td>Jenis Kelamin</td><td>:</td><td><label for='kelamin'><input type='radio' name='kelamin' value='Pria'>Pria</label><label for='kelamin'><input type='radio' name='kelamin' value='Wanita'>Wanita</label></td></tr>
<tr><td>Tahun Tahir</td><td>:</td><td>
<select name='tahun'>
<option value='1980'>1980</option>
<option value='1981'>1981</option>
<option value='1982'>1982</option>
<option value='1983'>1983</option>
<option value='1984'>1984</option>
<option value='1985'>1985</option>
<option value='1986'>1986</option>
<option value='1987'>1987</option>
<option value='1988'>1988</option>
<option value='1989'>1989</option>
<option value='1990'>1990</option>
<option value='1991'>1991</option>
<option value='1992'>1992</option>
<option value='1993'>1993</option>
<option value='1994'>1994</option>
<option value='1995'>1995</option></td></tr>
<tr><td>Alamat</td><td>:</td><td><textarea cols='20' rows='5' name='alamat'></textarea></td></tr>
<tr><td></td><td><input type='submit' name='kirim' value='kirim'></td></tr>
</table>
</form>
</body>
</html>
Untuk source code yang dibawah ini adalah untuk menginput ke database MySQL, saya beri nama inputProses.php.
<?php
include "koneksi.php";
$nama = $_POST['nama'];
$nrp = $_POST['nrp'];
$jurusan = $_POST['jurusan'];
$kelamin = $_POST['kelamin'];
$tahun = $_POST['tahun'];
$alamat = $_POST['alamat'];
$insert = "INSERT INTO data_mhs (nama, nrp, jurusan, kelamin, tahun, alamat) VALUES('$nama','$nrp','$jurusan','$kelamin','$tahun','$alamat')";
$insert_query = mysql_query($insert);
if($insert_query){
echo "<script>alert('Insert data $nama berhasil..!!')</script>";
}
else{
echo "<script>alert('Gagal insert $nama')</script>";
echo mysql_error();
}
mysql_close($koneksi);
?>
include "koneksi.php";
$nama = $_POST['nama'];
$nrp = $_POST['nrp'];
$jurusan = $_POST['jurusan'];
$kelamin = $_POST['kelamin'];
$tahun = $_POST['tahun'];
$alamat = $_POST['alamat'];
$insert = "INSERT INTO data_mhs (nama, nrp, jurusan, kelamin, tahun, alamat) VALUES('$nama','$nrp','$jurusan','$kelamin','$tahun','$alamat')";
$insert_query = mysql_query($insert);
if($insert_query){
echo "<script>alert('Insert data $nama berhasil..!!')</script>";
}
else{
echo "<script>alert('Gagal insert $nama')</script>";
echo mysql_error();
}
mysql_close($koneksi);
?>
Demikian tutorial cara menginput ke database MySQL pada PHP, semoga bermanfaat.
GBU.
0 comments:
Posting Komentar