Kumpulan Kode PHP



==============================================
-hapus di php
-Pencarian data
-Ketika tombol submit ditekan
-simpans.php
-mLogout.php
-PLogin.php
-Cek mysql_error dan koneksi php
-home.php
-login.php
-ProsesLogin.php
-logout.php
-add.php
-ProsesAdd.php
-Update.php
-ProsesUpdate.php
-index.php

==============================================
index.php

<!Doctype html>
<html>
<title>sanc</title>
<body>
<?php
$a="dsfa";
echo strtoupper($a);
?>

<table border="1">
<tr>
<td>NO</td>
<td>Nama</td>
<td>ALAMAT</td>
<td>ACTION</td>
</tr>
<?php
mysql_connect("localhost","root","");
mysql_select_db("dbcontoh");
$no=0;
$ambil = mysql_query("select * from tb_siswa");
while ($data = mysql_fetch_array($ambil)){
$no++;
?>
<tr>
<td><?php echo $no?></td>
<td><?php echo $data[1]?></td>
<td><?php echo $data[2]?></td>
<td><a href="update.php?idi=<?php echo $data[0]?>">Update</a></td>
</tr>
<?php
}
?>
</table>
</body>
</html>

ProsesUpdate.php

<?php
mysql_connect("localhost","root","");
mysql_select_db("dbcontoh");
$nama = $_POST['nama'];
$namas = strtoupper($nama);//mw dipake ataw tidak
$alamat = $_POST['alamat'];
$idim = $_POST['ido'];
mysql_query ("update tb_siswa set nama='$namas',alamat='$alamat' where id='$idim'");

?>
<a href="index.php">Back</a>

Update.php
<!Doctype html>
<html>
<title>sanc</title>
<body>
<?php
mysql_connect("localhost","root","");
mysql_select_db("dbcontoh");
$idis = $_GET['idi'];
$no=0;
$ambil = mysql_query("select * from tb_siswa where id= $idis");
while($data = mysql_fetch_array($ambil))
{$no++
?>
<form method="POST" action="prosesUpdate.php">
<table border="1">
<tr>
<td>NO</td>
<td><?= $no?></td>
</tr>
<tr>
<td>NAMA</td>
<td>
<input type="hidden" name ="ido" value="<?php echo $data[0]?>">
<input type="text" name ="nama" value="<?php echo $data[1]?>" onKeyUp="this.value=this.value.toUpperCase()" >
</td>

</tr>
<tr>
<td>ALAMAT</td>
<td><input type="text" name ="alamat" value="<?php echo $data[2]?>" onKeyUp="this.value=this.value.toUpperCase()"></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="SAVE" name="save"></td>
</tr>
<?php
}
?>
</table>
</body>
</html>
==============================================
hapus di php

tampil27.php
<h1><center><b>
DAFTAR NILAI SISWA
</center></b><hr>

<table width="70%" align="center" border="1">
<tr>
<td>NO
<td>NIS
<td>KODE MAPEL
<td>NILAI TUGAS
<td>NILAI UTS
<td>NILAI UAS
<td>AKSI
</tr>
<?
mysql_connect("","","");
mysql_select_db("p27");

$query = mysql_query("select * from nilai");
while($data = mysql_fetch_array($query))
{
$no++;
if($no%2==0)
{$warna = "cyan";}

else
{$warna ="green";}
?>
<tr bgcolor="<? echo $warna ?>">
<td><? echo $no?></td>
<td><? echo $data[0]?></td>
<td><? echo $data[1]?></td>
<td><? echo $data[2]?></td>
<td><? echo $data[3]?></td>
<td><? echo $data[4]?></td>
<td><a href="hapus27.php?nis=<? echo $data[0]?> &kdmp=<? echo $data[1]?> &ntugas=<? echo $data[2]?> &uts=<? echo $data[3]?> &nuas=<? echo $data[4]?>">Hapus</a></td>
<?
$no++;
} ?></tr></table>


hapus27.php

<?
mysql_connect("","","");
mysql_select_db("p27");

$nis = $_GET['nis'];
$kodemp = $_GET['kdmp'];

