html 代码

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>login</title>
</head>
<body>
<form action="result.php" method="post">
qq号码: <input type="text" name="username" ><br>
qq密码: <input type="text" name="password" ><br>
<input type="submit" value="登录">
</form>
</body>
</html>

这里写图片描述
点击 「登录」 按钮后,将数据 postresult.php

result.php

<?php
header("Content-type:text/html;charset=utf-8"); 
	// 数据类型是utf-8 不加入可能会乱码
	$serverName = "localhost";	// 服务器ip
    $username = "root";			// mysql用户名称
    $password = "123456";		// 密码
    $dataBase = "yunhu";		// 数据库名称

    $conn = new mysqli($serverName, $username, $password,$dataBase);

    if($conn->connect_error) 
    {
    	//连接失败
        echo "对不起,服务器未响应";
        echo "<br>";
    }
    // 数据库数据编码格式
    $sql = "SET NAMES UTF8";
    if($conn->query($sql) === true)
     {
        //echo "set success<br>";
    }
    else 
    {
       echo "set fail<br>";
    }

    $username = $_POST['username'];
    $password = $_POST['password'];
    $result = array();
    if($username == "") 
    {
        $result["result"] = "false";
    }
    else 
    {
        $add = $conn->prepare("INSERT INTO userInformation(username, password)  VALUES(?,?)");
        $add->bind_param("ss", $username,$password);

        $stmt = $conn->prepare("SELECT * FROM userInformation WHERE username=?");
        $stmt->bind_param("s", $username);
        $stmt->execute();
        $stmt->store_result();
        $stmt->bind_result($id);
        
        if($stmt->fetch()) 
        {
            $result["result"] = "false";
        }
        else
        {
            $add->execute();
            $result["result"] = "true";
        }
    }
?>
Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