Use PHP+MySql form to add, delete, modify, query, see a lot of simple cases, write a simple form, please look at the code below, there are errors in the place hope big guys to teach!

Database name ITcast, table name emp_INFO

CREATE TABLE `emp_info` ( `e_id` int(10) unsigned NOT NULL AUTO_INCREMENT, `e_name` varchar(20) NOT NULL, `e_dept` varchar(20) NOT NULL, `date_of_birth` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, `date_of_entry` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' )CHARSET=utf8; INSERT INTO 'emp_info' VALUES (11,' 2019-05-22 17:42:00','2019-05-22 17:42:00') (12, 'bill', 'human resources',' 2018-11-02 20:30:00 ', '2019-05-26 20:30:00');Copy the code

The directory structure

It doesn’t matter what images and JS don’t have, it just doesn’t look good

Query front page list_html.php

<? php if(! defined('APP')) die('error! '); ? > <! < span style>. Box {margin-bottom :20px; margin-bottom :20px; } .box .title{font-size:22px; font-weight:bold; text-align:center; } .box table{width:100%; margin-top:10px; border-collapse:collapse; font-size:12px; border:1px solid #B5D6E6; min-width:460px; } .box table th,.box table td{height:20px; border:1px solid #B5D6E6; } .box table th{background-color:#E8F6FC; font-weight:normal; } .box table td{text-align:center; } .box a{color:#444; text-decoration:none; } .box a:hover{text-decoration:underline; } .search{padding:10px 0; float:right; font-size:12px; } <div class="box"> <div class="title"> </div> < table border = "1" > < tr > < "width =" 5% "> ID < / th > < th > name < / th > < th > departments < / th > < th > date of birth < / th > < th > < / th > < induction time th Width ="25%"> </th> </tr> <? php if(! empty($rows)){ ? > <? php foreach($rows as $row){ ? > <tr> <td><? php echo $row['e_id']; ? ></td> <td><? php echo $row['e_name']; ? ></td> <td><? php echo $row['e_dept']; ? ></td> <td><? php echo $row['date_of_birth']; ? ></td> <td><? php echo $row['date_of_entry']; ? ></td> <td> <div align="center"> <span> <img src="images/edt.gif" width="16" height="16" /><a href="<? php echo './empUpdate.php? e_id='.$row['e_id'] ? > "> edit < / a > & have spent &nbsp; <a href="<? php echo './empDel.php? e_id='.$row['e_id'] ? > "> < img SRC =" images/del. GIF "width =" 16 "height =" 16 "/ > delete < / a > < / span > < / div > < / td > < / tr > <? php } ? > <? php }else{ ? >< tr><td colspan="6"> Query result does not exist! </td></tr> <? php } ? > < / table > < / form > < div class = "search" > < a href = ". / empAdd. PHP "> add employees < / a > < / div > < / body > < / HTML >Copy the code

Query the background page showlist.php

<? Header ('content-type:text/ HTML; charset=utf-8'); $dbms = 'mysql'; $host = 'localhost'; $host = 'localhost'; $port = ''; $dbname = 'itcast'; $charset = 'utf8'; $user = 'root'; $pwd = 'root'; $dsn = "$dbms:host=$host; port=$port; dbname=$dbname; charset=$charset"; try{ $pdo = new PDO($dsn,$user,$pwd); $SQL = 'select * from emp_info'; $result = $pdo->query($SQL); $rows = array(); / / execution success / / iterate through the result set while ($row = $result - > the fetch ()) {$rows [] = $row; } }catch(PDOException $e){ echo $e->getMessage().'<br>'; } define('APP', 'itcast'); // Load the view page and display the data require './list_html.php';Copy the code

Add the foreground page add_html.php

<? php if(! defined('APP')) die('error! '); ? > <! < font style =" text-align: center; text-align: center;" href="./js/jquery.datetimepicker.css"/ > <script src="./js/jquery.js"></script> <script src="./js/jquery.datetimepicker.js"></script> <script> $(function(){ $('#date_of_birth').datetimepicker({lang:'ch'}); $('#date_of_entry').datetimepicker({lang:'ch'}); }); </script> <style> body{background-color:#eee; margin:0; padding:0; } .box{width:400px; margin:15px auto; padding:20px; border:1px solid #ccc; background-color:#fff; } .box h1{font-size:20px; text-align:center; } .profile-table{margin:0 auto; } .profile-table th{font-weight:normal; text-align:right; } .profile-table input[type="text"]{width:180px; border:1px solid #ccc; height:22px; padding-left:4px; } .profile-table .button{background-color:#0099ff; border:1px solid #0099ff; color:#fff; width:80px; height:25px; margin:0 5px; cursor:pointer; } .profile-table .td-btn{text-align:center; padding-top:10px; } .profile-table th,.profile-table td{padding-bottom:10px; } .profile-table td{font-size:14px; } .profile-table .txttop{vertical-align:top; } .profile-table select{border:1px solid #ccc; min-width:80px; height:25px; } .profile-table .description{font-size:13px; width:250px; height:60px; border:1px solid #ccc; <div class="box"> <div class="box"> <h1> <form method="post" action="./ empadd.php "> <table Class = "profile - table > < tr > < th > name: < / th > < td > < input type =" text "name =" e_name "/ > < / td > < / tr > < tr > < th > subordinate departments: < / th > < td > < input type = "text" name = "e_dept" / > < / td > < / tr > < tr > < th > date of birth: < / th > < td > < input id = "date_of_birth" name = "date_of_birth" type = "text" > < / td > < / tr > < tr > < th > entry date: </th><td><input id="date_of_entry" name="date_of_entry" type="text" ></td></tr> <tr><td colspan="2" class="td-btn"> </td></tr> </td> </table> </form> </div> </body> </html>Copy the code

Add background page

<? php header ('content-type:text/html; chaset=utf-8'); $dbms = 'mysql'; $host = 'localhost'; $host = 'localhost'; $port = ''; $dbname = 'itcast'; $charset = 'utf8'; $user = 'root'; $pwd = 'root'; $dsn = "$dbms:host=$host; port=$port; dbname=$dbname; charset=$charset"; try{ $pdo = new PDO($dsn,$user,$pwd); // Check whether a form has been submitted. $fields = array('e_name','e_dept','date_of_birth','date_of_entry'); $fields = array('e_name',' E_dept ','date_of_entry'); $values = array(); $values = array(); Foreach ($fields as $k => $v){$data = isset($_POST[$v])? $_POST[$v] : ''; If ($data== ") die($v. $fields[$k] = "' $v '"; $fields[$k] =" $v' "; $values[] = "'$data'"; $data [] = "$data'"; $fields = implode(',', $fields); $fields = implode(',', $fields); $values = implode(',', $values); $values = implode(',', '); $SQL = "insert into 'emp_info' ($fields) values ($values)"; // Insert into 'emp_info' ($fields) values ($values); If ($res = $pdo->query($SQL)){// return the showlist.php header('Location:./ showlist.php '); // Stop the script die; }else{// Failed to execute die(' Employee added failed! '); } } }catch(PDOException $e){ echo $e->getMessage().'<br>'; } define ('APP','itcast'); require './add_html.php'; ? >Copy the code

Modify the front page update_html.php

<? php if(! defined('APP')) die('error! '); ? > <! < font style =" text-align: center; text-align: center; text-align: center; href="./js/jquery.datetimepicker.css"/ > <script src="./js/jquery.js"></script> <script src="./js/jquery.datetimepicker.js"></script> <script> $(function(){ $('#date_of_birth').datetimepicker({lang:'ch'}); $('#date_of_entry').datetimepicker({lang:'ch'}); }); </script> <style> body{background-color:#eee; margin:0; padding:0; } .box{width:400px; margin:15px auto; padding:20px; border:1px solid #ccc; background-color:#fff; } .box h1{font-size:20px; text-align:center; } .profile-table{margin:0 auto; } .profile-table th{font-weight:normal; text-align:right; } .profile-table input[type="text"]{width:180px; border:1px solid #ccc; height:22px; padding-left:4px; } .profile-table .button{background-color:#0099ff; border:1px solid #0099ff; color:#fff; width:80px; height:25px; margin:0 5px; cursor:pointer; } .profile-table .td-btn{text-align:center; padding-top:10px; } .profile-table th,.profile-table td{padding-bottom:10px; } .profile-table td{font-size:14px; } .profile-table .txttop{vertical-align:top; } .profile-table select{border:1px solid #ccc; min-width:80px; height:25px; } .profile-table .description{font-size:13px; width:250px; height:60px; border:1px solid #ccc; </style> </head> <body> <div class="box"> <h1> <form method="post"> <table class="profile-table"> < tr > < th > name: < / th > < td > < input type = "text" name = "e_name" value = "<? php echo $emp_info['e_name']; ? > "/ > < / td > < / tr > < tr > < th > subordinate departments: < / th > < td > < input type =" text "name =" e_dept "value =" <? php echo $emp_info['e_dept']; ? > "/ > < / td > < / tr > < tr > < th > date of birth: < / th > < td > < input id =" date_of_birth "name =" date_of_birth "type =" text "value =" <? php echo $emp_info['date_of_birth']; ? > "> < / td > < / tr > < tr > < th > entry date: < / th > < td > < input id =" date_of_entry "name =" date_of_entry "type =" text "value =" <? php echo $emp_info['date_of_entry']; ? > "> < / td > < / tr > < tr > < td colspan =" 2 "class =" td - BTN "> < input type =" submit "value =" save data "class =" button "/ > < input Type = "reset" value = "fill" class = "button" / > < / td > < / tr > < / table > < / form > < / div > < / body > < / HTML >Copy the code

Modify the background page emupdate.php

<? Header ('content-type:text/ HTML; charset=utf-8'); $dbms = 'mysql'; $host = 'localhost'; $host = 'localhost'; $port = ''; $dbname = 'itcast'; $charset = 'UTF8'; $user = 'root'; $pwd = 'root'; $dsn = "$dbms:host=$host; port=$port; dbname=$dbname; charset=$charset"; try{ $pdo = new PDO($dsn,$user,$pwd); $e_id = isset($_GET['e_id'])? intval($_GET['e_id']) : 0; // Check whether the POST data has been submitted if(! Empty ($_POST)){$update = array(); / / $fields defined legal field array = array (' e_name ', 'e_dept', 'date_of_birth', 'date_of_entry'); Foreach ($fields as $v){$data = isset($_POST[$v])? $_POST[$v] : ''; If ($data== ") die($v. $update[] = "'$v '='$data'"; $update[] =" $v' ='$data'"; $update_str = implode(',', $update); $SQL = "update 'emp_info' set $update_str WHERE 'e_id' =$e_id"; if($res = $pdo->query($sql)){ header("Location: ./showList.php"); die; }else{die(' failed to modify employee information '); $pdo = new pdo ($DSN,$user,$PWD); $pdo = new pdo ($DSN,$user,$PWD); $SQL = "select * from 'emp_info' WHERE 'e_id' =$e_id"; $result = $pdo->query($sql); $rows = array(); while($row = $result->fetch()){ $rows[] = $row; } } }catch(PDOEXception $e){ echo $e->getMessage().'<br>'; echo $e->getLine().'<br>'; echo $e->__toString().'<br>'; } // Display employee modification page define('APP', 'itcast'); require './update_html.php';Copy the code

Delete the page empadd.php

<? php header ('Conent-type:text/html; charset=utf-8'); $dbms = 'mysql'; $host = 'localhost'; $host = 'localhost'; $port = ''; $dbname = 'itcast'; $charset = 'utf8'; $user = 'root'; $pwd = 'root'; $dsn = "$dbms:host=$host; port=$port; dbname=$dbname; charset=$charset"; try{ $pdo = new PDO($dsn,$user,$pwd); $e_id = isset($_GET['e_id'])? intval($_GET['e_id']) : 0; DELETE FROM Employee WHERE ID='$ID '$SQL = "DELETE FROM emp_info WHERE e_id = $e_id"; $result = $pdo->query($sql); $rows = array(); while($row = $result->fetch()){ $rows[] = $row; } }catch(PDOException $e){ echo $e->getMessage().'<br>'; } define ('APP','itcast'); // Refresh the data header ("Location:./ showlist.php ");Copy the code

Finally, provide a check_form.lib.php to check the username password format

<? Function checkUsername($username){if($username){if($username){if($username){if($username); Preg_match (' / ^ [\ w \ - \ {4 e00} x x {9 fa5}] {2, 10} $/ u ', $username)) {return 'user name format does not conform to the requirements; } return true; } function checkPassword($password){if(! Preg_match (' / ^ 6 16th} {$\ w/', $password)) {return 'password format is not in conformity with the requirements; } return true; } function checkEmail($email){if(strlen($email) > 40){return 'Email length is not valid '; }elseif(! preg_match('/^[a-z0-9]+@([a-z0-9]+\.) +[a-z]{2,4}$/ I ',$email)){return 'email format is invalid '; } return true; } function checkQQ($QQ){if($QQ! Preg_match (' / ^ (1-9] [0-9] {4, 20} $/ ', $qq)) {return 'qq number format is not in conformity with the requirements; } return true; } function checkPhone($num){if(! Preg_match (' / ^ 1 [358] \ d {9} $/ ', $num)) {return 'cell phone number is not in conformity with the requirements; } return true; } function checkURL($URL){if(strlen($URL) > 200){return 'URL length is not valid '; }elseif(! Preg_match (' / ^ HTTP: / / / / [-] a-z \ d + (\. \ \ w /] +) + $/ I ', $url)) {return 'url format does not conform to the requirements; } return true; }Copy the code
The modification page here is not very good, there is no display of data, but the modification function can be realized, do not want to modify, intention to help me to modify, and in the comment area below the message to tell me!

Demo, open the home page, to achieve the query function

Add employees

Enter employee information

Click Save to automatically return to the query page, showing the newly added data

The rest will not be demonstrated, have a question to leave a message!