mysql_query("delete from nilai where nis='$nis' and kdmp='$kodemp'");
?>
<meta http-equiv="refresh" content="1;tampil27.php">



==============================================
Pencarian data
index.php

<!Doctype html>
<html>
<head>
<title></title>
</head>
<body>
<form method="POST" action="index.php">
<table>
<tr>
<td>Masukan data yang dicari</td>
</tr>
<tr>
<td><input type="text" placeholder="masukan nama yang dicari" name="nama"></td>
</tr>
<tr>
<td><input type="submit" value="CARI" name="submit"></td>
</tr>
</table>
</form>

</body>
</html>

<?php
if (isset($_POST['submit']))
{

include"prosesCari.php";

}
?>

prosesCari.php



<table border="1">
<tr>
<th>no</th>
<th>nama</th>
<th>nilai</th>
<th>mata kuliah</th>
</tr>

<?php
$nama=$_POST['nama'];
mysql_connect("localhost","root","");
mysql_select_db("dbdata");

$ambil = mysql_query("select tb_siswa.nama,tb_nilai.nilai,tb_makul.matakul
from tb_siswa,tb_nilai,tb_makul
where tb_siswa.id=tb_nilai.id_siswa  and
tb_makul.kode=tb_nilai.kode_makul and tb_siswa.nama='$nama'");
$no=0;
$hasil1=mysql_num_rows($ambil);
while ($data=mysql_fetch_array($ambil))
{
$no++;

?>
<tr>
<td><?= $no;?></td>
<td><?= $data[0]?></td>
<td><?= $data[1]?></td>
<td><?= $data[2]?></td>

</tr>

<?php } ?>
</table>
<?php

if($hasil1==0)
echo "<script>alert('Data not found')</script>";
?>


==============================================
Ketika tombol submit ditekan;

<!Doctype html>
<html>
<head>
<title></title>
</head>
<body>
<form method="POST" action="index.php">
<table>
<tr>
<td>Masukan data yang dicari</td>
</tr>
<tr>
<td><input type="text" placeholder="masukan nama yang dicari" name="nama"></td>
</tr>
<tr>
<td><input type="submit" value="CARI" name="submit"></td>
</tr>
</table>
</form>

</body>
</html>
<?php
if(isset($_POST['submit']))
{
$nama=$_POST['nama'];
echo $nama;
}
?>
==============================================
Pertemuan 22
simpans.php

<?php
mysql_connect("","","");
mysql_select_db("barumalam");
$nis = $_POST['t1'];
$nama = $_POST['t2'];
$alamat = $_POST['t3'];
$kelas = $_POST['t4'];
$tgl = $_POST['tgl'];
$bln = $_POST['bln'];
$thn = $_POST['thn'];

$tlahir = "$thn-$bln-$tgl";
if($nis=="" or $nama =="" or $alamat=="" or $kelas == "" or thn=="")
{
echo "<script>alert('Data tidak boleh kosong')</script>";
exit;
include "inputs.php";
}
else{
$cari = mysql_query("select * from siswa where nis='$nis'");
$jlh = mysql_num_rows($cari);
if($jlh>0){
echo "<h3>Data nis sudah ada </h3>";
exit;
include "inputs.php";
}else{
echo "<h3>Data berhasil diinput </h3>";
mysql_query("insert into siswa values ('$nis','$nama','$alamat','$kelas','$tlahir')");


}
}

?>
<a href="inputs.php">INPUT SISWA</a>;


==============================================
Pertemuan 21
mLogout.php

<?php
session_start();
session_destroy();
echo "Anda berhasil keluar ....!! ";
?>
<meta http-equiv="refresh" content="1;FLogin.php">



PLogin.php

<?php
session_start();
mysql_connect("","","");
mysql_select_db('php21');
$user = $_POST['tuser'];
$pass = $_POST['tpass'];
$cari = mysql_query("select * from admin where user='$user' and pass='$pass'");
$uji = mysql_num_rows($cari);
if($uji<=0)
{
echo "<H2 align='center'>Username dan password yang anda masukan salah </H2>";
include "FLogin.php";
exit;
}
else
{
$_SESSION['sukses']=$user;
echo "Anda berhasil Login ...";

}

?>

<meta http-equiv="refresh" content="2;mLogin.php">




==============================================
Form 5
Cek mysql_error dan koneksi php

index.php

<?php
include "login.php";
?>

conn.php

<?php
mysql_connect("localhost","root","")or die(mysql_error());
mysql_select_db("dbData") or die(mysql_error());
?>

home.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<?php session_start();
if(isset($_SESSION['user'])){

?>
<body>
<h1><center>Selamat datang <?php echo $_SESSION['user']?></center></h1>


<p>&nbsp;</p>
<table width="427" border="1" align="center">
  <tr>
    <td width="37">No</td>
    <td width="65">Nama</td>
    <td width="303">Alamat</td>
  </tr>
  <?php
  include "conn.php";
  $no=0;
  $tampil= mysql_query("select * from tbusers");
  while($data= mysql_fetch_array($tampil))
  {
  $no++;
  ?>
  <tr>
    <td><?php echo $no ?></td>
    <td><?php echo $data[1]?></td>
    <td><?php echo $data[2]?></td>
  </tr>
  <?php } }else{ header("location: index.php");}?>
</table>
<p>&nbsp;</p>
<form id="form1" name="form1" method="post" action="add.php">
  <input type="submit" name="button" id="button" value="ADD" />
</form>
<form id="form2" name="form2" method="post" action="logout.php">
  <input type="submit" name="button2" id="button2" value="Log out" />
</form>
<p>&nbsp;</p>
<p>&nbsp;</p>
</body>
</html>

login.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<form id="form1" name="form1" method="post" action="ProsesLogin.php">
  <h1><CENTER>
    <p>&nbsp;</p>
    <p>SELAMAT DATANG.</p>
  </center>
  </h1>
  <p>&nbsp;</p>
  <table width="250" border="1" align="center">
    <tr>
      <td width="92">username</td>
      <td width="142"><label for="t1"></label>
      <input type="text" name="t1" id="t1" /></td>
    </tr>
    <tr>
      <td>Password</td>
      <td><input type="password" name="t2" id="t2" /></td>
    </tr>
    <tr>
      <td colspan="2"><input type="submit" name="button" id="button" value="Login" /></td>
    </tr>
  </table>
  <p>&nbsp;</p>
  <p>&nbsp;</p>
  <p>&nbsp;</p>
</form>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
</body>
</html>

ProsesLogin.php

<?php
session_start();
$u=$_POST['t1'];
$p=$_POST['t2'];
if ($p=='darat'){
$_SESSION['user']=$u;
echo $_SESSION['user'];
header("location: home.php");
}else{
header("location: index.php");
}

?>

logout.php

<?php
session_start();
if(isset($_SESSION['user'])){
unset($_SESSION['user']);
session_destroy();
header("location: index.php");
}else{
echo "Gagal ";
}
?>

add.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<form id="form1" name="form1" method="post" action="ProsesAdd.php">
  <p>&nbsp;</p>
  <table width="200" border="1">
    <tr>
      <td>Nama</td>
      <td><label for="t1"></label>
      <input type="text" name="t1" id="t1" /></td>
    </tr>
    <tr>
      <td>Alamat</td>
      <td><label for="t2"></label>
      <input type="text" name="t2" id="t2" /></td>
    </tr>
    <tr>
      <td colspan="2"><input type="submit" name="button" id="button" value="ADD" /></td>
    </tr>
  </table>
  <p>&nbsp;</p>
  <p>&nbsp;</p>
</form>
</body>
</html>

ProsesAdd.php

<?php
include "conn.php";
$nama=$_POST['t1'];
$alamat=$_POST['t2'];
mysql_query("insert into tbusers values('','$nama','$alamat')") or die(mysql_error());
header("location: home.php");
?>




REFERENSI LAIN https://www.dumetschool.com/blog/Cara-Mengambil-Data-Dari-Database-Menggunakan-PHP


include("index.php");
header("location: index.php");


==============================================







Kumpulan Kode PHP Kumpulan Kode PHP Reviewed by Leo on 09:07 Rating: 5

No comments