HTML表单使用PHP实现MySQL数据库插入

1.employeeInf.html页面提供用户信息的录入,如下图所示:

HTML表单使用PHP实现MySQL数据库插入

2.employeeInf.html源代码如下图所示:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Insert the information of employee!</title>
</head>
<body>
    <form action="shishi2.php" method="post">
        工号:<input type="text" name="gonghao" />
        姓名:<input type="text" name="name"/>
        地址:<input type="text" name="address"/>
        <input type="submit" />
    </form>
</body>
</html>

3.shishi2.php源代码如下所示;主要负责向MySQL数据库中插入数据;

<?php
//第一种链接mysql数据库的代码方式;
$con = mysqli_connect('localhost','root','rootroot');
if(!$con)
{
    echo "link failly";
}
else
{
   // echo "link successfully";
   // echo $_POST[gonghao];

    mysqli_select_db($con,"finance");
    $sql="insert into yuangonginf(gonghao,name,address) values ('$_POST[gonghao]','$_POST[name]', '$_POST[address]')";

    //if(!mysqli_query($con,"insert into yuangonginf(gonghao,name,address) values ('1002','lisi', 'Wuhan,BayiRoad')"))
    if(!mysqli_query($con, $sql))
    {
        die('Error:'.mysqli_error($con));
    }
    else
    {
        echo "One record added";
        mysqli_close($con);
    }

}