By GokiSoft.com| 20:05 01/04/2022|
Học PHP

[Video] Bài tập - Tạo website bán hàng lư niệm - Login - Lập trình PHP/MySQL - C2108L

Bài tập - Tạo website bán hàng lư niệm - Login - Lập trình PHP/MySQL



#add.php


<?php
if(!empty($_POST)) {
	require_once('dbhelper.php');

	$title = $_POST['title'];
	$thumbnail = $_POST['thumbnail'];
	$price = $_POST['price'];
	$content = $_POST['content'];
	$created_at = $updated_at = date('Y-m-d H:i:s');

	$sql = "insert into gift (title, thumbnail, price, content, created_at, updated_at) values ('$title', '$thumbnail', '$price', '$content', '$created_at', '$updated_at')";
	execute($sql);
}
?>

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>Register Page</title>
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>

	<style type="text/css">
		.container {
			width: 500px;
			background-color: orange;
			margin: 0px auto;
			padding: 10px;
		}
	</style>
</head>
<body>
<div class="container">
	<h3 style="text-align: center;">Add Gift Page</h3>
	<form method="post">
		<label>Title</label>
		<p>
			<input required="true" type="text" name="title" size="50">
		</p>
		<label>Thubmanil</label>
		<p>
			<input required="true" type="text" name="thumbnail" size="50">
		</p>
		<label>Content</label>
		<p>
			<textarea name="content" rows="5" style="width: 400px"></textarea>
		</p>
		<label>Price</label>
		<p>
			<input required="true" type="number" name="price" size="50">
		</p>
		<button>Add Gift</button>
	</form>
</div>
</body>
</html>


#config.php


<?php
define('HOST', 'localhost');
define('DATABASE', 'gift_db');
define('USERNAME', 'root');
define('PASSWORD', '');

const SQL_CREATE_DATABASE = "create database if not exists ".DATABASE;
const SQL_CREATE_TABLE_USER = 'create table if not exists users (
								id int primary key auto_increment,
								fullname varchar(50),
								email varchar(150),
								password varchar(32),
								token varchar(32)
							)';
const SQL_CREATE_TABLE_GIFT = 'create table if not exists gift (
								id int primary key auto_increment,
								title varchar(250),
								thumbnail varchar(500),
								content longtext,
								price float,
								created_at datetime,
								updated_at datetime,
								id_user int references users(id)
							)';


#dbhelper.php


<?php
require_once('config.php');
/**
 * Ham nay se su dung cho TH cau truy van: insert, update, delete
 */
function init() {
	// Mo ket noi toi CSDL
	$conn = mysqli_connect(HOST, USERNAME, PASSWORD);
	mysqli_set_charset($conn, 'utf8');

	mysqli_query($conn, SQL_CREATE_DATABASE);

	// Dong ket noi CSDL
	mysqli_close($conn);
}

/**
 * Ham nay se su dung cho TH cau truy van: insert, update, delete
 */
function execute($query) {
	// Mo ket noi toi CSDL
	$conn = mysqli_connect(HOST, USERNAME, PASSWORD, DATABASE);
	mysqli_set_charset($conn, 'utf8');

	mysqli_query($conn, $query);

	// Dong ket noi CSDL
	mysqli_close($conn);
}

function executeResult($query) {
	// Mo ket noi toi CSDL
	$conn = mysqli_connect(HOST, USERNAME, PASSWORD, DATABASE);
	mysqli_set_charset($conn, 'utf8');

	$resultset = mysqli_query($conn, $query);

	$data = [];
	while(($row = mysqli_fetch_array($resultset, 1)) != null) {
		$data[] = $row;
	}

	// Dong ket noi CSDL
	mysqli_close($conn);

	return $data;
}


#edit.php


<?php
require_once('dbhelper.php');

