我如何使用php在mysql上添加数据?

我是编程新手,正在学习如何将表单连接到服务器。 我正在上一课,但没有收到任何输出,因此我很难找到问题所在。 该练习还包括了解cookie的工作原理

this is the form:
       <form action="logreg.php" method="post">

        Username:<input type="text" name="username"><br>
        Password:<input type="password" name="password"><br>

        <input type="submit" name="login" value="Login">
        <input type="submit" name="register" value="Register">
</form>

这是PHP文件:

<?php 


$cookie_name="loggedin";
$servername="localhost";
$username="root";
$password="";
$database="login";

$conn= mysqli_connect($servername,$username,$password,$database);

if(!$conn){
    die("Not connected to database".mysqli_connect_error());
}
if(isset($_POST['login']))
{
    $user=$_POST['username'];
    $pass=$_POST['password'];
    $phash= sha1(sha1($pass."salt")."salt");

    $sql= "SELECT * FROM records WHERE username='$user' AND password='$phash';";


    $result=mysqli_query($conn, $sql);
    $count=mysqli_num_rows($result);


    if($count==1)
    {
$cookie_value=$user;
setcookie($cookie_name, $cookie_value, time()+ (180),"/");

header("Location:personal.php");
    }
    else
    {
        echo "Username or Password is incorrect";
    }
}
    else if(isset($_POST['register']))
    {
        $user=$_POST['username'];
        $pass=$_POST['password'];

        $phash= sha1(sha1($pass."salt")."salt");

$sql="INSERT INTO users (id, username, password)
 VALUES ('','$user','$phash');";
    $result=mysqli_query($conn,$sql);
    }
    mysqli_close($conn);
?>

评论