php database to obtain the form content:
Example:
<?php // 创建连接 $conn = new mysqli("localhost", "uesename", "password"); // 检测连接 if ($conn->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:
<?php // 创建连接 $conn = new mysqli("localhost", "uesename", "password","test"); // 检测连接 if ($conn->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:
<!DOCTYPE html> <html> <head> <title>用户注册页面</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="请输入注册账号"/> </li> <li> <label>密 码:</label> <input type="password" name="password" placeholder="请输入密码" /> </li> <li> <label>确认密码:</label> <input type="password" name="confirm" placeholder="请再次输入密码" /> </li> <li> <label>邮 箱:</label> <input type="text" name="email" placeholder="请输入邮箱"/> </li> <li> <input type="submit" value="注册" /> </li> </ul> </form> </body> </html>
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 put Store 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:
<?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>"; //判断用户名长度 }elseif(strlen($password) < 5){ echo "<script>alert('密码至少5位!重新填写');window.location.href='zhuce.html'</script>"; //判断密码长度 }elseif($password != $confirm) { echo "<script>alert('两次密码不相同!重新填写');window.location.href='zhuce.html'</script>"; //检测两次输入密码是否相同 } 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')"; //插入数据库 if(!(mysqli_query($link,$sql))){ echo "<script>alert('数据插入失败');window.location.href='zhuce.html'</script>"; }else{ echo "<script>alert('注册成功!)</script>"; } } ?>
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!

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Notepad++7.3.1
Easy-to-use and free code editor

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Mac version
God-level code editing software (SublimeText3)
