abstract:HTML代碼<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8">
HTML代碼
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>注冊頁面</title> </head> <body> <p><label for="email">郵箱:</label><input type="email" id="email" name="email"></p> <p><label for="password">密碼:</label><input type="password" id="password" name="password"></p> <p><button>注冊</button></p> <script type="text/javascript" src="static/jquery-3.3.1.js"></script> <script> $('button').click(function(){ if ($('#email').val().length === 0) { $('#email').after('<span style="color:red">郵箱不能為空</span>').next().fadeOut(2000); $('#email').focus(); return false; } if ($('#password').val().length === 0) { $('#password').after('<span style="color:red">密碼不能為空</span>').next().fadeOut(2000); $('#password').focus(); return false; } else if($('#password').val().length < 6) { $('#password').after('<span style="color:red">密碼不能少于6位</span>').next().fadeOut(2000); $('#password').focus(); return false; } $.ajax({ type: 'post', // 請求類型 url: 'check.php', // 請求的處理腳本 data: { email: $('#email').val(), password: $('#password').val() }, dataType: 'json', success: function(data,status,xhr) { console.log($(this)); // console.log(data.message); if (data.status === 1) { $('#email').after('<span style="color: red"></span>').next().html(data.message).fadeOut(2000); }else { $('button').after('<span style="color: green"></span>').next().html(data.message).fadeOut(2000); } } }); }) </script> </body> </html>
PHP代碼
<?php //連接數(shù)據(jù)庫 $db=@mysqli_connect('127.0.0.1','root','root','phpcn',3306); if(!$db){ exit('數(shù)據(jù)庫連接錯誤:'.mysqli_connect_error()); } $email = htmlspecialchars(trim($_POST['email'])); $password = htmlspecialchars(trim($_POST['password'])); $res=count_number($db,'admin','email',"$email"); if ($res) { $status = 1; $message = '該郵箱已注冊!'; }else{ $sql="INSERT INTO `admin` (email,password) VALUES ('{$email}','{$password}')"; $return=mysqli_query($db,$sql); if($return){ $status = 0; $message = '注冊成功!'; }else{ $status=0; $message ='注冊失敗!'; } } mysqli_close($db); echo json_encode(['status' => $status, 'message' => $message]); function count_number($db,$table,$value,$key){ $sql="SELECT COUNT(*) AS count_number FROM ".$table." WHERE ".$value.'="'.$key.'"'; $return=mysqli_query($db,$sql); $return=mysqli_fetch_assoc($return); return $return['count_number']; }
Correcting teacher:查無此人Correction time:2019-05-05 09:08:12
Teacher's summary:完成的不錯。以后都用pdo進行查詢了,但是也要了解下mysqli。繼續(xù)加油。