PHP之连接mysql小练习

Posted 0Nullptr

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP之连接mysql小练习相关的知识,希望对你有一定的参考价值。


mysql 
Test.sql
1
-- phpMyAdmin SQL Dump 2 -- version 4.6.6 3 -- https://www.phpmyadmin.net/ 4 -- 5 -- Host: localhost:3306 6 -- Generation Time: 2017-05-16 21:49:46 7 -- 服务器版本: 5.6.35 8 -- PHP Version: 7.1.1 9 10 SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO"; 11 SET time_zone = "+00:00"; 12 13 14 /*!40101 SET @[email protected]@CHARACTER_SET_CLIENT */; 15 /*!40101 SET @[email protected]@CHARACTER_SET_RESULTS */; 16 /*!40101 SET @[email protected]@COLLATION_CONNECTION */; 17 /*!40101 SET NAMES utf8mb4 */; 18 19 -- 20 -- Database: `Test` 21 -- 22 23 -- -------------------------------------------------------- 24 25 -- 26 -- 表的结构 `table6` 27 -- 28 29 CREATE TABLE `table6` ( 30 `ID` int(11) NOT NULL, 31 `Username` varchar(50) NOT NULL, 32 `Password` varchar(50) NOT NULL, 33 `Phone` varchar(15) NOT NULL, 34 `Date` datetime(2) NOT NULL 35 ) ENGINE=InnoDB DEFAULT CHARSET=utf8; 36 37 -- 38 -- 转存表中的数据 `table6` 39 -- 40 41 INSERT INTO `table6` (`ID`, `Username`, `Password`, `Phone`, `Date`) VALUES 42 (1, Admin_1, 123456, 0, 0000-00-00 00:00:00.00); 43 44 -- 45 -- Indexes for dumped tables 46 -- 47 48 -- 49 -- Indexes for table `table6` 50 -- 51 ALTER TABLE `table6` 52 ADD PRIMARY KEY (`ID`); 53 54 -- 55 -- 在导出的表使用AUTO_INCREMENT 56 -- 57 58 -- 59 -- 使用表AUTO_INCREMENT `table6` 60 -- 61 ALTER TABLE `table6` 62 MODIFY `ID` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=2; 63 /*!40101 SET [email protected]_CHARACTER_SET_CLIENT */; 64 /*!40101 SET [email protected]_CHARACTER_SET_RESULTS */; 65 /*!40101 SET [email protected]_COLLATION_CONNECTION */;

 1 <?
 2 echo 3 <html>
 4 <head>
 5     <title>用户注册</title>
 6 </head>
 7 <body>
 8     <form method="post" action="7-2.php">
 9     <p><font size="18">用户注册</p>
10     <table border="1" >
11     
12     <tr><td>用户名:</td>
13         <td><input name="xm" type="text"  size="12"></td>
14         <td><font color="red">*6-12个字符(数字字母下划线)</font></td>
15     </tr>
16 
17     <tr><td>密码:</td>
18         <td><input name="pwd1" type="password" size="16"></td>
19         <td><font color="red">*6-16个数字</font></td>
20     </tr>
21 
22     <tr><td>确认密码:</td>
23         <td><input name="pwd2" type="password" size="16"></td>
24         <td><font color="red">*必须和密码一致</font></td>
25     </tr>
26 
27     <tr><td>电话:</td>
28         <td><input name="phone" type="text" size="11"></td>
29         <td><font color="red">*11位数字,第一位为1</font></td>
30     </tr>
31 
32     <tr><td colspan="3"><input type="submit" value="注册">
33                         <input type="reset" value="取消">
34     </td>
35     </tr>
36 
37     </table>
38     </form>
39 </body>
40 </html>‘;
41 ?>

 1 <?
 2 
 3     $conn=mysqli_connect(‘localhost‘,‘User‘,‘1‘,‘Test‘);
 4     mysqli_query("set names ‘UTF-8‘ ");
 5     $result=mysqli_query($conn,"Select * FROM table6 ");
 6     $row=$result->fetch_assoc();
 7     $Username=$row[‘Username‘];
 8     $Password=$row[‘Password‘];
 9 