if(!empty($_POST)) {
	$id = $_POST['id'];
	$title = $_POST['title'];
	$thumbnail = $_POST['thumbnail'];
	$price = $_POST['price'];
	$content = $_POST['content'];
	$updated_at = date('Y-m-d H:i:s');

	$sql = "update gift set title = '$title', thumbnail = '$thumbnail', price = '$price', content = '$content', updated_at = '$updated_at' where id = '$id'";
	execute($sql);

	header('Location: quantri.php');
	die();
}

$id = $_GET['id'];
$data = executeResult("select * from gift where id = '$id'");
$title = $thumbnail = $price = $content = "";
if($data != null && count($data) > 0) {
	$title = $data[0]['title'];
	$thumbnail = $data[0]['thumbnail'];
	$price = $data[0]['price'];
	$content = $data[0]['content'];
}
?>

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>Edit Page</title>
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>

	<style type="text/css">
		.container {
			width: 500px;
			background-color: orange;
			margin: 0px auto;
			padding: 10px;
		}
	</style>
</head>
<body>
<div class="container">
	<h3 style="text-align: center;">Edit Gift Page</h3>
	<form method="post">
		<label>Title</label>
		<p>
			<input required="true" type="text" name="title" size="50" value="<?=$title?>">
			<input required="true" type="text" name="id" size="50" value="<?=$id?>" style="display: none;">
		</p>
		<label>Thubmanil</label>
		<p>
			<input required="true" type="text" name="thumbnail" size="50" value="<?=$thumbnail?>">
		</p>
		<label>Content</label>
		<p>
			<textarea name="content" rows="5" style="width: 400px"><?=$content?></textarea>
		</p>
		<label>Price</label>
		<p>
			<input required="true" type="number" name="price" size="50" value="<?=$price?>">
		</p>
		<button>Update Gift</button>
	</form>
</div>
</body>
</html>


#init.php


<?php
if(!empty($_POST)) {
	require_once('dbhelper.php');

	init();

	execute(SQL_CREATE_TABLE_USER);
	execute(SQL_CREATE_TABLE_GIFT);
}
?>

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>Khoi Tao CSDL</title>

	<style type="text/css">
		.container {
			width: 500px;
			background-color: orange;
			margin: 0px auto;
		}
	</style>
</head>
<body>
<div class="container">
	<h1 style="text-align: center;">Khoi Tao CSDL</h1>
	<form method="post">
		<button style="width: 100%" name="action" value="init">Khoi Tao CSDL</button>
	</form>
</div>
</body>
</html>


#login.php


<?php
if(!empty($_POST)) {
	require_once('dbhelper.php');

	$email = $_POST['email'];
	$pwd = $_POST['pwd'];

	$sql = "select * from users where email = '$email' and password = '$pwd'";
	$data = executeResult($sql);
	if($data != null && count($data) > 0) {
		//login thanh cong
		header('Location: quantri.php');
		die();
	}
}
?>

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>Login Page</title>
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>

	<style type="text/css">
		.container {
			width: 500px;
			background-color: orange;
			margin: 0px auto;
			padding: 10px;
		}
	</style>
</head>
<body>
<div class="container">
	<h3 style="text-align: center;">Login Page</h3>
	<form method="post">
		<label>Email</label>
		<p>
			<input required="true" type="email" name="email" size="50">
		</p>
		<label>Password</label>
		<p>
			<input required="true" type="password" name="pwd" id="pwd" size="50">
		</p>
		<p>
			<a href="register.php">Create a new account</a>
		</p>
		<button>Login</button>
	</form>
</div>
</body>
</html>


#quantri.php


<?php
require_once('dbhelper.php');

if(!empty($_POST)) {
	$id = $_POST['id'];
	$sql = "delete from gift where id = $id";
	execute($sql);
}

$sql = "select gift.*, users.fullname from gift left join users on gift.id_user = users.id";
$data = executeResult($sql);
?>

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>Login Page</title>
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>

	<style type="text/css">
		.container {
			width: 600px;
			background-color: orange;
			margin: 0px auto;
			padding: 10px;
		}
	</style>
