
PHP增删改查数据库(前端+后台)
发布日期:2021-05-06 10:56:21
浏览次数:29
分类:精选文章
本文共 15766 字,大约阅读时间需要 52 分钟。
PHP增删改查数据库(前端+后台)
要求:
-
首页导航栏中内置功能
-
查看数据库
-
点击Edit修改数据库内容
-
点击Delete后删除数据库内此记录,返回首页输出删除成功。
-
向数据库里增加数据
-
向搜索框输入查询的关键字
之后输出查询结果:
目录结构:
代码如下:
index.php:query("select * from contacts order by id"); $statement->setFetchMode(PDO::FETCH_ASSOC); $records = []; while ($row = $statement->fetch()) { $records[] = $row; } //display records in bootstrap table include_once("includes/viewContacts.php"); } else if ($action == 'editContacts') { $id = $_GET['id']; $firstname = $_GET['firstname']; $lastname = $_GET['lastname']; $Email = $_GET['email']; $Mobile = $_GET['mobile']; $photoname = $_GET['photoname']; $first_name = ''; $last_name = ''; $email = ''; $mobile = ''; $filename = ''; $errors = []; if (isset($_POST['submit'])) { //validation if (isset($_POST['first_name'])) { //设定first_name的输入规范 $first_name = $_POST['first_name']; if (strlen($first_name) == 0) { $errors['first_name'] = 'First Name is missing input'; } elseif (!ctype_alpha($first_name)) { $errors['first_name'] = 'Enter a valid First Name'; } } if (isset($_POST['last_name'])) { //设定last_name的输入规范 $last_name = $_POST['last_name']; if (strlen($last_name) == 0) { $errors['last_name'] = 'Last Name is missing input'; } elseif (!ctype_alpha($last_name)) { $errors['last_name'] = 'Enter a valid Last Name'; } } if (isset($_POST['email'])) { //设定email的输入规范 $email = trim($_POST['email']); if (strlen($email) == 0) { $errors['email'] = "Email address is missing input"; } else if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { $errors['email'] = "Enter a valid Email Address"; } } if (isset($_POST['mobile'])) { $mobile = trim($_POST['mobile']); if (strlen($mobile) == 0) { $errors['mobile'] = "Missing input"; } elseif (strlen($mobile) < 10) { $errors['mobile'] = "Mobile number must be 10 digits"; } else if (!ctype_digit($mobile)) { $errors['mobile'] = "Enter a valid mobile number"; } } // image $filename = $_FILES["image"]["name"]; $temp_file = $_FILES["image"]["tmp_name"]; $type = $_FILES["image"]["type"]; $size = $_FILES["image"]["size"]; $errorLevel = $_FILES["image"]["error"]; $error_messages = [ "Upload successful", "File exceeds maximum upload size specified by default", "File exceeds size specified by MAX_FILE_SIZE", "File only partially uploaded", "Form submitted with no file specified", "", "No temporary folder", "Cannot write file to disk", "File type is not permitted" ]; $destination = 'D:/xampp/htdocs/A1P4/img/'; $target_file = $destination . $filename; $max = 3000000; if ($errorLevel > 0) { // Set the error message to the errors array $errors["image"] = $error_messages[$errorLevel]; } else { if (file_exists($temp_file)) { $size = $_FILES["image"]["size"]; if ($size <= $max) { $permitted = ["gif", "jpg", "jpeg", "png"]; $ext = pathinfo($filename, PATHINFO_EXTENSION); if (in_array($ext, $permitted)) { move_uploaded_file($temp_file, $target_file); //$errors["image"] = "The file $filename has been uploaded."; } else { $errors["image"] = "$filename type is not permitted"; } } else { $errors["image"] = "$filename is too big – upload failed"; } } else { $errors["image"] = "File upload has failed"; } } if (count($errors) == 0) { $values = [$first_name, $last_name, $email, $mobile, $filename]; $dsn = 'mysql:host=localhost;dbname=contactsdb'; $username = 'root'; $password = ''; $conn = new PDO($dsn, $username, $password); $sql = "update contacts set first_name=? ,last_name=?,email=?,mobile=?,photo_filename=? where id = $id"; $statement = $conn->prepare($sql); $success = $statement->execute($values); if ($success) { include_once 'index.php'; //如果无错误就返回主页 echo"Edit contact record successed!"; } else { include_once 'index.php'; echo"Insert contact record failed!"; } } else { include_once 'includes/editContacts.php'; //有不符合规范的就返回form并提示 } } else { include_once 'includes/editContacts.php'; } } else if ($action == 'deleteContacts') { $id = $_GET['id']; $dsn = 'mysql:host=localhost;dbname=contactsdb'; $username = 'root'; $password = ''; $conn = new PDO($dsn, $username, $password); $sql = "delete from contacts where id = $id"; $statement = $conn->query($sql); $success = $statement->execute(); if ($success) { include_once 'index.php'; echo "Delete booking record successed!"; } else { include_once 'index.php'; echo "Delete booking record failed!"; } } else if ($action == 'addcontacts') { $first_name = ''; $last_name = ''; $email = ''; $mobile = ''; $filename = ''; $errors = []; if (isset($_POST['submit'])) { //validation if (isset($_POST['first_name'])) { //设定first_name的输入规范 $first_name = $_POST['first_name']; if (strlen($first_name) == 0) { $errors['first_name'] = 'First Name is missing input'; } elseif (!ctype_alpha($first_name)) { $errors['first_name'] = 'Enter a valid First Name'; } } if (isset($_POST['last_name'])) { //设定last_name的输入规范 $last_name = $_POST['last_name']; if (strlen($last_name) == 0) { $errors['last_name'] = 'Last Name is missing input'; } elseif (!ctype_alpha($last_name)) { $errors['last_name'] = 'Enter a valid Last Name'; } } if (isset($_POST['email'])) { //设定email的输入规范 $email = trim($_POST['email']); if (strlen($email) == 0) { $errors['email'] = "Email address is missing input"; } else if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { $errors['email'] = "Enter a valid Email Address"; } } if (isset($_POST['mobile'])) { $mobile = trim($_POST['mobile']); if (strlen($mobile) == 0) { $errors['mobile'] = "Missing input"; } elseif (strlen($mobile) < 10) { $errors['mobile'] = "Mobile number must be 10 digits"; } else if (!ctype_digit($mobile)) { $errors['mobile'] = "Enter a valid mobile number"; } } // image $filename = $_FILES["image"]["name"]; $temp_file = $_FILES["image"]["tmp_name"]; $type = $_FILES["image"]["type"]; $size = $_FILES["image"]["size"]; $errorLevel = $_FILES["image"]["error"]; $error_messages = [ "Upload successful", "File exceeds maximum upload size specified by default", "File exceeds size specified by MAX_FILE_SIZE", "File only partially uploaded", "Form submitted with no file specified", "", "No temporary folder", "Cannot write file to disk", "File type is not permitted" ]; $destination = 'D:/xampp/htdocs/A1P4/img/'; $target_file = $destination . $filename; $max = 3000000; if ($errorLevel > 0) { // Set the error message to the errors array $errors["image"] = $error_messages[$errorLevel]; } else { if (file_exists($temp_file)) { $size = $_FILES["image"]["size"]; if ($size <= $max) { $permitted = ["gif", "jpg", "jpeg", "png"]; $ext = pathinfo($filename, PATHINFO_EXTENSION); if (in_array($ext, $permitted)) { move_uploaded_file($temp_file, $target_file); //$errors["image"] = "The file $filename has been uploaded."; } else { $errors["image"] = "$filename type is not permitted"; } } else { $errors["image"] = "$filename is too big – upload failed"; } } else { $errors["image"] = "File upload has failed"; } } if (count($errors) == 0) { $values = [$first_name, $last_name, $email, $mobile, $filename]; $dsn = 'mysql:host=localhost;dbname=contactsdb'; $username = 'root'; $password = ''; $conn = new PDO($dsn, $username, $password); $sql = "insert into contacts (first_name,last_name,email,mobile,photo_filename)values(?,?,?,?,?)"; $statement = $conn->prepare($sql); $success = $statement->execute($values); if ($success) { include_once 'index.php'; //如果无错误就返回主页 echo"Insert contact record successed!"; } else { include_once 'index.php'; echo"Insert contact record failed!"; } } else { include_once 'includes/addContacts.php'; //有不符合规范的就返回form并提示 } } else { include_once 'includes/addContacts.php'; } } else if ($action == 'seachercontacts') { $keyword = ''; $errors = []; if (isset($_POST['send'])) { //validation if (isset($_POST['keyword'])) { //设定keyword的输入规范 $keyword = $_POST['keyword']; if (strlen($keyword) == 0) { $errors['keyword'] = 'keyword is missing input'; } if (count($errors) == 0) { $conn = mysqli_connect('localhost', 'root', '', 'contactsdb'); $res = mysqli_query($conn, "select * from contacts where first_name like '%$keyword%' or last_name like '%$keyword%' or email like '%$keyword%' or mobile like '%$keyword%' or photo_filename like '%$keyword%'") or die(mysqli_error($conn)); $records = []; while ($row = mysqli_fetch_assoc($res)) { $records[] = $row; } include_once("includes/viewContacts.php"); } } else { include_once 'includes/seacherContacts.php'; //有不符合规范的就返回form并提示 } } else { include_once 'includes/seacherContacts.php'; } }} else { include_once('includes/content.php');}include_once('includes/footer.php');
includes/addConacts.php:
Add Contacts
includes/viewConacts.php:
includes/editConacts.php:
Edit Contacts
includes/seacherConacts.php:
Seacher Contacts
includes/header.php:
E-Bookings Theme
includes/footer.php:
includes/content.php:
WELCOME TO E-CONTACTS THEME
I AM A STUDENT IN THE WEB APPLICATION & SERVER MANAGEMENT UNIT
以上!
发表评论
最新留言
哈哈,博客排版真的漂亮呢~
[***.90.31.176]2025年04月09日 05时03分27秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
02、MySQL—数据库基本操作
2019-03-05
RedHat Linux-配置YUM仓库
2019-03-05
Redis数据类型
2019-03-05
1907: 树的路径覆盖
2019-03-05
OpenJDK1.8.0 源码解析————HashMap的实现(一)
2019-03-05
MySQL-时区导致的时间前后端不一致
2019-03-05
2021-04-05阅读小笔记:局部性原理
2019-03-05
将Java编译为本地代码
2019-03-05
go语言简单介绍,增强了解
2019-03-05
2.1 Kubernetes--Pod
2019-03-05
python file文件操作--内置对象open
2019-03-05
ERP/MIS开发 LLBL Gen多表操作
2019-03-05
Remove function
2019-03-05
在没实践机会的前提下,如何跨越级别
2019-03-05
从面试官角度告诉大家如何准备项目方面的描述
2019-03-05
去创业公司不能有一夜暴富的侥幸,更不能指望掉馅饼
2019-03-05
架构师入门:搭建基本的Eureka架构(从项目里抽取)
2019-03-05