
PHP增删改查数据库(前端+后台)
发布日期:2021-05-06 10:56:21
浏览次数:21
分类:技术文章
本文共 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年03月16日 19时26分34秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
python基础的总结
2019-03-04
用python计算水仙花数
2019-03-04
OpenStack-Mitaka 一键安装测试环境脚本
2019-03-04
优秀小程序demo 源码
2019-03-04
如何注册一个免费的企业邮箱(阿里企业邮箱)真实可靠
2019-03-04
背包问题基础-01背包
2019-03-04
尺取-算法详解及例题
2019-03-04
Marvell 98DX51xx / 98DX81xx 系列交换芯片 内部初始化
2019-03-04
智比奈特万兆光口网卡 ZB-10G-1F 驱动安装和带宽测试
2019-03-04
H3C-NS228万兆交换机端口聚合调试报告
2019-03-04
易津开发板FPGA端环路测试代码分析
2019-03-04
AMD推土机架构桌面CPU品牌各代情况
2019-03-04
scrapy分布式爬虫编写流程
2019-03-04
一个Python爬虫工程师的修养
2019-03-04
快速安装laravel框架的IDE提示工具
2019-03-04
初次使用 Supervisor 管理 Laravel 队列进程
2019-03-04
线程的退出
2019-03-04
2-2 畅通工程之局部最小花费问题 (30分)
2019-03-04
数据库第十周作业——第七章课后习题
2019-03-04