insert , update delete dengan php

Posted on Updated on


langkah pertama buat database nya dulu

CREATE TABLE `mazaadie` (
`id` tinyint(4) NOT NULL auto_increment,
`firstname` varchar(32) NOT NULL,
`lastname` varchar(32) NOT NULL,
PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;


— Dumping data for table `mazadie`

INSERT INTO `mazaadie` VALUES(1, ‘Bob’, ‘Baker’);
INSERT INTO `mazaadie` VALUES(2, ‘Tim’, ‘Thomas’);
INSERT INTO `mazaadie` VALUES(3, ‘Rachel’, ‘Roberts’);
INSERT INTO `mazaadie` VALUES(4, ‘Sam’, ‘Smith’);

kemudian buat file koneksi.php

<?php
/*
ini file untuk koneksi ke database
*/

// silahkan edit sesuai kebutuhan
$server = ‘localhost’;
$user = ‘root’;
$pass = ”;
$db = ‘mazadie’;

// Connect to Database
$connection = mysql_connect($server, $user, $pass)
or die (“Could not connect to server … \n” . mysql_error ());
mysql_select_db($db)
or die (“Could not connect to database … \n” . mysql_error ());

?>

kemudian buat file php lagi dengan nama view.php

<?php
/*
VIEW.PHP
Displays all data from ‘players’ table
*/

// connect to the database
include(‘koneksi.php’);

// get results from database
$result = mysql_query(“SELECT * FROM mazadie”)
or die(mysql_error());

// display data in table
echo “<table border=’1′ cellpadding=’10’>”;
echo “<tr> <th>ID</th> <th>First Name</th> <th>Last Name</th> <th></th> <th></th></tr>”;

// loop through results of database query, displaying them in the table
while($row = mysql_fetch_array( $result )) {

// echo out the contents of each row into a table
echo “<tr>”;
echo ‘<td>’ . $row[‘id’] . ‘</td>’;
echo ‘<td>’ . $row[‘firstname’] . ‘</td>’;
echo ‘<td>’ . $row[‘lastname’] . ‘</td>’;
echo ‘<td><a href=”edit.php?id=’ . $row[‘id’] . ‘”>Edit</a></td>’;
echo ‘<td><a href=”delete.php?id=’ . $row[‘id’] . ‘”>Delete</a></td>’;
echo “</tr>”;
}

// close table>
echo “</table>”;
?>
<p><a href=”new.php”>Add a new record</a></p>

</body>
</html>

sedangkan kode untuk membuat data baru adalaha berikut, simpan dengan nama new.php

<?php

function renderForm($first, $last, $error)
{
?>
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01//EN” “http://www.w3.org/TR/html4/strict.dtd”&gt;
<html>
<head>
<title>New Record</title>
</head>
<body>
<?php
// if there are any errors, display them
if ($error != ”)
{
echo ‘

‘.$error.’

‘;
}
?>

<form action=”” method=”post”>

First Name: * ” />
Last Name: * ” />

* required

</form>
</body>
</html>
<?php
}

//koneksi ke database
include(‘koneksi.php’);

if (isset($_POST[‘submit’]))
{
// get form data, making sure it is valid
$firstname = mysql_real_escape_string(htmlspecialchars($_POST[‘firstname’]));
$lastname = mysql_real_escape_string(htmlspecialchars($_POST[‘lastname’]));

// check to make sure both fields are entered
if ($firstname == ” || $lastname == ”)
{
// generate error message
$error = ‘ERROR: Please fill in all required fields!’;

// if either field is blank, display the form again
renderForm($firstname, $lastname, $error);
}
else
{
// simpan database
mysql_query(“INSER mazadie SET firstname=’$firstname’, lastname=’$lastname'”)
or die(mysql_error());

header(“Location: view.php”);
}
}
else
{
renderForm(”,”,”);
}
?>

berikut simpan dengan nama edit.php untuk melakukan pengeditan pada data yang telah ada

<?php

function renderForm($id, $firstname, $lastname, $error)
{
?>
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01//EN” “http://www.w3.org/TR/html4/strict.dtd”&gt;
<html>
<head>
<title>Edit Record</title>
</head>
<body>
<?php
// if there are any errors, display them
if ($error != ”)
{
echo ‘

‘.$error.’

‘;
}
?>

<form action=”” method=”post”>
<input type=”hidden” name=”id” value=”<?php echo $id; ?>”/>

ID:

First Name: * “/>
Last Name: * “/>

* Required

</form>
</body>
</html>
<?php
}

// koneksi database
include(‘koneksi.php’);

if (isset($_POST[‘submit’]))
{

if (is_numeric($_POST[‘id’]))
{

$id = $_POST[‘id’];
$firstname = mysql_real_escape_string(htmlspecialchars($_POST[‘firstname’]));
$lastname = mysql_real_escape_string(htmlspecialchars($_POST[‘lastname’]));

if ($firstname == ” || $lastname == ”)
{
$error = ‘ERROR: !!!!!!!!!’;

renderForm($id, $firstname, $lastname, $error);
}
else
{

mysql_query(“UPDATE mazadie SET firstname=’$firstname’, lastname=’$lastname’ WHERE id=’$id'”)
or die(mysql_error());

header(“Location: view.php”);
}
}
else
{

echo ‘Error!’;
}
}
else

{

if (isset($_GET[‘id’]) && is_numeric($_GET[‘id’]) && $_GET[‘id’] > 0)
{
// query db
$id = $_GET[‘id’];
$result = mysql_query(“SELECT * FROM mazadie WHERE id=$id”)
or die(mysql_error());
$row = mysql_fetch_array($result);

if($row)
{

$firstname = $row[‘firstname’];
$lastname = $row[‘lastname’];

renderForm($id, $firstname, $lastname, ”);
}
else

{
echo “kosong!”;
}
}
else

{
echo ‘Error!’;
}
}
?>

dan yang terakhir, kode untuk menghapus dan simpan dengan nama delete.php

<?php

include(‘koneksi.php’);

if (isset($_GET[‘id’]) && is_numeric($_GET[‘id’]))
{
$id = $_GET[‘id’];

$result = mysql_query(“DELETE FROM mazadie WHERE id=$id”)
or die(mysql_error());

header(“Location: view.php”);
}
else

{
header(“Location: view.php”);
}

?>

selesai

selamat mencoba semoga sukses…………

langkah pertama buat database nya dulu

CREATE TABLE `mazaadie` (
`id` tinyint(4) NOT NULL auto_increment,
`firstname` varchar(32) NOT NULL,
`lastname` varchar(32) NOT NULL,
PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;


— Dumping data for table `players`

INSERT INTO `mazaadie` VALUES(1, ‘Bob’, ‘Baker’);
INSERT INTO `mazaadie` VALUES(2, ‘Tim’, ‘Thomas’);
INSERT INTO `mazaadie` VALUES(3, ‘Rachel’, ‘Roberts’);
INSERT INTO `mazaadie` VALUES(4, ‘Sam’, ‘Smith’);

kemudian buat file koneksi.php

<?php
/*
ini file untuk koneksi ke database
*/

// silahkan edit sesuai kebutuhan
$server = ‘localhost’;
$user = ‘root’;
$pass = ”;
$db = ‘mazadie’;

// Connect to Database
$connection = mysql_connect($server, $user, $pass)
or die (“Could not connect to server … \n” . mysql_error ());
mysql_select_db($db)
or die (“Could not connect to database … \n” . mysql_error ());

?>

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s