0% found this document useful (0 votes)
43 views6 pages

Rachmat Ramadhan-183112706450047-TUGAS 7

The document describes steps to create a basic CRUD (Create, Read, Update, Delete) application using PHP and MySQL. The steps include: 1. Creating a Database.sql file to define the database table 2. Creating a config.php file to connect to the database 3. Creating index.php to display all user data from the database 4. Creating add.php to insert new user data 5. Creating edit.php to update existing user data 6. Creating delete.php to remove user data from the database Screenshots are provided of the index.php, add.php and edit.php files. The CRUD application allows users to view, add, edit and

Uploaded by

Kurnia Asmiati
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views6 pages

Rachmat Ramadhan-183112706450047-TUGAS 7

The document describes steps to create a basic CRUD (Create, Read, Update, Delete) application using PHP and MySQL. The steps include: 1. Creating a Database.sql file to define the database table 2. Creating a config.php file to connect to the database 3. Creating index.php to display all user data from the database 4. Creating add.php to insert new user data 5. Creating edit.php to update existing user data 6. Creating delete.php to remove user data from the database Screenshots are provided of the index.php, add.php and edit.php files. The CRUD application allows users to view, add, edit and

Uploaded by

Kurnia Asmiati
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Praktikum Pemrograman Web

Nama : Rachmat Ramadhan


NPM : 183112706450047
Kelas : R.03

Latihan CRUD

Buatlah CRUD sederhana menggunakan PHP dan MySQL dengan langkah-langkah sebagai berikut:

1. Membuat Database.sql
2. Membuat file config.php
3. Membuat file index.php
4. Membuat file add.php
5. Membuat file edit.php
6. Membuat file delete.php

Tulis skrip Database.sql untuk membuat Database

CREATE TABLE `users` (


-> `id` int(11) NOT NULL auto_increment,
-> `name` varchar(100),
-> `email` varchar(100),
-> `mobile` varchar(15),
-> PRIMARY KEY (`id`)
-> );
Tulis skrip config.php untuk membuat koneksi ke database

<?php
/**
* using mysqli_connect for database connection
*/

$databaseHost = 'localhost';
$databaseName = 'crud_db';
$databaseUsername = 'root';
$databasePassword = '';

$mysqli = mysqli_connect($databaseHost, $databaseUsername,


$databasePassword, $databaseName);

?>

Tulis skrip index.php untuk tampilan halaman utama

<?php
// Create database connection using config file
include_once("config.php");

// Fetch all users data from database


$result = mysqli_query($mysqli, "SELECT * FROM users ORDER BY id DESC");
?>

<html>
<head>
<title>Homepage</title>
</head>

<body>
<a href="add.php">Add New User</a><br/><br/>

<table width='80%' border=1>

<tr>
<th>Name</th> <th>Mobile</th> <th>Email</th> <th>Update</th>
</tr>
<?php
while($user_data = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>".$user_data['name']."</td>";
echo "<td>".$user_data['mobile']."</td>";
echo "<td>".$user_data['email']."</td>";
echo "<td><a href='edit.php?id=$user_data[id]'>Edit</a> | <a
href='delete.php?id=$user_data[id]'>Delete</a></td></tr>";
}
?>

</table>
</body>
</html>
Tampilkan tangkapan layar index.php

Tulis skrip add.php untuk menambahkan data

<html>
<head>
<title>Add Users</title>
</head>

<body>
<a href="index.php">Go to Home</a>
<br/><br/>

<form action="add.php" method="post" name="form1">


<table width="25%" border="0">
<tr>
<td>Name</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>Email</td>
<td><input type="text" name="email"></td>
</tr>
<tr>
<td>Mobile</td>
<td><input type="text" name="mobile"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="Submit" value="Add"></td>
</tr>
</table>
</form>

<?php

// Check If form submitted, insert form data into users table.


if(isset($_POST['Submit'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$mobile = $_POST['mobile'];

// include database connection file


include_once("config.php");
// Insert user data into table
$result = mysqli_query($mysqli, "INSERT INTO
users(name,email,mobile) VALUES('$name','$email','$mobile')");

// Show message when user added


echo "User added successfully. <a href='index.php'>View
Users</a>";
}
?>
</body>
</html>

Tampilkan tangkapan layar add.php

Tulis skrip edit.php untuk menambahkan data

<?php
// include database connection file
include_once("config.php");

// Check if form is submitted for user update, then redirect to homepage


after update
if(isset($_POST['update']))
{
$id = $_POST['id'];

$name=$_POST['name'];
$mobile=$_POST['mobile'];
$email=$_POST['email'];

// update user data


$result = mysqli_query($mysqli, "UPDATE users SET
name='$name',email='$email',mobile='$mobile' WHERE id=$id");

// Redirect to homepage to display updated user in list


header("Location: index.php");
}
?>
<?php
// Display selected user data based on id
// Getting id from url
$id = $_GET['id'];

// Fetech user data based on id


$result = mysqli_query($mysqli, "SELECT * FROM users WHERE id=$id");
while($user_data = mysqli_fetch_array($result))
{
$name = $user_data['name'];
$email = $user_data['email'];
$mobile = $user_data['mobile'];
}
?>
<html>
<head>
<title>Edit User Data</title>
</head>

<body>
<a href="index.php">Home</a>
<br/><br/>

<form name="update_user" method="post" action="edit.php">


<table border="0">
<tr>
<td>Name</td>
<td><input type="text" name="name" value=<?php echo
$name;?>></td>
</tr>
<tr>
<td>Email</td>
<td><input type="text" name="email" value=<?php echo
$email;?>></td>
</tr>
<tr>
<td>Mobile</td>
<td><input type="text" name="mobile" value=<?php echo
$mobile;?>></td>
</tr>
<tr>
<td><input type="hidden" name="id" value=<?php echo
$_GET['id'];?>></td>
<td><input type="submit" name="update"
value="Update"></td>
</tr>
</table>
</form>
</body>
</html>

Tampilkan tangkapan layar edit.php


Tulis skrip delete.php untuk menambahkan data

<?php
// include database connection file
include_once("config.php");

// Get id from URL to delete that user


$id = $_GET['id'];

// Delete user row from table based on given id


$result = mysqli_query($mysqli, "DELETE FROM users WHERE id=$id");

// After delete redirect to Home, so that latest user list will be


displayed.
header("Location:index.php");
?>

You might also like