Rabu, 10 Desember 2014

Contoh script menggunakan database



Insert
<?php
//panggil file config.php untuk menghubung ke server
include('config.php');

//tangkap data dari form
$Username = $_POST['Username'];
$Password = $_POST['Password'];
$Fullname = $_POST['Fullname'];
$Email = $_POST['Email'];
$Agama = $_POST['Agama'];
$No_tlp = $_POST['No_tlp'];

//simpan data ke database
$query = mysql_query("insert into usera values('', '$Username', '$Password', '$Email', '$Fullname', '$Agama', '$No_tlp')") or die(mysql_error());

if ($query) {
    header('location:index.php?message=success');
}
?>




Index
<html>
<head>
<title>Belajar PHP</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="insert.php" method="post">


<form name="input_data" action="insert.php" method="post">
<table border="0" cellpadding="5" cellspacing="0">
    <tbody>
        <tr>
            <td>Username</td>
            <td>:</td>
            <td><input type="text" name="Username" maxlength="20" required="required" /></td>
        </tr>
        <tr>
            <td>Password</td>
            <td>:</td>
            <td><input type="password" name="Password" maxlength="20" required="required" /></td>
        </tr>
        <tr>
            <td>Fullname</td>
            <td>:</td>
            <td><input type="text" name="Fullname" maxlength="100" required="required" /></td>
        </tr>
        <tr>
            <td>Email</td>
            <td>:</td>
            <td><input type="email" name="Email" required="required" /></td>
        </tr>
        <tr>
            <td>Agama</td>
            <td>:</td>
            <td><input type="text" name="Agama" required="required" /></td>
        </tr>
        <tr>
            <td>Nomor HP</td>
            <td>:</td>
            <td><input type="text" name="No_tlp" maxlength="14" required="required" /></td>
        </tr>
        <tr>
            <td align="right" colspan="3"><input type="submit" name="submit" value="Simpan" /></td>
        </tr>
    </tbody>
</table>
</form>

<a href="view.php">Lihat Data</a>

</body>
</html>










View
<?php
include('config.php');
?>

<html>
<head>
<title>Belajar PHP</title>
</head>

<body>
<h1>Data User</h1>
 <?php
if (!empty($_GET['message']) && $_GET['message'] == 'success') {
    echo '<h3>Berhasil meng-update data!</h3>';
}
?>

<a href="index.php">+ Tambah Data</a>

<table border="1" cellpadding="5" cellspacing="0">
    <thead>
        <tr>
            <td>No.</td>
            <td>Username</td>
            <td>Password</td>
            <td>Email</td>
            <td>Fullname</td>
            <td>Agama</td>
            <td>No.tlp</td>
            <td>Opsi</td>
        </tr>
    </thead>
    <tbody>
    <?php
    $query = mysql_query("select * from usera");

    $no = 1;
    while ($data = mysql_fetch_array($query)) {
    ?>
        <tr>
            <td><?php echo $no; ?></td>
            <td><?php echo $data['Username']; ?></td>
            <td><?php echo $data['Password']; ?></td>
            <td><?php echo $data['Email']; ?></td>
            <td><?php echo $data['Fullname']; ?></td>
            <td><?php echo $data['Agama']; ?></td>
            <td><?php echo $data['No_tlp']; ?></td>
          <td>
    <a href="edit.php?id=<?php echo $data['Userid']; ?>">Edit</a> ||
    <a href="delete.php?id=<?php echo $data['Userid']; ?>">Hapus</a>
</td>

                          
        </tr>
    <?php
        $no++;
    }
    ?>
    </tbody>
</table>
</body>
</html>












Edit
<?php
include('config.php');
?>

<html>
<head>
<title>Belajar PHP</title>
</head>

<body>
<h1>Form Input Data</h1>

<?php
$id = $_GET['id'];

$query = mysql_query("select * from usera where Userid='$id'") or die(mysql_error());

$data = mysql_fetch_array($query);
?>

<form name="update_data" action="update.php" method="post">
<input type="hidden" name="Userid" value="<?php echo $id; ?>" />
<table border="0" cellpadding="5" cellspacing="0">
    <tbody>
        <tr>
            <td>Username</td>
            <td>:</td>
            <td><input type="text" name="Username" maxlength="20" required="required" value="<?php echo $data['Username']; ?>" disabled /></td>
        </tr>
        <tr>
            <td>Password</td>
            <td>:</td>
            <td><input type="password" name="Password" maxlength="20" required="required" value="<?php echo $data['Password']; ?>" /></td>
        </tr>
        <tr>
            <td>Fullname</td>
            <td>:</td>
            <td><input type="text" name="Fullname" maxlength="100" required="required" value="<?php echo $data['Fullname']; ?>" /></td>
        </tr>
        <tr>
            <td>Email</td>
            <td>:</td>
            <td><input type="email" name="Email" required="required" value="<?php echo $data['Email']; ?>" /></td>
        </tr>
        <tr>
            <td>Agama</td>
            <td>:</td>
            <td><input type="text" name="Agama" required="required" value="<?php echo $data['Agama']; ?>" /></td>
        </tr>
        <tr>
            <td>Nomor HP</td>
            <td>:</td>
            <td><input type="text" name="No_tlp" maxlength="14" required="required" value="<?php echo $data['No_tlp']; ?>" /></td>
        </tr>
        <tr>
            <td align="right" colspan="3"><input type="submit" name="submit" value="Simpan" /></td>
        </tr>
    </tbody>
</table>
</form>

<a href="view.php">Lihat Data</a>

</body>
</html>





Update
<?php
include('config.php');

//tangkap data dari form
$id = $_POST['Userid'];
$Password = $_POST['Password'];
$Fullname = $_POST['Fullname'];
$Email = $_POST['Email'];
$Agama = $_POST['Agama'];
$No_tlp = $_POST['No_tlp'];

//update data di database sesuai user_id
$query = mysql_query("update usera set Password='$Password', Fullname='$Fullname', Email='$Email', Agama='$Agama', No_tlp='$No_tlp' where Userid='$id'") or die(mysql_error());

if ($query) {
    header('location:view.php?message=success');
}
?>





Delete
<?php
include('config.php');

$id = $_GET['id'];

$query = mysql_query("delete from usera where Userid='$id'") or die(mysql_error());

if ($query) {
    header('location:view.php?message=delete');
}
?>

Tidak ada komentar:

Posting Komentar