[go: up one dir, main page]

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

CRUD

1. A database called "crud_db" is created with a table called "users" containing user data like name, email, mobile. 2. A configuration file connects to the database. 3. An index page displays all users by querying the database and shows edit/delete links. Additional pages allow adding new users, editing user data, and deleting users all by querying the database.

Uploaded by

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

CRUD

1. A database called "crud_db" is created with a table called "users" containing user data like name, email, mobile. 2. A configuration file connects to the database. 3. An index page displays all users by querying the database and shows edit/delete links. Additional pages allow adding new users, editing user data, and deleting users all by querying the database.

Uploaded by

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

1.

Membuat database

/* SQL MANUAL */
create database crud_db;

use crud_db;

CREATE TABLE `users` (


`id` int(11) NOT NULL auto_increment,
`name` varchar(100),
`email` varchar(100),
`mobile` varchar(15),
PRIMARY KEY (`id`)
);

2. Config.php

<?php

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

$mysqli = mysqli_connect($databaseHost, $databaseUsername, $databasePassword,


$databaseName);

?>
3. Index.php

<?php
include_once("config.php");

$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>
4. Add.php

<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
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>

5. edit.php

<?php
include_once("config.php");

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

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

$result = mysqli_query($mysqli, "UPDATE users SET


name='$name',email='$email',mobile='$mobile' WHERE id=$id");

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

<?php
$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>
6. delete.php

<?php
include_once("config.php");

$id = $_GET['id'];

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

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

You might also like