Tugas Framework 1 CRUD Data Pasien dengan MYSQLI

Kamis, 27 September 2012

Kelompok:

  1. Damar Wisnu Candra P (09018036) pinginaja.blogspot.com
  2. Sandra Refya Irawan (09018047) dhudhu-koplak.blogspot.com
  3. Niko Adhy Prasetyo (09018082) blog.uad.ac.id/nicoadhy
  4. Efawan Rezha Arshandy (09018031)
  5. Andri Susanto (09018066) blog.uad.ac.id/nurandrisusanto

Form Input 

source code:


</strong>

<html>

<head>

<title>MySQLi Create Record</title>

</head>

<body>

<?php

//if there's any user action

$action = isset($_POST['action']) ? $_POST['action'] : "";

if($action=='create'){ //the the user submitted the form

//include database connection

include 'conn.php';

//our insert query query

//$mysqli->real_escape_string() function helps us prevent attacks such as SQL injection

$query = "insert into pasien_

set

nama = '".$mysqli->real_escape_string($_POST['nama'])."',

alamat = '".$mysqli->real_escape_string($_POST['alamat'])."',

usia = '".$mysqli->real_escape_string($_POST['usia'])."',

telp  = '".$mysqli->real_escape_string($_POST['telp'])."',

keluhan  = '".$mysqli->real_escape_string($_POST['keluhan'])."'

penanggung = '".$mysqli->real_escape_string($_POST['penanggung'])."'";

//execute the query

if( $mysqli->query($query) ) {

//if saving success

echo "User was created ";

}else{

//if unable to create new record

echo "Database Error: Unable to create record.";

}

//close database connection

$mysqli->close();

}

?>

<!--we have our html form here where user information will be entered-->
<h1>Input Data Pasien</h1>
<form action='#' method='post' border='0'>

<table>

<tr>

<td>Nama</td>

<td><input type='text' name='nama' /></td>

</tr>

<tr>

<td>Alamat</td>

<td><input type='text' name='alamat' /></td>

</tr>

<tr>

<td>Usia</td>

<td><input type='text' name='usia' /></td>

</tr>

<tr>

<td>Telpon</td>

<td><input type='text' name='telp' /></td>

</tr>
 <td>Keluhan</td>

<td><input type='text' name='keluhan' /></td>

</tr>

<td>Penanggung Jawab</td>

<td><input type='text' name='penanggung' /></td>

</tr>
<td></td>

<td>

<input type='hidden' name='action' value='create' />

<input type='submit' value='Save' />

<a href='index.php'>Back to index</a>

</td>

</tr>

</table>

</form>

</body>

</html>

Form Tampil Pasien

Source Code:
</strong>

<html>

<head>

<title>MySQLi Read Records</title>

</head>

<body>

<?php

//include database connection

include 'conn.php';

//query all records from the database

$query = "select * from pasien_";

//execute the query

$result = $mysqli->query( $query );

//get number of rows returned

$num_results = $result->num_rows;

//this will link us to our add.php to create new record

echo "<div><a href='add.php'>Create New Record</a></div>";

if( $num_results > 0){ //it means there's already a database record

echo "<table border='1'>";//start table

//creating our table heading

echo "<tr>";

echo "<th>Nama</th>";

echo "<th>Alamat</th>";

echo "<th>Usia</th>";

echo "<th>Telpon</th>";

echo "<th>Keluhan</th>";

echo "<th>Penanggung Jawab</th>";

echo "</tr>";

//loop to show each records

while( $row = $result->fetch_assoc() ){

//extract row

//this will make $row['firstname'] to

//just $firstname only

extract($row);

//creating new table row per record

echo "<tr>";

echo "<td>{$nama}</td>";

echo "<td>{$alamat}</td>";

echo "<td>{$usia}</td>";

echo "<td>{$telp}</td>";

echo "<td>{$keluhan}</td>";

echo "<td>{$penanggung}</td>";

echo "<td>";

//just preparing the edit link to edit the record

echo "<a href='edit.php?id={$id}'>Edit</a>";

echo " / ";

//just preparing the delete link to delete the record

echo "<a href='#' onclick='delete_user( {$id} );'>Delete</a>";

echo "</td>";

echo "</tr>";

}

echo "</table>";//end table

}else{

//if database table is empty

echo "No records found.";

}

//disconnect from database

$result->free();

$mysqli->close();

?>

</body>

</html>

Form Edit Data Pasien

Source Code:
</strong>

<?php

//include database connection

include 'conn.php';

//check any user action

$action = isset( $_POST['action'] ) ? $_POST['action'] : "";

if($action == "update"){ //if the user hit the submit button

//write our update query

//$mysqli->real_escape_string() function helps us prevent attacks such as SQL injection

$query = "update users

set

nama = '".$mysqli->real_escape_string($_POST['nama'])."',

alamat = '".$mysqli->real_escape_string($_POST['alamat'])."',

usia = '".$mysqli->real_escape_string($_POST['usia'])."',

telp  = '".$mysqli->real_escape_string($_POST['telp'])."',

keluhan  = '".$mysqli->real_escape_string($_POST['keluhan'])."'

penanggung = '".$mysqli->real_escape_string($_POST['penanggung'])."'";

//execute the query

if( $mysqli->query($query) ) {

//if updating the record was successful

echo "User was updated.";

}else{

//if unable to update new record

echo "Database Error: Unable to update record.";

}

}

//select the specific database record to update

$query = "select id, nama, alamat, usia, telp, keluhan, penanggung

from pasien_

where id='".$mysqli->real_escape_string($_REQUEST['id'])."'

limit 0,1";

//execute the query

$result = $mysqli->query( $query );

//get the result

$row = $result->fetch_assoc();

//assign the result to certain variable so our html form will be filled up with values

$id = $row['id'];

$nama = $row['nama'];

$alamat = $row['alamat'];

$usia = $row['usia'];

$telp = $row['telp'];

$keluhan = $row['keluhan'];

$penanggung = $row['penanggung'];

?>

<!--we have our html form here where new user information will be entered-->

<form action='#' method='post' border='0'>

<table>

<tr>

<td>Nama</td>

<td><input type='text' name='firstname' value='<?php echo $nama;  ?>' /></td>

</tr>

<tr>

<td>Alamat</td>

<td><input type='text' name='lastname' value='<?php echo $alamat;  ?>' /></td>

</tr>

<tr>

<td>Usia</td>

<td><input type='text' name='username'  value='<?php echo $usia;  ?>' /></td>

</tr>

<tr>

<td>Telpon</td>

<td><input type='text' name='password'  value='<?php echo $telp;  ?>' /></td>

<tr>

<td>Keluhan</td>

<td><input type='text' name='password'  value='<?php echo $keluhan;  ?>' /></td>

<tr>

<td>Penanggung</td>

<td><input type='text' name='password'  value='<?php echo $penanggung;  ?>' /></td>

<tr>

<td></td>

<td>

<!-- so that we could identify what record is to be updated -->

<input type='hidden' name='id' value='<?php echo $id ?>' />

<!-- we will set the action to update -->

<input type='hidden' name='action' value='update' />

<input type='submit' value='Edit' />

<a href='display.php'>Back to display page</a>

</td>

</tr>

</table>

</form>

0 komentar:

Posting Komentar

 
Sandra Refya Irawan © 2012 | Designed by Bubble Shooter, in collaboration with Reseller Hosting , Forum Jual Beli and Business Solutions