PHP develops user registration function of simple book lending system
In the previous section we created the user registration front-end page
This section will implement its function
It is necessary to judge each registration content through javascript, here we will The checkreg() function is defined.
#For example: the user name cannot be empty, the password and confirmation password must be the same, the email address must comply with the specifications, etc.
<script type="text/javascript"> function checkreg() { if (form1.name.value=="") { // 如果真實姓名為空,則顯示警告信息 alert("真實姓名不能為空!"); form1.name.focus(); return false; } if (form1.password.value=="" ) { // 如果密碼為空,則顯示警告信息 alert("密碼不能為空!"); form1.password.focus(); return false; } if (form1.pwd.value=="" ) { // 如果確認密碼為空,則顯示警告信息 alert("確認密碼不能為空!"); form1.pwd.focus(); return false; } // 兩次密碼應(yīng)一樣 if (form1.password.value!=form1.pwd.value && form1.password.value!="") { alert("兩次密碼不一樣,請確認!"); form1.password.focus(); return false; } if (form1.email.value=="") { // 如果Email為空,則顯示警告信息 alert("Email不能為空!"); form1.email.focus(); return false; } // 檢查email格式是否正確 else if (form1.email.value.charAt(0)=="." || form1.email.value.charAt(0)=="@"|| form1.email.value.indexOf('@', 0) == -1 || form1.email.value.indexOf('.', 0) == -1 || form1.email.value.lastIndexOf("@")==form1.email.value.length-1 || form1.email.value.lastIndexOf(".")==form1.email.value.length-1) { alert("Email的格式不正確!"); form1.email.select(); return false; } return true; } </script>
The next step is to add various registration information to the database by clicking the "Register" button submit
Here, the POST method is used to obtain various values, and the SQL statement INSERT INTO is used to enter the text box The entered user name, password and other information are added to the database.
After successful registration, the automatic ID of the registered user will be obtained.
<?php if($_POST['submit']){ // 取得網(wǎng)頁的參數(shù) $name=$_POST['name']; $password=$_POST['password']; $email=$_POST['email']; $tel=$_POST['tel']; $address=$_POST['address']; // 加密密碼 //$password=md5($password); // 連接數(shù)據(jù)庫,注冊用戶 $SQL ="INSERT INTO user(name, password, email, tel, address) VALUES('$name','$password','$email', '$tel','$address')"; mysqli_query($link,$sql); // 獲得注冊用戶的自動id,以后使用此id才可登錄 $result=mysqli_query($link,"select last_insert_id()"); $re_arr=mysqli_fetch_array($result); $id=$re_arr[0]; //注冊成功,自動登錄,注冊session變量 $_SESSION['user'] = null; $user=$id; echo "<script language=javascript>alert('注冊成功,進入首頁!');window.location='index.php'</script>"; } ?>
After successful registration, you can jump to the login page.