php 连接数据库 和 form表单简单注册
原创
©著作权归作者所有:来自51CTO博客作者周成乐的原创作品,请联系作者获取转载授权,否则将追究法律责任
数据库连接
<?php
header("Content-Type: text/html;charset=utf-8");
$conn=mysqli_connect("localhost:3306","root","root","mydb") or die("连接失败");
?>
form表单
<?php
include("conn.php");
if (mysqli_connect_errno($conn))
{
echo "连接 MySQL 失败: " . mysqli_connect_error();
}
if(!empty($_POST['sub'])){
$username=$_POST['username'];
$password=$_POST['password'];
echo $sql="insert into weblog(username,password) values('$username','$password')";
mysqli_query($conn,"$sql");
echo "注册成功";
}
?>
<form action="add.php" method="POST" >
账号:<input type="text" name="username"><br>
密码:<input type="password" name="password"><br>
<input type="submit" name="sub" value="注册">
</form>