1. Connect to the database
The database is named eating, and the user table is used to store user information
//connect_mysql.php <?php header("Content-type:text/html;charset=utf-8"); //Define encoding and page header("Access-Control-Allow-Origin: *"); //Cross-domain issues session_start(); //Open the session $host ='localhost'; //Host address $database ='eating'; //Database name $username ='root'; //User name of the database $password = '123456'; //Database password /* Connect to the database */ $link = mysqli_connect($host, $username, $password); mysqli_select_db($link, "eating"); mysqli_query($link,"set names'utf8'");//coding conversion if (!$link) { die("could not connect to the database.\n". mysqli_error($link));//Diagnosing connection errors } ?>12345678910111213141516171819202122
2. User login
php implements user login, the user name is the phone number, the php background accepts and parses the json data sent by the front end through the http post method, determines whether the user exists,
if it does not exist, returns -1, if the user exists, then checks whether the user name and password are Consistent, if they are consistent, return 1; if the password is wrong, return -2.
//login.php <?php header("Content-type:text/html;charset=utf-8"); header("Access-Control-Allow-Origin: *"); //跨域 session_start(); if(isset($_POST["button"]) && $_POST["button"] == "登录") { $tel = $_POST["tel"]; //User phone number $password = $_POST["password"]; //User password //include ("connect_mysql.php"); require_once('connect_mysql.php'); //Determine whether the user exists $sql = "SELECT * FROM user WHERE user_tel = '$_POST[tel]';"; $result = mysqli_query($link, $sql); //Execute sql statement and return the result set after query $rows = mysqli_num_rows($result); //returns the number of rows in the result set if ($rows == 0) {//User does not exist $json_arr = array('success' => -1); } else { $sql = "SELECT password FROM user WHERE password = '$_POST[password]' AND user_tel = '$_POST[tel]'"; $result = mysqli_query($link, $sql); $rows = mysqli_num_rows($result); if ($rows == 1) {//The password is correct $json_arr = array('success' => 1); $_SESSION["is_login"] = "true"; $_SESSION["tel"] = $tel; $_SESSION["password"] = $password; } else {//Password error $json_arr = array('success' => -2); } } $login_json = json_encode($json_arr, TRUE); //Convert to json data echo $login_json;//Send json data }123456789101112131415161718192021222324252627282930313233343536373839404142
3. User registration
The user uses the mobile phone number to register when registering an account. The php background first checks whether the information submitted by the user is complete, and returns 0 if it is incomplete; then checks whether the
user's mobile phone number has been registered, 0 if the mobile phone number has been registered, returns to the front end -1; If the mobile phone number has not been registered, check
whether the passwords entered by the user twice are the same. If they are the same, the registration is successful, and 1 is returned to the front end, if not, it returns -2; otherwise, it returns -3.
// register.php <?php header("Access-Control-Allow-Origin: *"); session_start(); if(isset($_POST["submit"]) && $_POST["submit"] == "马上注册") { $tel = $_POST["tel"]; $password = $_POST["password"]; $password_conf = $_POST["confirm"]; $hometown = $_POST["hometown"]; $tasty = $_POST["tasty"]; $type_of_cooking = $_POST["type_of_cooking"]; if($tel == "" || $password == "" || $password == "" || $password_conf == "" || $hometown == "" || $tasty == "" || $type_of_cooking == "") { //用户信息不完整 $json_arr = array('success' => 0); } else { if($password == $password_conf) { include "connect_mysql.php"; //require_once('connect_mysql.php'); $sql = "select tel from user where tel = '$_POST[tel]'"; //SQL语句 $result = mysqli_query($link, $sql); //Execute SQL statement $num = mysqli_num_rows($result); //Count the number of rows affected by the execution result if($num) {//If the user already exists $json_arr = array('success' => -1); } else {//There is no current registered user name $sql_insert = "insert into user (user_tel, password, hometown, tasty, type_of_cooking) values('$_POST[tel]', '$_POST[password]', '$_POST[hometown]','$_POST[tasty]', '$_POST[type_of_cooking]')"; $res_insert = mysqli_query($link, $sql_insert); //$num_insert = mysql_num_rows($res_insert); if($res_insert) {//Registered successfully $json_arr = array('success' => 1); } else {//The system is busy, please try again later $json_arr = array('success' => -3); } } } else {//Passwords are inconsistent $json_arr = array('success' => -2); } } $register_json = json_encode($json_arr, TRUE); echo $register_json; } ?>1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
4. Modify user information
//alter_info.php <?php header("Content-type:text/html;charset=utf-8"); header("Access-Control-Allow-Origin: *"); session_start(); if (isset($_POST["submit"]) && $_POST["submit"] == "确认修改") { include "connect_mysql.php"; //require_once('connect_mysql.php'); $tel = $_POST['tel']; $new_hometown = $_POST['new_hometown']; //家乡 $new_tasty = $_POST['new_tasty']; //flavor $new_type_of_cooking = $_POST['new_type_of_cooking']; // category (dry/soup) $sql = "UPDATE user SET hometown = '$new_hometown', tasty = '$new_tasty', type_of_cooking = '$new_type_of_cooking' WHERE user_tel = '$_POST[tel]';"; $result = mysqli_query($link, $sql); if ($result) {//Modify the information successfully $json_arr = array('success' => 1); } else {//Failed to modify information $json_arr = array('success' => 0); } $login_json = json_encode($json_arr); echo $login_json; }1234567891011121314151617181920212223242526272829303132
5. Change password
//alter_password.php <?php header("Content-type:text/html;charset=utf-8"); header("Access-Control-Allow-Origin: *"); session_start(); if (isset($_POST["submit"]) && $_POST["submit"] == "修改密码") { //include "connect_mysql.php"; require_once('connect_mysql.php'); $tel = $_POST['tel']; $old_password = $_POST['old_password']; $sql = "SELECT password, user_tel FROM user WHERE password = '$old_password' AND user_tel = '$tel';"; $result = mysqli_query($link, $sql); $row = mysqli_num_rows($result); if ($row == 1) { $new_password = $_POST['new_password']; $new_password_conf = $_POST['new_password_conf']; if ($new_password == $new_password_conf) { $sql = "UPDATE user SET password = '$new_password' WHERE user_tel = '$tel';"; $result = mysqli_query($link, $sql); if ($result) {//Password modified successfully $arr = array('success' => 1); } } } else {// Password modification failed $arr['success'] = 0; } $json_arr = json_encode($arr, TRUE); echo $json_arr; }