php database to obtain the form content:
Example:
connect_error) { die("连接失败: " . $conn->connect_error);} // 创建数据库 $sql = "CREATE DATABASE test"; if ($conn->query($sql) === TRUE) { echo "数据库创建成功"; } else { echo "Error creating database: " . $conn->error; } $conn->close(); ?>
Then use the CREATE TABLE statement to create a MySQL table and set the following fields.
id : It is unique, of type int , and selects the primary key.
#uesrname: User name, type is varchar, length is 30.
password: Password, type is varchar, length is 30.
confirm: Confirm password, type is varchar, length is 30.
email: Email, type is varchar, length is 30.
Then use sql statement to create the database table. The code is shown as follows:
connect_error) { die("连接失败: " . $conn->connect_error); } // 使用 sql 创建数据表 $sql = "CREATE TABLE login ( id INT(10) UNSIGNED AUTO_INCREMENT PRIMARY KEY, username VARCHAR(30) NOT NULL, password VARCHAR(30) NOT NULL, confirm VARCHAR(30) NOT NULL, email VARCHAR(30) NOT NULL, )ENGINE=InnoDB DEFAULT CHARSET=utf8 "; if ($conn->query($sql) === TRUE) { echo "Table MyGuests created successfully"; } else { echo "创建数据表错误: " . $conn->error; } $conn->close(); ?>
We have created the database and table above, and now we will create a simple form registration On the front-end page, the form page here is very simple, with a few simple text boxes such as username, password, password confirmation, registration email, etc. The code is as follows:
用户注册页面
Next, you need to use PHP code to submit the information submitted by the new user to the database.Use the POST method to transfer and obtain the value.
First of all, you need to connect to the database and table created previously, because the user name, password and other information registered by the new user need to be saved in the corresponding fields in the table. Before storing the data in the database table, make some judgments and verifications on the submitted data. For example, user names and emails that do not meet the requirements need to be filtered and error prompts are needed. Also, if the user name is registered by other users, you need to be prompted that you will not be able to To use this username again, this is to first read the username that already exists in the database and then make a judgment.
Simply putStore all the data submitted in the form into variables, and then judge the password and verification code. After they are correct, store the user information in the database and store the table of user information in the database. All data extracted from the file are printed out. To put it bluntly, the second half of the sentence is about data storage and retrieval. The specific code is as follows:
alert('信息不能为空!重新填写');window.location.href='zhuce.html'"; } elseif ((strlen($username) < 3)||(!preg_match('/^\w+$/i', $username))) { echo ""; //判断用户名长度 }elseif(strlen($password) < 5){ echo ""; //判断密码长度 }elseif($password != $confirm) { echo ""; //检测两次输入密码是否相同 } elseif (!preg_match('/^[\w\.]+@\w+\.\w+$/i', $email)) { echo ""; //判断邮箱格式是否合法 } elseif(mysqli_fetch_array(mysqli_query($link,"select * from login where username = '$username'"))){ echo ""; } else{ $sql= "insert into login(username, password, confirm, email)values('$username','$password','$confirm','$email')"; //插入数据库 if(!(mysqli_query($link,$sql))){ echo ""; }else{ echo ""; } } ?>
Recommended:php server
The above is the detailed content of How to get form data from php database. For more information, please follow other related articles on the PHP Chinese website!