By GokiSoft.com|
20:16 17/01/2024|
Học PHP
Viết website quản lý sinh viên PHP & MySQL - Lập Trình PHP
Viết chương trình quản lý sinh viên
Thêm, sửa, xoá, tìm kiếm.
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)
Triệu Văn Lăng
2021-06-14 08:37:17
#addstudent.php
<?php
// require_once('formStudent.php');
require_once ('dbhelper.php');
$fullname = $age = $address = '';
if(!empty($_POST)) {
$id = '';
if (isset($_POST['fullname'])) {
// code...
$fullname = $_POST['fullname'];
}
if (isset($_POST['age'])) {
// code...
$age = $_POST['age'];
}
if (isset($_POST['address'])) {
// code...
$address = $_POST['address'];
}
if (isset($_POST['id'])) {
// code...
$id = $_POST['id'];
}
$fullname =str_replace('\'', '\\\'', $fullname);
$age =str_replace('\'', '\\\'', $age);
$address =str_replace('\'', '\\\'', $address);
$id =str_replace('\'', '\\\'', $id);
if ($id != '') {
// code...
$sql =
"update student set fullname = '$fullname', age = '$age', address = '$address' where id =".$id;
} else {
$sql = "insert into student(fullname, age, address) values ('$fullname', '$age', '$address')";
}
// echo($sql);
execute($sql);
header('Location: student.php');
}
$id = '';
if (isset($_GET['id'])) {
// code...
$id = $_GET['id'];
$sql = 'select * from student where id = '.$id;
$studentList = executeResult($sql);
if ($studentList != null && count($studentList)>0 ) {
// code...
$std = $studentList[0];
$fullname = $std['fullname'];
$age = $std['age'];
$address = $std['address'];
} else {
$id = '';
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Registation Form * Form Tutorial</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<!-- Popper JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="panel panel-primary">
<div class="panel-heading">
<h2 class="text-center">Nhập thông tin sinh viên</h2>
</div>
<div class="panel-body">
<form method="post">
<div class="form-group">
<label for="usr">Họ Tên:</label>
<input type="number" name="id" value="<?=$id?>" style="display: none;">
<input required="true" type="text" class="form-control" id="fullname" name="fullname" value="<?=$fullname?>">
</div>
<div class="form-group">
<label for="usr">Tuổi:</label>
<input required="true" type="number" class="form-control" id="age" name="age" value="<?=$age?>">
</div>
<div class="form-group">
<label for="address">Địa chỉ:</label>
<input type="text" class="form-control" id="address" name="address" value="<?=$address?>">
</div>
<a href="student.php"><button type="button" class="btn btn-danger">Danh sách sinh viên</button></a>
<button class="btn btn-success">Save</button>
</form>
</div>
</div>
</div>
</body>
</html>
#config.php
<?php
define('HOST', 'localhost');
define('USERNAME', 'root');
define('PASSWORD', '');
define('DATABASE', 'qlsinhvien');
#dbhelper.php
<?php
require_once('config.php');
function execute($sql) {
//save data -> database (product)
//B1. Mo ket noi toi database
$conn = mysqli_connect(HOST, USERNAME, PASSWORD, DATABASE);
mysqli_set_charset($conn, 'utf8');
//B2. Thuc thi query (select, insert, update, delete)
mysqli_query($conn, $sql);
//B3. Dong ket noi database
mysqli_close($conn);
}
function executeResult($sql) {
//save data -> database (product)
//B1. Mo ket noi toi database
$conn = mysqli_connect(HOST, USERNAME, PASSWORD, DATABASE);
mysqli_set_charset($conn, 'utf8');
//B2. Thuc thi query (select, insert, update, delete)
$data = [];
$resultset = mysqli_query($conn, $sql);
while (($row = mysqli_fetch_array($resultset, 1)) != null) {
$data[] = $row;
}
//B3. Dong ket noi database
mysqli_close($conn);
return $data;
}
function deleteStudent() {
$id = $_POST('id');
$sql = "delete from student where id = $id";
execute($sql);
echo "Xóa thành công";
}
#student.php
<?php
require_once('dbhelper.php');
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Student</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<!-- Popper JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="panel-primary">
<div class="panel-heading" style="margin-top: 20px">
<h3>Quản lí thông tin sinh viên</h3>
</div>
<div class="panel-body">
<table class="table table-bordered">
<thead>
<tr>
<th width="60px">STT</th>
<th>Họ tên </th>
<th>Tuổi</th>
<th>Địa chỉ</th>
<th width="60px"></th>
<th width="60px"></th>
</tr>
</thead>
<tbody>
<?php
$sql = 'select * from student';
$studentList = executeResult($sql);
$count = 1;
foreach ($studentList as $std) {
// code...
echo '<tr>
<td>'.($count++).'</td>
<td>'.$std['fullname'].'</td>
<td>'.$std['age'].'</td>
<td>'.$std['address'].'</td>
<td><button class="btn btn-warning" onclick=\'window.open("addstudent.php?id='.$std['id'].'", "_self")\'>Sửa</button></td>
<td><button class="btn btn-danger" onclick="deleteStudent('.$std['id'].')">Xóa</button></td>
</tr>';
}
?>
</tbody>
</table>
<a href="addstudent.php"><button class="btn btn-success" style="margin-top: 15px">
Thêm sinh viên</button></a>
</div>
</div>
</div>
<script type="text/javascript">
function deleteStudent(id) {
option = confirm('Bạn có muốn xóa sinh viên này?')
if(!option) return
$.post('dbhelper.php', {
'action': 'delete',
'id': id
}, function(data) {
alert(data)
location.reload()
})
}
</script>
</body>
</html>
Trần Văn Lâm
2021-06-09 10:22:00
#config.php
<?php
define('HOST','localhost');
define('USERNAME','ROOT');
define('PASSWORD','');
define('DATABASE','student');
#dbhelper.php
<?php
require_once ('config.php');
/**
* Su dung voi cau lenh query: insert, update, delete -> ko tra ve ket qua.
*/
function execute($sql) {
//Mo ket noi toi database
$conn = mysqli_connect(HOST, USERNAME, PASSWORD, DATABASE);
mysqli_set_charset($conn, 'utf8');
//Xu ly cau query
mysqli_query($conn, $sql);
//Dong ket noi database
mysqli_close($conn);
}
/**
* Su dung voi cau lenh query: select.
*/
function executeResult($sql) {
//Mo ket noi toi database
$conn = mysqli_connect(HOST, USERNAME, PASSWORD, DATABASE);
mysqli_set_charset($conn, 'utf8');
// echo $sql;
//Xu ly cau query
$resultset = mysqli_query($conn, $sql);
// var_dump($resultset);
// die();
$data = [];
while (($row = mysqli_fetch_array($resultset, 1)) != null) {
$data[] = $row;
}
/**
* TH: param2 = 1
* $row = [
* 'id' => 1,
* 'title' => '1 - Android Tivi Sony 4K 55 inch KD-55X8000H',
* 'thumbnail' => '12321',
* ...
* ];
*
* TH: param2 = 2
* $row = [1, '1 - Android Tivi Sony 4K 55 inch KD-55X8000H', '12321', ...];
*/
//Dong ket noi database
mysqli_close($conn);
return $data;
}
#index.php
<?php
require_once('config.php');
require_once('dbhelper.php');
if (isset($_GET['delete_id'])) {
$delete_id = $_GET['delete_id'];
$sql = 'delete from students where id = '.$delete_id.;
execute($sql);
}
$fullname = $age = $address = '';
if (isset($_POST['fullname'])) {
$hoten = $_POST['fullname'];
}
if (isset($_POST['age'])) {
$tuoi = $_POST['age'];
}
if (isset($_POST['address'])) {
$diachi = $_POST['address'];
}
if ($hoten != '' && $tuoi != '' && $diachi != '') {
$sql = 'insert into students (fullname,age,address)
values ("'.$hoten.'","'.$tuoi.'","'.$diachi.'")';
execute($sql);
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Quản Lí Sinh Viên</title>
<meta charset="utf-8">
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<!-- Popper JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="panel panel-primary">
Management Student's Detail Information
</div>
<div class="panel-body">
<table class="table table-hover table-bordered">
<tr>
<th>No</th>
<th>Fullname</th>
<th>Age</th>
<th>Address</th>
</tr>
<?php
$sql = 'select * from students';
$result = executeReult($sql);
$no = 1;
foreach ($result as $row) {
echo '<tr>
<td>'.($no++).'</td>
<td>'.$row['fullname'].'</td>
<td><'.$row['age'].'/td>
<td>'.$row['address'].'</td>
<td><a href = "?delete_id='.$row['id'].'"><button class="btn btn-danger">Delete</button></a></td>
</tr>';
}
?>
</table>
</div>
</div>
<div class="container">
<div class="panel panel-primary">
Input
</div>
<div class="panel-body">
<form method="POST">
<div class="form-group">
<label>Fullname</label>
<input type="text" name="fullname" class="form-control">
</div>
<div class="form-group">
<label>Age</label>
<input type="text" name="age" class="form-control">
</div>
<div class="form-group">
<label>Address</label>
<input type="text" name="address" class="form-control">
</div>
<button class="btn btn-success">Add</button>
</form>
</div>
</div>
</body>
</html>
vuong huu phu
2021-06-08 05:48:09
<?php
define('HOST', 'localhost');
define('DATABASE', 'bt1');
define('USERNAME', 'root');
define('PASSWORD', '');
<?php
require_once('dbhelper.php');
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
<title>Quan ly sinh vien</title>
</head>
<body>
<div class="container">
<table class="table">
<thead>
<tr>
<th>No</th>
<th>Fullname</th>
<th>Age</th>
<th>Address</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<?php
$sql = 'select * from student';
$list = executeresult($sql);
$i = 1;
foreach ($list as $s) {
echo '<tr>
<td>'.$i++.'</td>
<td>'.$s['Name'].'</td>
<td>'.$s['Age'].'</td>
<td>'.$s['Address'].'</td>
<td><button type="button" class="btn btn-success" onclick = \'window.open("addstd.php?ID='.$s['id'].'","_self")\'>Edit</button></td>
<td><button type="button" class="btn btn-danger" onclick = "DELETE('.$s['id'].')">Delete</button></td>
</tr>';
}
?>
</tbody>
</table>
<center><a href="addstd.php"><button type="button" class="btn btn-info">Add Student</button></a></center>
</div>
<script type="text/javascript">
function DELETE(id){
console.log(id)
$.post ('delete.php',{
'id' : id
},function(data){
location.reload()
})
}
</script>
</body>
</html>
<?php
require_once('dbhelper.php');
if (isset($_POST['id'])) {
$id = $_POST['id'];
$sql = "DELETE FROM `student` WHERE `student`.`id` = " . $id;
execute($sql);
}
<?php
require_once('dbhelper.php');
$name = $age= $address = '';
if(!empty($_POST)){
$id = '';
if (isset($_POST['id'])) {
$id = $_POST['id'];
}
if (isset($_POST['name'])) {
$name = $_POST['name'];
}
if (isset($_POST['age'])) {
$age = $_POST['age'];
}
if (isset($_POST['address'])) {
$address = $_POST['address'];
}
if ($id != null) {
$sql = "UPDATE `student` SET `Name` = '$name', `Age` = '$age', `Address` = '$address' WHERE `student`.`id` = " . $id;
}else{
$sql = "INSERT INTO `student` (`Name`, `Age`, `Address`) VALUES ('$name', '$age', '$address')";
}
execute($sql);
header('Location: studen.php');
die();}
$id = '';
if (isset($_GET['ID'])) {
$id = $_GET['ID'];
$sql = 'SELECT * FROM `student` WHERE `id` = '.$id.'';
$data = executeresult($sql);
if($data != null && count($data)> 0){
$datastd = $data[0];
$name = $datastd['Name'];
$age = $datastd['Age'];
$address = $datastd['Address'];
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Them sinh vien</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<form method="POST">
<div class="form-group">
<input style="display: none;" type="number" name="id"value="<?=$id?>">
<label for="name">Fullname:</label>
<input type="text" class="form-control" id="name" placeholder="Enter name" name="name" value="<?=$name?>">
</div>
<div class="form-group">
<label for="age">Age:</label>
<input type="number" class="form-control" id="age" placeholder="Enter age" name="age"value="<?=$age?>">
</div>
<div class="form-group">
<label for="address">Address:</label>
<input type="text" class="form-control" id="address" placeholder="Enter address" name="address"value="<?=$address?>">
</div>
<button type="submit" class="btn btn-info">Submit</button>
</form></div>
</body>
</html>
<?php
require_once('config.php');
function execute($sql){
$Connect = mysqli_connect(HOST,USERNAME,PASSWORD,DATABASE);
mysqli_query($Connect,$sql);
mysqli_close($Connect);
}
function executeresult($sql){
$Connect = mysqli_connect(HOST,USERNAME,PASSWORD,DATABASE);
$result = mysqli_query($Connect,$sql);
$data = [];
while ($row = mysqli_fetch_array($result,1)) {
$data[] = $row;
}
mysqli_close($Connect);
return $data;
}
Do Trung Duc
2021-06-07 15:16:51
<?php
define('HOST', 'localhost');
define('USERNAME', 'root');
define('PASSWORD', '');
define('DATABASE', 'studentmanager');
Do Trung Duc
2021-06-07 15:16:31
<?php
require_once('config.php');
function execute($sql){
$conn = mysqli_connect(HOST,USERNAME,PASSWORD,DATABASE);
mysqli_set_charset($conn,'utf8');
mysqli_query($conn, $sql);
mysqli_close($conn);
}
function executeResult($sql){
$conn = mysqli_connect(HOST,USERNAME,PASSWORD,DATABASE);
mysqli_set_charset($conn,'utf8');
$resultset = mysqli_query($conn, $sql);
$data = [];
while( ($row = mysqli_fetch_array($resultset,1)) !=null ){
$data[] = $row;
}
mysqli_close($conn);
return $data;
}
Do Trung Duc
2021-06-07 15:16:19
<?php
require_once('dbhelper.php');
if(!empty($_POST)){
if(isset($_POST['action'])){
$action = $_POST['action'];
}
switch ($action) {
case 'delete':
$idDelete = $_POST['idDelete'];
$sql = "DELETE FROM students WHERE id = $idDelete";
execute($sql);
echo "Da xoa thanh cong";
break;
default:
if(isset($_POST['fullname'])){
$fullname = $_POST['fullname'];
}
if(isset($_POST['age'])){
$age = $_POST['age'];
}
if(isset($_POST['address'])){
$address = $_POST['address'];
}
if(isset($_POST['idUpdate'])){
$idUpdate = $_POST['idUpdate'];
if ($idUpdate == '') {
$sql = "insert into students (fullname,age,address) values ('$fullname', '$age', '$address')";
}else{
$sql = "update students set fullname = '$fullname', age = '$age', address = '$address' where id = '$idUpdate'";
}
}
execute($sql);
header("Location: index.php");
break;
}
}
if (!empty($_GET)) {
if(isset($_GET['id'])){
$idUpdate = $_GET['id'];
$sql = "select * FROM students WHERE id = $idUpdate";
$data = executeResult($sql);
$student = $data[0];
$fullname = $student['fullname'];
$age= $student['age'];
$address = $student['address'];
var_dump($student);
}
}
Do Trung Duc
2021-06-07 15:16:02
<?php
$idUpdate = $fullname = $age = $address = '';
require_once('dbhelper.php');
require_once('action.php');
$sql = "select * from students";
$data = executeResult($sql);
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
</head>
<body>
<div style = "margin-top: 30px;" class="container">
<div class="panel panel-info">
<div class="panel-heading">
<h3 class="panel-title">Student Manager</h3>
</div>
<div class="panel-body">
<table class="table table-bordered table-hover">
<thead>
<tr>
<th>No</th>
<th>Full Name</th>
<th>Age</th>
<th>Address</th>
<th colspan="2">Action</th>
</tr>
</thead>
<tbody>
<?php
$count = 0;
foreach ($data as $student) {
echo '<tr>
<td>'.++$count.'</td>
<td>'.$student['fullname'].'</td>
<td>'.$student['age'].'</td>
<td>'.$student['address'].'</td>
<td><button onclick = Delete('.$student['id'].')>Delete</button></td>
<td><button onclick = window.open("index.php?id='.$student['id'].'","_self");>Update</button></td>
</tr>';
}
?>
</tbody>
</table>
</div>
</div>
</div>
<div style = "margin-top: 30px;" class="container">
<div class="panel panel-info">
<div class="panel-heading">
<h3 class="panel-title">Input Student Information</h3>
</div>
<div class="panel-body">
<form method="post" action="action.php">
<input value="<?=$idUpdate?>" type="text" name="idUpdate">
<div class="form-group">
<label for="fullname">FullName</label>
<input value="<?=$fullname?>" name="fullname" type="text" class="form-control" id="fullname">
</div>
<div class="form-group">
<label for="age">Age</label>
<input value="<?=$age?>" name = "age" type="text" class="form-control" id="age">
</div>
<div class="form-group">
<label for="address">Address</label>
<input value="<?=$address?>" name="address" type="address" class="form-control" id="address">
</div>
<button type="submit" class="btn btn-default btn-primary">Save</button>
</form>
</div>
</div>
</div>
<script type="text/javascript">
function Delete(id) {
$.post("action.php",
{idDelete: id,
action: 'delete'},
function(data, status){
alert(data);
location.reload();
});
}
</script>
<script src="//code.jquery.com/jquery.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
</body>
</html>
Nguyễn Anh Vũ
2021-06-07 10:16:23
<?php
require_once ('config.php');
require_once ('dbhelper.php');
require_once ('process.php');
?>
<!DOCTYPE html>
<html>
<head>
<title>Input Student's Detail Information</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<!-- Popper JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="panel panel-primary">
<div class="panel-heading">
<h2 class="text-center pt-5">Input Student's Detail Information</h2>
</div>
<div class="panel-body">
<form method="POST">
<div class="form-group">
<label for="fullname">Full Name:</label>
<input type="text" class="form-control" name="fullname">
</div>
<div class=" form-group">
<label for="age">Age:</label>
<input type="text" class="form-control" name="age">
</div>
<div class="form-group">
<label for="address">Address:</label>
<input type="text" class="form-control" name="address">
</div>
<button class="btn btn-success">Save</button>
</form>
<br>
<input class="form-control" name="s" id="myInput" type="text" placeholder="Search..">
<table class="table">
<thead>
<tr>
<th>ID</th>
<th>Full Name</th>
<th>Age</th>
<th>Address</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody id="myTable">
<?php
$sql = "select * from student";
$userList = execute_result($sql);
// ends
foreach ($userList as $row) {
echo '<tr>
<td>'.$row['id'].'</td>
<td>'.$row['fullname'].'</td>
<td>'.$row['age'].'</td>
<td>'.$row['address'].'</td>
<td><button type="button" class="btn btn-warning">Edit</button></td>
<td><button class="btn btn-danger btn-sm rounded-0" type="button" data-toggle="tooltip" data-placement="top" title="Delete">Delete</button></td>
</tr>';
}
?> </tbody>
</table>
</div>
</div>
</div>
<script>
$(document).ready(function() {
$("#myInput").on("keyup", function() {
var value = $(this).val().toLowerCase();
$("#myTable tr").filter(function() {
$(this).toggle($(this).text().toLowerCase().indexOf(value) > -1)
});
});
});
</script>
</body>
</html>
<?php
define('HOST', 'localhost');
define('DATABASE', 'quanlisinhvienphp');
define('USERNAME', 'root');
define('PASSWORD', '');
?>
<?php
function execute($sql) {
$con = mysqli_connect(HOST, USERNAME, PASSWORD, DATABASE);
mysqli_query($con, $sql);
mysqli_close($con);
}
function execute_result($sql) {
$con = mysqli_connect(HOST, USERNAME, PASSWORD, DATABASE);
$resultset = mysqli_query($con, $sql);
$resultList = [];
while ($row = mysqli_fetch_array($resultset, 1)) {
$resultList[] = $row;
}
mysqli_close($con);
return $resultList;
}
<?php
$fullname = $age = $address ='';
if (isset($_POST)) {
if (isset($_POST['fullname'])) {
$fullname = $_POST['fullname'];
}
if (isset($_POST['age'])) {
$age = $_POST['age'];
}
if (isset($_POST['address'])) {
$address = $_POST['address'];
}
$sql = "INSERT INTO student(fullname, age, address) VALUES ('$fullname', '$age','$address')";
execute($sql);
}
?>
NguyenHuuThanh
2020-07-01 05:35:19
<?php
define('HOST','localhost');
define('DATABASE' , 'db1656');
define('USERNAME' , 'root');
define('PASSWORD' , '');
<?php
require_once ('config.php');
/**
* insert, update, delete -> su dung function
*/
function execute($sql) {
//create connection toi database
$conn = mysqli_connect(HOST, USERNAME, PASSWORD, DATABASE);
//query
mysqli_query($conn, $sql);
//dong connection
mysqli_close($conn);
}
/**
* su dung cho lenh select => tra ve ket qua
*/
function executeResult($sql) {
//create connection toi database
$conn = mysqli_connect(HOST, USERNAME, PASSWORD, DATABASE);
//query
$resultset = mysqli_query($conn, $sql);
$list = [];
while ($row = mysqli_fetch_array($resultset, 1)) {
$list[] = $row;
}
//dong connection
mysqli_close($conn);
return $list;
}
<?php
if (isset($_POST['id'])) {
$id = $_POST['id'];
require_once ('dbhelp.php');
$sql = 'delete from student where id = '.$id;
execute($sql);
echo 'Xoá sinh viên thành công';
}
<?php
require_once ('dbhelp.php');
?>
<!DOCTYPE html>
<html>
<head>
<title>Student Management</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<!-- Popper JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="panel panel-primary">
<div class="panel-heading">
Quản lý thông tin sinh viên
<form method="get">
<input type="text" name="s" class="form-control" style="margin-top: 15px; margin-bottom: 15px;" placeholder="Tìm kiếm theo tên">
</form>
</div>
<div class="panel-body">
<table class="table table-bordered">
<thead>
<tr>
<th>STT</th>
<th>Họ & Tên</th>
<th>Tuổi</th>
<th>Địa chỉ</th>
<th width="60px"></th>
<th width="60px"></th>
</tr>
</thead>
<tbody>
<?php
// TIm kiem :
if (isset($_GET['s']) && $_GET['s'] != '') {
$sql = 'select * from student where fullname like "%'.$_GET['s'].'%"';
} else {
$sql = 'select * from student';
}
$studentList = executeResult($sql);
$index = 1;
foreach ($studentList as $std) {
echo '<tr>
<td>'.($index++).'</td>
<td>'.$std['fullname'].'</td>
<td>'.$std['age'].'</td>
<td>'.$std['address'].'</td>
<td><button class="btn btn-warning" onclick=\'window.open("input.php?id='.$std['id'].'","_self")\'>Edit</button></td>
<td><button class="btn btn-danger" onclick="deleteStudent('.$std['id'].')">Delete</button></td>
</tr>';
}
?>
</tbody>
</table>
<button class="btn btn-success" onclick="window.open('input.php', '_self')">Add Student</button>
</div>
</div>
</div>
<script type="text/javascript">
function deleteStudent(id) {
option = confirm('Bạn có muốn xoá sinh viên này không')
if(!option) {
return;
}
console.log(id)
$.post('delete_student.php', {
'id': id
}, function(data) {
alert(data)
location.reload()
})
}
</script>
</body>
</html>
<?php
require_once ('dbhelp.php');
$s_fullname = $s_age = $s_address = '';
if (!empty($_POST)) {
$s_id = '';
if (isset($_POST['fullname'])) {
$s_fullname = $_POST['fullname'];
}
if (isset($_POST['age'])) {
$s_age = $_POST['age'];
}
if (isset($_POST['address'])) {
$s_address = $_POST['address'];
}
if (isset($_POST['id'])) {
$s_id = $_POST['id'];
}
$s_fullname = str_replace('\'', '\\\'', $s_fullname);
$s_age = str_replace('\'', '\\\'', $s_age);
$s_address = str_replace('\'', '\\\'', $s_address);
$s_id = str_replace('\'', '\\\'', $s_id);
if ($s_id != '') {
//update
$sql = "update student set fullname = '$s_fullname', age = '$s_age', address = '$s_address' where id = " .$s_id;
} else {
//insert
$sql = "insert into student(fullname, age, address) value ('$s_fullname', '$s_age', '$s_address')";
}
// echo $sql;
execute($sql);
header('Location: index.php');
die();
}
$id = '';
if (isset($_GET['id'])) {
$id = $_GET['id'];
$sql = 'select * from student where id = '.$id;
$studentList = executeResult($sql);
if ($studentList != null && count($studentList) > 0) {
$std = $studentList[0];
$s_fullname = $std['fullname'];
$s_age = $std['age'];
$s_address = $std['address'];
} else {
$id = '';
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Registation Form * Form Tutorial</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<!-- Popper JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="panel panel-primary">
<div class="panel-heading">
<h2 class="text-center">Add Student</h2>
</div>
<div class="panel-body">
<form method="post">
<div class="form-group">
<label for="usr">Name:</label>
<input type="number" name="id" value="<?=$id?>" style="display: none;">
<input required="true" type="text" class="form-control" id="usr" name="fullname" value="<?=$s_fullname?>">
</div>
<div class="form-group">
<label for="birthday">Age:</label>
<input type="number" class="form-control" id="age" name="age" value="<?=$s_age?>">
</div>
<div class="form-group">
<label for="address">Address:</label>
<input type="text" class="form-control" id="address" name="address" value="<?=$s_address?>">
</div>
<button class="btn btn-success">Save</button>
</form>
</div>
</div>
</div>
</body>
</html>
Đỗ Văn Huấn
2020-06-18 01:10:00
<?php
define('HOST', 'localhost');
define('DATABASE', 'quanlysinhvien');
define('USERNAME', 'root');
define('PASSWORD', '');
<?php
function select($sql)
{
//mở kết nối
$con = mysqli_connect(HOST, USERNAME, PASSWORD, DATABASE);
//tạo truy vấn
$resulset = mysqli_query($con, $sql);
// tạo mảng lưu student
$studentList = array();
while ($row = mysqli_fetch_array($resulset, 1)) {
$studentList[] = $row;
}
// đóng kết nối
mysqli_close($con);
return $studentList;
}
function insert_remove($sql)
{
//mở kết nối
$con = mysqli_connect(HOST, USERNAME, PASSWORD, DATABASE);
//tạo truy vấn
mysqli_query($con, $sql);
// đóng kết nối
mysqli_close($con);
}
<?php
require_once('a.php');
require_once('manage.php');
require_once('insertDatabase.php');
$tr = '';
$xoa = '';
$hienthi = '';
if (!empty($_GET)) {
if (isset($_GET['id'])) {
$d = $_GET['id'];
$xoa = 'DELETE FROM student WHERE student.id =' . $d;
insert_remove($xoa);
}
}
$hienthi = "select * from student";
$studentList = select($hienthi);
foreach ($studentList as $student) {
$tr .= '<tr>' .
'<td class="id">' . $student['id'] . '</td>' .
'<td>' . $student['name'] . '</td>' .
'<td>' . $student['age'] . '</td>' .
' <td colspan="2">' . $student['address'] . '</td>' .
'<td >' . '<a href="?id=' . $student['id'] . '"><button>Delete</button></a>' . '</td>' .
'</tr>';
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<!-- <script>
</script> -->
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://code.jquery.com/jquery-latest.js"></script>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<!-- Popper JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script>
<title>Document</title>
<style>
.container {
border: 1px solid red;
margin-top: 30px;
border-radius: 10px;
}
.btn {
width: 100px;
background-color: rgb(111, 214, 216);
background: red;
}
</style>
</head>
<body>
<div class="container">
<table class="table table-bordered">
<thead>
<tr>
<th>ID</th>
<th>Full name</th>
<th>Age</th>
<th>Address</th>
</tr>
</thead>
<tbody>
<?= $tr ?>
</tbody>
</table>
</div>
<div class="container">
<!-- method: get => $_GET => du lieu se duoc hien len url -->
<!-- method: post => $_POST => du lieu se duoc an -->
<form method="post">
<div class="form-group">
<label for="user">
<h6>Full name: </h6>
</label>
<input type="text" name="name" class="form-control" placeholder="Enter name" id="name">
</div>
<div class="form-group">
<label for="age">
<h6>Age: </h6>
</label>
<input type="number" name="age" class="form-control" placeholder="Enter age" id="age">
</div>
<div class="form-group">
<label for="pwd">
<h6>Address: </h6>
</label>
<input type="text" name="address" class="form-control" placeholder="Enter address" id="address">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
</body>
</html>
<?php
$name = $age = $address = '';
if (isset($_POST['name'])) {
$name = $_POST['name'];
}
if (isset($_POST['age'])) {
$age = $_POST['age'];
}
if (isset($_POST['address'])) {
$address = $_POST['address'];
}
if ($name != '' && $age != '' && $address != '') {
$sql= " INSERT INTO student(name, age, address) VALUES ('$name','$age','$address')";
insert_remove($sql);
}