??? ??? ???? PHP ?? ?? ????
?? ??? ?? ???? ??? ?????. ?? ????? ? ?? ????? ????:
??? ??
????
?? ????
???? ? ?? ????? ??? ? ??? ???? POST ???? connect.php ???? ?????.
POST ???? ???? MySQL ??????? ???? ??? ??? ?????.
??? ??? ????.
<form action="connect.php" method="post"> 用戶名:<input type="text" name="username"><br /> 密碼:<input type="password" name="password"><br /> 重復密碼:<input type="password" name="repassword"><br /> <input type="submit" value="提交"> </form>
? ?? ??? ???? ?? ?? ?????? ???? ?????. ??? ??? ??? ??? ??? ??????.
1. ???? ???? ??
???? ? ? ??? ????? ???? ?? ?? ????? ?????? ??.
????? ?? ???? ??? ???? ????? ???? ????. ???? ??? ???? ??? ?????. ????? ?? ???????.
???? ???? ?? ? ??? ???? ? ?? ??? ? ??? ? ????. ??? ??? Trim? ???? ????? ???? ????? ?? ??? ???????.
if(trim($_POST['password']) != trim($_POST['repassword'])){ exit('兩次密碼不一致,請返回上一頁'); }
2. ? ???? ?????
??? ?? ???? ??? ???? ?? ??????? ?? ???.
???? ???? ??? ????.
??
| ???> < br/> | ||||||
$_POST['??? ??'] ?> | ??? ?? ?> | ||||||
$_POST['password'] ?>< br/> | ?????> ?> |
???? ??? ???? ???? ??? ?? ??? ??? ???? ???.
mysql ??? ????? ???? ????? ?? ???? ??? ????? ???? ? ???. ????? ??? ? ??? ?????. ?? ????? MD5? ????. ???? ?? ??? ??? ??? ??????.
??? ?? ???? ??? ????:
?? / ??>
| ???> | ||||||
$time?> | ??? ?? ???> | ||||||
???? ??? IP |
???? ??? unix ?????
REMOTE_ADDR? IP ??? ????, ip2long? ???? ?? ???? ??? ? ????.
$username = trim($_POST['username']); $password = md5(trim($_POST['password'])); $time = time(); $ip = ip2long($_SERVER['REMOTE_ADDR']);
3. ??????? ????, ??? ????, ????? ? ?? ??? ?????
-
mysqli_connect? ???? ?????? ??? ?????.
??? ???? mysqli_errno? ???? ?? ??? ?????.
??? ???? mysqli_error? ?? ??? ???? ?????. ???? ??
??????? ???? ???? utf8? ?????.
//連接數(shù)據(jù)庫 $conn = mysqli_connect('localhost','root','liwenkaihaha'); //如果有錯誤,存在錯誤號 if(mysqli_errno($conn)){ echo mysqli_error($conn); exit; } mysqli_select_db($conn,'user'); mysqli_set_charset($conn,'utf8');
4. SQL ? ??
?? ??? ??????? ???? ???. ??? ??, ????, ?? ?? ? IP? ????.
SQL ?? ?? ??? ???? ???. ??? SQL ?? ??? ????.
$sql = "insert into user(username,password,createtime,createip) values('" . $username . "','" . $password . "','" . $time . "','" . $ip . "')";
??? ???? ???? ?? ??? ????.
CREATE TABLE IF NOT EXISTS user ( id int(11) NOT NULL, username varchar(30) NOT NULL, password char(32) NOT NULL, createtime int(11) NOT NULL, createip int(11) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
??? ??(?? ?? ??):
ID
| < td width=" 193" valign="top">??? ???>?????> | ?? ???> | createip?> | ||||||||||
??? ??< ??> | ??? ?? ?> < /td> | ???? ?> | ?? ???> | ?>IP ?? ?> ?>?>< /td> |
??? ???, ??? ?? ??? $conn???.
??? SQL ????. $sql? ??? ?? ???? ????.- mysqli_query? ?? SQL ?? MySQL ??? ?? ? ????. $result? ????? ???? true???. ??? ??? ?????. ???? ????? ??? ????? ???? ??? ? ????.
- ?? ???? mysqli_insert_id()? ???? ? ?? ????. ??? ?? ??? ?? ? ID? ?????. ??? ??? ? ?? ??? ? ?? ???? ??? ??? ????.
$result = mysqli_query($conn,$sql); if($result){ echo '注冊成功'; }else{ echo '注冊失敗'; } echo '當前用戶插入的ID為'.mysqli_insert_id($conn);
6. ?????? ??? ????.
resource ??? mysqli_close ??? ?????.
mysqli_close($conn);
??? ??? ?? ?? ?? ??? ???????. ??? ???? ??? ?? ?????.
??? ??? connect.php ??? ??? ????. <?php
if (trim($_POST['password']) != trim($_POST['repassword'])) {
exit('兩次密碼不一致,請返回上一頁');
}
$username = trim($_POST['username']);
$password = md5(trim($_POST['password']));
$time = time();
$ip = $_SERVER['REMOTE_ADDR'];
$conn = mysqli_connect('localhost', 'root', 'liwenkaihaha');
//如果有錯誤,存在錯誤號
if (mysqli_errno($conn)) {
echo mysqli_error($conn);
exit;
}
mysqli_select_db($conn, 'book');
mysqli_set_charset($conn, 'utf8');
$sql = "insert into user(username,password,createtime,createip) values('" . $username . "','" . $password . "','" . $time . "','" . $ip . "')";
$result = mysqli_query($conn, $sql);
if ($result) {
echo '成功';
} else {
echo '失敗';
}
echo '當前用戶插入的ID為' . mysqli_insert_id($conn);
mysqli_close($conn);
?>