FORM CRUD Sederhana

inputtgs.php

<DOCTYPE html>

                <html>

                                <head>

                                                <title> Input Data Mahasiswa ke Database dengan PHP dan MySQL </title>

                                 </head>

                <body>

                                <h1>Form Input Data</h1>

                                <?php

                                                if (!empty($_GET[‘message’]) && $_GET[‘message’] == ‘success’) {

                                                echo ‘<h3>Berhasil menambah data!</h3>’;

                                                }

                                ?>

                                <form name=”input_data” action=”inserttgs.php” method=”post”>

                                <table border=”1″ cellpadding=”5″ cellspacing=”0″ style=”background-color: blue;”>

                                <tr>

                                                                <td> NIM </td>

                                                                <td>:</td>

                                                                <td><input type=”text” name=”txtnim” maxlength=”20″ /></td>

                                                </tr>

                                                <tr>

                                                                <td> Nama </td>

                                                                <td>:</td>

                                                                <td><input type=”text” name=”txtnama” maxlength=”20″ /></td>

                                                </tr>

                                                <tr>

                                                                <td> Alamat </td>

                                                                <td>:</td>

                                                                <td><input type=”text” name=”txtalamat” maxlength=”20″ /></td>

                                                </tr>

                                                 <tr>

                                                                <td>Email</td>

                                                                <td>:</td>

                                                                <td><input type=”email” name=”txtemail” /></td>

                                                </tr>

                                                <tr>

                                                                <td>Agama</td>

                                                                <td>:</td>

                                                                <td><input type=”text” name=”txtagama” /></td>

                                                </tr>

                                                <tr>

                                                                <td>Nomor HP</td>

                                                                <td>:</td>

                                                                <td><input type=”text” name=”txthp” maxlength=”14″ /></td>

                                                </tr>

                                                <tr>

                                                                <td align=”right” colspan=”3″><input type=”submit” name=”submit” value=”Simpan” /></td>

                                                </tr>

                                </table>

</form>

<a href=”tampiltgs.php”>Lihat Data</a>

</body>

</html>

koneksitgs.php

<?php

                $host = ‘localhost’; //host yang digunakan

                $user = ‘root’; //username untuk login ke host

                $pass =”; //jika menggunakan PC sendiri sebagai host, secara default password dikosongkan

                $dbname =’universitas’; //isikan nama database sesuai database yang dibuat pada langkah-1

                $connect = mysql_connect($host,$user,$pass) or die(mysql_error()); //mengubung ke host

                $dbselect = mysql_select_db($dbname,$connect); //memilih database yang akan digunakan

?>

Inserttgs.php

<?php

//panggil file koneksitgs.php untuk menghubung ke server

include (‘koneksitgs.php’);

$query= “INSERT INTO

                `universitas`.`mhs`

(

                `mhs`.`nim`

                , `mhs`.`nama`

                , `mhs`.`alamat`

                , `mhs`.`email`

                , `mhs`.`agama`

                , `mhs`.`hp`

)

 VALUES

 (

                 ‘”.$_POST[‘txtnim’].”‘

                 , ‘”.$_POST[‘txtnama’].”‘

                 , ‘”.$_POST[‘txtalamat’].”‘

                 , ‘”.$_POST[‘txtemail’].”‘

                 , ‘”.$_POST[‘txtagama’].”‘

                 , ‘”.$_POST[‘txthp’].”‘

);”;

 $sql_simpan = mysql_query ($query) or die(mysql_error());

 if ($sql_simpan)  {

   header(“location:inputtgs.php?message=success”);

  }

?>

tampiltgs.php

<?php

include(‘koneksitgs.php’);

?>

<html>

<head>

<title> Input Data Mahasiswa ke Database dengan PHP dan MySQL </title>

</head>

<body>

<h1>Data Mahasiswa</h1>

<?php

if (!empty($_GET[‘message’]) && $_GET[‘message’] == ‘success’) {

    echo ‘<h3>Berhasil meng-update data!</h3>’;

}

?>

<a href=”inputtgs.php”>+ Tambah Data</a>

<table border=”1″ cellpadding=”5″ cellspacing=”0″>

    <thead>

        <tr>

            <td>NIM</td>

            <td>Nama</td>

            <td>Alamat</td>

                                                <td>Email</td>

                                                <td>Agama</td>

                                                <td>HP</td>

        </tr>

    </thead>

    <tbody>

    <?php

                $query=mysql_query(“select * from mhs”);

                while($data=mysql_fetch_array($query)) {

                ?>

                <tr>

                                <td><?php echo $data[‘nim’];?></td>

                                <td><?php echo $data[‘nama’];?></td>

                                <td><?php echo $data[‘alamat’];?></td>

                                <td><?php echo $data[’email’];?></td>

                                <td><?php echo $data[‘agama’];?></td>

                                <td><?php echo $data[‘hp’];?></td>

                                <td>

                                                <a href=”edittgs.php?txtnim=<?php echo $data[‘nim’]; ?>”>Edit</a>

                                                || <a href=”deletetgs.php?nim=<?php echo $data[‘nim’]; ?>”>Hapus</a>

                                </td>

        </tr>

    <?php

       }

    ?>

    </tbody>

</table>

</body>

</html>

Leave a Reply

Your email address will not be published. Required fields are marked *