PHP ??? ?? ??
?? ????? ?? ???? ???? reg.php? ???????
regin.php ??? ??? ?????
?? ??? ???????
? ????? ??????
?? ?? ????
??? ?? ??? ?????
SQL ?? ???? ??????? ???? ?????
?? ?? ??? ?????
?? : ??? ???? ? ?? ? ?? ?????. ??? ?? ??? ??? ??????? ???? ??, ??? ??? Zhang San? ?? ??? ???? ??? ????? ? ???. ??? ?? Zhang San? ???? ?? ?? ??? ??? ?? ???? ??? ???? ???.
???? ??? ???????. ?? ???? ???????.
??? ?????. ??????? ?? ???? ?????. ?? ??? ???? conn.php ??? ?????
, ?? ??? ?? ?? ??? ??? ??? ? ????. Trim() ??? ???? ????? ??????. ??? ??:
$name=trim($_POST['username']);
$ ????=$_POST['password'];
trim ??, ?? ??, ??? ?? ?? ?? ?? ?????. ??? ?? ?? ??? ??? ??? ???? Firebug ??? ??? ?? ??? ??? ?? ?? ??? ?? ??? ? ? ????. ??, ??? ??? ???? ???? ??? ???? ? ????
??? ?? ?? ?? ??
trim() ??? ???? ?? ??? ?? ??? ?? ?? ?? ??? ? ???,
?? ? ??? ??? ????
md5( ) ???? ?? ? 32?? ???? ?????
??? ?? Zhang San, ?? 123456, ????? ?????
??? ???? ?????. ??? ????. ?? ??? ??? ?? role
?? ??? ??? ?????? ???? ???
?? ?? ??? ??? ?? ?????? ???? ?? ??? ??? ?????
??? ??? ????
$sql = "select * from user where username='$name'";
$info = mysql_query($sql);
$res = mysql_num_rows($info);
?? ?? $res? ?????. ???? ?? ???? ?????? ???? ???? ?? ???? ?????
??? ?? ?? ?? ??? ?????
?? ??? ???? ?? ??? ??? ?? ???? ???? ???. ?? ???? ???? ???? ???? ????
??? ??? ????:
if(empty($name)){
?????????????echo "<script>alert('??? ??? ??? ? ????');location.href= 'reg.php';</script>" ;
}else if(empty($password)){
?echo "<script>alert('????? ??? ? ????.');location.href='reg.php ';</script>";
}else {
//?? ??
}
?? ?? ??? ??? ????.
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? { ')";
$result = mysql_query($sql1);
if($result){
"alert('?? ??')</script>";
}else {
???????????????echo "< ;script>alert('?? ??')</script>";
???}
??? ????? ??? ????.
<?php require_once("conn.php");//首先鏈接數(shù)據(jù)庫 $name=trim($_POST['username']); //trim函數(shù),過濾空格,如果不加,我們在用戶名后面添加很多空格,提交表單,打開firebug //調(diào)試工具,我們可以到輸入的用戶名后面會有很多空格,使用trim函數(shù),我們可以把表單中空格給過濾掉 $password=$_POST['password']; $sql = "select * from user where username='$name'"; $info = mysql_query($sql); $res = mysql_num_rows($info); if(empty($name)){ echo "<script>alert('用戶名不能為空');location.href='reg.php';</script>"; }else if(empty($password)){ echo "<script>alert('密碼不能為空');location.href='reg.php';</script>"; }else{ if($res){ echo "<script>alert('用戶名已存在');location.href='reg.php';</script>"; }else{ $sql1 ="insert into user(username,password) values('".$name."','" .md5($password)."')"; $result = mysql_query($sql1); if($result){ echo "<script>alert('注冊成功')</script>"; }else{ echo "<script>alert('注冊失敗')</script>"; } } } ?>
??? ?? ??? ???? ????? ???????. ??? ??? ???? ??? ??? ???? ????? ? ????