To submit data to the server-side database, form form is used. This is the most common and simplest way to submit data. After filling in the form, the post is submitted to the background .php file, and after processing, it returns to the specified page. Finally, The page refreshed again, displaying the expected page. The following article will give you a detailed explanation of submitting PHP forms to the database through examples.

Generally when friends visit some websites and want to use the website or see more content on the website, the website will ask the user to register as a new user, and the website will The registration information of new users is stored in the database and retrieved when needed.
In this way, the website will first create its own database and corresponding tables. Here we use php to create a simple database and table, and use phpMyAdmin to create MySql database and table. For example, create a test database. The sample code is as follows:
<?php
// 創(chuàng)建連接
$conn = new mysqli("localhost", "uesename", "password");
// 檢測(cè)連接
if ($conn->connect_error)
{
die("連接失敗: " . $conn->connect_error);}
// 創(chuàng)建數(shù)據(jù)庫(kù)
$sql = "CREATE DATABASE test";
if ($conn->query($sql) === TRUE)
{
echo "數(shù)據(jù)庫(kù)創(chuàng)建成功";
} 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: Username, 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 statements to create database tables. The code is shown as follows:
<?php
// 創(chuàng)建連接
$conn = new mysqli("localhost", "uesename", "password","test");
// 檢測(cè)連接
if ($conn->connect_error)
{
die("連接失敗: " . $conn->connect_error);
}
// 使用 sql 創(chuàng)建數(shù)據(jù)表
$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 "創(chuàng)建數(shù)據(jù)表錯(cuò)誤: " . $conn->error;
}
$conn->close();
?>
We have created the database and tables above, and now we will create a simple front-end page for form registration. 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:
<!DOCTYPE html>
<html>
<head>
<title>用戶注冊(cè)頁(yè)面</title>
<meta charset="UTF-8"/>
<style type="text/css">
*{margin:0px;padding:0px;}
ul{
width:400px;
list-style:none;
margin:50px auto;
}
li{
padding:12px;
position:relative;
}
label{
width:80px;
display:inline-block;
float:left;
line-height:30px;
}
input[type='text'],input[type='password']{
height:30px;
}
img{
margin-left:10px;
}
input[type="submit"]{
margin-left:80px;
padding:5px 10px;
}
</style>
</head>
<body>
<form action="zhuce.php" method="post">
<ul>
<li>
<label>用戶名:</label>
<input type="text" name="username" placeholder="請(qǐng)輸入注冊(cè)賬號(hào)"/>
</li>
<li>
<label>密 碼:</label>
<input type="password" name="password" placeholder="請(qǐng)輸入密碼" />
</li>
<li>
<label>確認(rèn)密碼:</label>
<input type="password" name="confirm" placeholder="請(qǐng)?jiān)俅屋斎朊艽a" />
</li>
<li>
<label>郵 箱:</label>
<input type="text" name="email" placeholder="請(qǐng)輸入郵箱"/>
</li>
<li>
<input type="submit" value="注冊(cè)" />
</li>
</ul>
</form>
</body>
</html>
Next, you need to use PHP code to submit the information submitted by the new user to the database, and use the POST method to transfer and obtain the value.
First, 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 put, the data submitted by the form is stored in the variable, and then the password and verification code are judged. After both are correct, the user information is stored The database extracts and prints out all the data in the table where user information is stored in the database.
To put it bluntly, the second half of the sentence is about data storage and extraction. The specific code is as follows:
<?php
session_start();
header("Content-type:text/html;charset=utf-8");
$link = mysqli_connect('localhost','root','root','test');
if (!$link) {
die("連接失敗:".mysqli_connect_error());
}
$username = $_POST['username'];
$password = $_POST['password'];
$confirm = $_POST['confirm'];
$email = $_POST['email'];
if($username == "" || $password == "" || $confirm == "" || $email == "")
{
echo "<script>alert('信息不能為空!重新填寫');window.location.href='zhuce.html'</script>";
} elseif ((strlen($username) < 3)||(!preg_match('/^\w+$/i', $username))) {
echo "<script>alert('用戶名至少3位且不含非法字符!重新填寫');window.location.href='zhuce'</script>";
//判斷用戶名長(zhǎng)度
}elseif(strlen($password) < 5){
echo "<script>alert('密碼至少5位!重新填寫');window.location.href='zhuce.html'</script>";
//判斷密碼長(zhǎng)度
}elseif($password != $confirm) {
echo "<script>alert('兩次密碼不相同!重新填寫');window.location.href='zhuce.html'</script>";
//檢測(cè)兩次輸入密碼是否相同
} elseif (!preg_match('/^[\w\.]+@\w+\.\w+$/i', $email)) {
echo "<script>alert('郵箱不合法!重新填寫');window.location.href='zhuce.html'</script>";
//判斷郵箱格式是否合法
} elseif(mysqli_fetch_array(mysqli_query($link,"select * from login where username = '$username'"))){
echo "<script>alert('用戶名已存在');window.location.href='zhuce.html'</script>";
} else{
$sql= "insert into login(username, password, confirm, email)values('$username','$password','$confirm','$email')";
//插入數(shù)據(jù)庫(kù)
if(!(mysqli_query($link,$sql))){
echo "<script>alert('數(shù)據(jù)插入失敗');window.location.href='zhuce.html'</script>";
}else{
echo "<script>alert('注冊(cè)成功!)</script>";
}
}
?>
Friends can do various operations and attempts by themselves. After becoming proficient, they will have a certain in-depth understanding of form operations and database operations, laying a good foundation for future development. Foundation.
The above is the detailed content of How to submit php form to database? (detailed explanation). For more information, please follow other related articles on the PHP Chinese website!