首先創建數據庫:
數據庫名:kinoko1824;
創建表(用戶表和成績表):
用戶表名:user
主鍵:uid
設置自增
成績表名:score
主鍵:sid
設置自增
在完成之前圖解思路:
數據庫創建好之后新建register.html,代碼如下:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <form action="register.php" method = "POST"> <p>用戶名:<input type="text" name = "uname"></p> <p>密碼:<input type="text" name = "upwd"></p> <p><button>注冊</button></p> <div></div> </form> </body> </html> <script> //獲取form元素: var oForm = document.forms[0]; //console.log(oForm); //給oForm 添加onsubmit事件: oForm.onsubmit = function(){ //判斷如果用戶名、密碼符合規則,則可以提交,否則不能提交; if(flagname&&flagpwd){ return true; }else{ return false; } } //獲取用戶名、密碼元素: var oName = oForm.elements[0]; var oPwd = oForm.elements[1]; var oDiv = document.querySelector('div'); console.log(oDiv); //聲明一個變量控制onsubmit提交: var flagname = null; //添加失焦事件: oName.onblur = function(){ var reg = /^\w{6,10}/; var str = oForm.elements[0].value; if(reg.test(str)){ flagname = true; }else{ oDiv.innerHTML = "包含字母、數字、下划線,在6-10位之間"; flagname = false; } } //聲明一個變量控制onsubmit提交: var flagpwd = null; //添加失焦事件: oPwd.onblur = function(){ var reg = /^\w{6,10}/; var str = oForm.elements[1].value; if(reg.test(str)){ flagpwd = true; }else{ oDiv.innerHTML = "包含字母、數字、下划線,在6-10位之間"; flagpwd = false; } } </script>
新建register.php,代碼如下:
<?php header('content-type:text/html;charset=utf-8'); //接受客戶端的數據: $name = $_POST["uname"]; $pwd = $_POST["upwd"]; //操作數據庫: //連接數據源: $db = mysql_connect("localhost","root","root"); //選擇數據庫: mysql_select_db("kinoko1824",$db); //設置編碼符: mysql_query("set names utf8"); //編寫sql語句: $sql = "INSERT INTO `user`( `uname`, `upwd`) VALUES ('$name','$pwd')"; //執行sql語句: $res = mysql_query($sql); //返回數據: if($res){ echo"<script>alert('注冊成功!');location.href = 'login.html';</script>"; }else{ echo"<script>alert('注冊失敗!');location.href = 'register.html';</script>"; } ?>
新建login.html,代碼如下:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <form action="login.php" method = "POST"> <p>用戶名:<input type="text" name = "uname"></p> <p>密碼:<input type="text" name = "upwd"></p> <p><button>登錄</button></p> <div></div> </form> </body> </html>
新建login.php,代碼如下:
<?php header('content-type:text/html;charset=utf-8'); //接受客戶端的數據: $name = $_POST["uname"]; $pwd = $_POST["upwd"]; //操作數據庫: //連接數據源: $db = mysql_connect("localhost","root","root"); //選擇數據庫: mysql_select_db("kinoko1824",$db); //設置編碼符: mysql_query("set names utf8"); //編寫sql語句: $sql = "SELECT * FROM `user` WHERE uname = '$name'"; //執行sql語句: $res = mysql_query($sql); //將資源轉為數組: $arr = mysql_fetch_array($res); //返回數據: if($arr){ if($arr['upwd']==$pwd){ echo"<script>alert('登錄成功!');location.href = 'score.php';</script>"; }else{ echo"<script>alert('密碼錯誤!');location.href = 'login.html';</script>"; } }else{ echo"<script>alert('用戶名不存在!');location.href = 'register.html';</script>"; } ?>
新建score.php,代碼如下:
<?php //引入public.php文件: include 'public.php'; header('content-type:text/html;charset=utf-8'); echo"<a href = 'add.html'>添加學生成績</a>"; //編寫sql語句: $sql = "SELECT * FROM `score`"; //執行sql語句: $res = mysql_query($sql); //返回的是一個資源,所以用一個數組來接受這個資源: /* $arr = mysql_fetch_array($res); //把數據庫里的數據放到php文件中: echo"<table border = 1 width = 300 cellspacing = 0>"; echo"<tr><th>學號</th><th>姓名</th><th>數學成績</th><th>語文成績</th></tr>"; echo"<tr><td>{$arr['sid']}</td><td>{$arr['sname']}</td><td>{$arr['math']}</td><td>{$arr['chinese']}</td></tr>"; echo"</table>"; */ //每執行一次$arr,添加一次數據,因此采用for循環,減少垃圾代碼: /* for($id=1;$id<8;$id++){ $arr = mysql_fetch_array($res); //把數據庫里的數據放到php文件中: echo"<table border = 1 width = 300 cellspacing = 0>"; echo"<tr><th>學號</th><th>姓名</th><th>數學成績</th><th>語文成績</th></tr>"; echo"<tr><td>{$arr['sid']}</td><td>{$arr['sname']}</td><td>{$arr['math']}</td><td>{$arr['chinese']}</td></tr>"; echo"</table>"; } */ //此時$id的值每添加一次就需要改一次,很不靈活,所以使用while循環語句: while($arr=mysql_fetch_array($res)){ //$arr = mysql_fetch_array($res); //把數據庫里的數據放到php文件中: echo"<table border = 1 width = 600 cellspacing = 0>"; echo"<tr><th>學號</th><th>姓名</th><th>數學成績</th><th>語文成績</th><th>操作</th></tr>"; echo"<tr> <td>{$arr['sid']}</td> <td>{$arr['sname']}</td> <td>{$arr['math']}</td> <td>{$arr['chinese']}</td> <td><a class = 'del' href = 'javascript: ;'>刪除</a>|<a href = 'update.php ?id={$arr['sid']}'>修改</a></td> </tr>"; //注意:寫id={$arr['sid']}時,等號連接處不能有空格。 echo"</table>"; } ?> <style> td{ text-align:center; } </style> <script> //點擊刪除有確認提示: //利用事件委托添加點擊事件: //獲取刪除元素以及其父元素table: var oDel = document.getElementsByClassName('del'); var oTable = document.getElementsByTagName('table')[0]; //console.log(oDel); //給oTable添加點擊事件: oTable.onclick = function(e){ //做事件兼容: var e=e||event; //委托兼容: var target = e.target||e.srcElement; //判斷事件源是否是oDel; if(target.className == 'del'){ //利用confirm來做確認刪除: if(confirm('確認要刪除嗎?')){ //href = 'delete.php ?id={$arr['sid']}' //獲取當前要刪除行對應的id值: var id = target.parentNode.parentNode.children[0].innerHTML; //console.log(id); location.href = "delete.php?id=" + id; } } } </script>
新建add.html,代碼如下:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <form action="add.php" method = "POST"> <input type="hidden" name = "sid"> <p>姓名:<input type="text" name= "sname"></p> <p>語文:<input type="text" name = "schinese"></p> <p>數學:<input type="text" name = "smath"></p> <p><button>添加</button></p> </form> </body> </html>
新建add.php,代碼如下:
<?php header('content-type:text/html;charset=utf-8'); //從客戶端獲取數據: $name = $_POST['sname']; $id = $_POST['sid']; $chinese = $_POST['schinese']; $math = $_POST['smath']; //操作數據庫 //連接數據源 $db = mysql_connect('localhost','root','root'); //選擇數據庫 mysql_select_db('kinoko1824',$db); //設置編碼符 mysql_query('set names utf8'); //編寫sql語句 $sql = "INSERT INTO `score`(`sid`, `sname`, `chinese`, `math`) VALUES ('$id','$name','$chinese','$math')"; //執行sql語句 $res = mysql_query($sql); //判斷返回值 if($res){ echo"<script>alert('添加成績成功!');location.href = 'score.php';</script>"; }else{ echo"<script>alert('添加成績失敗!');location.href = 'add.html';</script>"; } ?>
新建delate.php,代碼如下:
<?php include"public.php"; //此處的id是通過超鏈接路徑來獲取的;用get是因為路徑傳值(將頁面中的數據引入服務器的方法:'路徑?id=$id') $id = $_GET["id"];//此處的id是?后的key. //編寫sql語句: $sql = "DELETE FROM `score` WHERE sid = $id"; //echo"$id"; //執行sql語句: mysql_query($sql); //執行刪除語句時的內建代碼: $row = mysql_affected_rows(); //判斷: if($row){ echo"<script>alert('刪除成功!');location.href = 'score.php';</script>"; }else{ echo"<script>alert('刪除失敗!');location.href = 'score.php';</script>"; } ?>
新建update.php,代碼如下:
<?php include"public.php"; //獲取$id $id = $_GET["id"]; //編寫sql語句,在修改之前需要查詢表中所有信息: $sql = "SELECT * FROM `score` where sid = $id "; //執行sql語句: $res = mysql_query($sql); //用arr來接收資源: $arr = mysql_fetch_array($res); ?> <form action="updateDo.php" method = "post"> <input type="hidden" name = "sid" value = "<?php echo $arr['sid'];?>"> <p>姓名:<input type="text" name = "sname" value = "<?php echo $arr['sname'];?>"></p> <p>數學成績:<input type="text" name = "math" value = "<?php echo $arr['math'];?>"></p> <p>語文成績:<input type="text" name = "chinese" value = "<?php echo $arr['chinese'];?>"></p> <p><button>確認修改</button></p> </form>
新建updateDo.php,代碼如下:
<?php include"public.php"; //獲取客戶端傳遞的數據: $name = $_POST["sname"]; $id= $_POST["sid"]; $math = $_POST["math"]; $chinese= $_POST["chinese"]; //編寫sql語句: $sql = "UPDATE `score` SET `sname`='$name',`chinese`='$chinese',`math`='$math' WHERE sid =$id"; //執行sql語句: mysql_query($sql); //用內建代碼來執行: $res = mysql_affected_rows(); //判斷: if($res){ echo"<script>alert('修改成績成功!');location.href = 'score.php';</script>"; }else{ echo"<script>alert('修改成績失敗!');location.href = 'update.php';</script>"; } ?>
附件:新建public.php,代碼如下:
<?php header('content-type:text/html;charset=utf-8'); //操作數據庫: //連接數據源: $db = mysql_connect("localhost","root","root"); //選擇數據庫: mysql_select_db("kinoko1824",$db); //設置編碼符: mysql_query("set names utf8");