1. 程式人生 > >PHP之連接mysql小練習

PHP之連接mysql小練習

eset logs int echo mod reg title htm cal


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 @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT
*/; 15 /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; 16 /*!40101 SET @OLD_COLLATION_CONNECTION=@@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小練習