10 
11     if( strlen($_POST["xm"])<6 || strlen($_POST["xm"])>12 ){
12         echo "<script>alert(‘用户名长度错误‘);location.href=‘7-1.php‘;</script>";
13     }
14     else if ( strpos($_POST["xm"], "_")==false) {
15         echo "<script>alert(‘用户名中应存在下划线‘);location.href=‘7-1.php‘;</script>";
16     }
17     else if ( preg_match(‘/[a-zA-Z]/‘, $_POST["xm"])==false) {
18         echo "<script>alert(‘用户名中应存在英文字母‘);location.href=‘7-1.php‘;</script>";
19     }
20     else if ( preg_match(‘/[0-9]/‘, $_POST["xm"])==false) {
21         echo "<script>alert(‘用户名中应存在数字‘);location.href=‘7-1.php‘;</script>";
22     }
23     else if ( $_POST["xm"]!=$Username) {
24         echo "<script>alert(‘用户名错误‘);location.href=‘7-1.php‘;</script>";
25     } 
26     else if ( strlen($_POST["pwd1"])<6 || strlen($_POST["pwd1"])>16 ) {
27         echo "<script>alert(‘密码长度错误!‘);location.href=‘7-1.php‘;</script>";
28     }
29     else if ($_POST["pwd1"]!=$_POST["pwd2"]) {
30         echo "<script>alert(‘两次密码不一致‘);location.href=‘7-1.php‘;</script>";
31     }
32     else if ($_POST["pwd1"]!=$Password) {
33         echo "<script>alert(‘密码错误!‘);location.href=‘7-1.php‘;</script>";
34     } 
35     else if (strlen($_POST["phone"])!=11) {
36         echo "<script>alert(‘电话长度错误‘);location.href=‘7-1.php‘;</script>";
37     }
38     else if (substr($_POST["phone"],0,1)!="1") {
39         echo "<script>alert(‘电话第一位应该为1‘);location.href=‘7-1.php‘;</script>";
40     }
41     else if ( strpos($_POST["phone"], "_")) {
42         echo "<script>alert(‘电话中不应该存在下划线‘);location.href=‘7-1.php‘;</script>";
43     }
44     else if ( preg_match(‘/[a-zA-Z]/‘, $_POST["phone"])==true) {
45         echo "<script>alert(‘电话中不应该存在英文字母‘);location.href=‘7-1.php‘;</script>";
46     }
47     else{
48         $up="update table6 set Phone = "." ‘ ".$_POST["phone"]." ‘ "."where Username = ‘Admin_1‘ ";
49         $result=mysqli_query($conn,$up);
50         $date=date(‘Y-m-d H:i:s‘);
51         $result=mysqli_query($conn,"update table6 set Date = "." ‘ ".$date." ‘ "."where Username = ‘Admin_1‘ ");
52         $result=mysqli_query($conn,"Select * FROM table6 ");
53         $row=$result->fetch_assoc();
54 
55         echo "注册成功"."<br/>"; 
56         echo "<table border=1>";
57         echo "<tr><td>ID: </td><td>".$row[‘ID‘]."</td></tr>";
58         echo "<tr><td>Username </td><td>".$row[‘Username‘]."</td></tr>";
59         echo "<tr><td>Password </td><td>".$row[‘Password‘]."</td></tr>";
60         echo "<tr><td>Phone </td><td>".$row[‘Phone‘]."</td></tr>";
61         echo "<tr><td>Register Time </td><td>".$row[‘Date‘]."</td></tr>";
62         echo "</table>"; 
63         
64     }
65 
66 ?>

 



以上是关于PHP之连接mysql小练习的主要内容,如果未能解决你的问题,请参考以下文章

随手写的一个检测php连接mysql的小脚本

php课程---练习连接数据库及增删改

ORM之youku项目小练习(上)

谁有php的小程序啊带连接mysql数据库的

PHP面向对象练习2

PHP连接MySQL之原生MySQL函数总结