</head>
<body>
<div class="container">
	<h3 style="text-align: center;">Gift Page</h3>
	<a href="add.php">Add Gift</a>
	<table border="1" cellpadding="3" cellspacing="3" style="width: 100%">
		<thead>
			<tr>
				<th>No</th>
				<th>Thumbnail</th>
				<th>Title</th>
				<th>Price</th>
				<th>Owner Name</th>
				<th></th>
				<th></th>
			</tr>
		</thead>
		<tbody>
<?php
$index = 0;
foreach($data as $item) {
	echo '<tr>
				<td>'.(++$index).'</td>
				<td><img src="'.$item['thumbnail'].'" style="width: 100px"/></td>
				<td>'.$item['title'].'</td>
				<td>'.$item['price'].'</td>
				<td>'.$item['fullname'].'</td>
				<td>
					<a href="edit.php?id='.$item['id'].'"><button>Edit</button></a>
				</td>
				<td>
					<button onclick="deleteItem('.$item['id'].')">Delete</button>
				</td>
			</tr>';
}
?>
		</tbody>
	</table>
</div>

<script type="text/javascript">
	function deleteItem(id) {
		var option = confirm('Are you sure to delete this gift?')
		if(!option) return

		$.post('quantri.php', {
			'id': id
		}, function(data) {
			location.reload()
		})
	}
</script>
</body>
</html>


#register.php


<?php
if(!empty($_POST)) {
	require_once('dbhelper.php');

	$fullname = $_POST['fullname'];
	$email = $_POST['email'];
	$pwd = $_POST['pwd'];

	$sql = "insert into users (fullname, email, password) values ('$fullname', '$email', '$pwd')";
	execute($sql);
}
?>

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>Register Page</title>
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>

	<style type="text/css">
		.container {
			width: 500px;
			background-color: orange;
			margin: 0px auto;
			padding: 10px;
		}
	</style>
</head>
<body>
<div class="container">
	<h3 style="text-align: center;">Register Page</h3>
	<form method="post" onsubmit="return validateData();">
		<label>Full Name</label>
		<p>
			<input required="true" type="text" name="fullname" size="50">
		</p>
		<label>Email</label>
		<p>
			<input required="true" type="email" name="email" size="50">
		</p>
		<label>Password</label>
		<p>
			<input required="true" type="password" name="pwd" id="pwd" size="50">
		</p>
		<label>Confirm Password</label>
		<p>
			<input required="true" type="password" name="confirmPwd" size="50">
		</p>
		<p>
			<a href="login.php">I have a account</a>
		</p>
		<button>Register</button>
	</form>
</div>

<script type="text/javascript">
	function validateData() {
		if($('[name=pwd]').val() != $('[name=confirmPwd]').val()) {
			alert('Password doens"t match')
			return false
		}

		return true
	}
</script>
</body>
</html>


#vidu.php


<?php
var_dump($_POST);
if(isset($_POST['background'])) {
	$bg = $_POST['background'];
} else {
	$bg = '';
}
?>

<!DOCTYPE html>
<html>
<head>
	<title>Test PHP</title>
	<meta charset="utf-8">
</head>
<body style="background-color: <?=$bg?>">
<h1>Test Input Radio</h1>
<form method="post">
	<input type="text" name="fullname" placeholder="Enter name">
	<p>
		<label>
			<input type="radio" name="gender" value="Name"> Nam
		</label>
		<label>
			<input type="radio" name="gender" value="Nu"> Nu
		</label>
	</p>
	<p>
		Chon BackGround
		<label>
			<input type="radio" name="background" value="red"> Red
		</label>
		<label>
			<input type="radio" name="background" value="blue"> Blue
		</label>
	</p>
	<button name="btn01" value="1">Save Data 1</button>
	<button name="btn02" value="2">Save Data 2</button>
	<button name="btn03" value="3">Save Data 3</button>
</form>
</body>
</html>


Tags:



Phản hồi từ học viên

5

(Dựa trên đánh giá ngày hôm nay)

Đăng nhập để làm bài kiểm tra

Chưa có kết quả nào trước đó