My website doesn't “recognize” my database on localhost

六月ゝ 毕业季﹏ 提交于 2020-02-16 13:25:47

问题


I'm creating a website on localhost using XAMPP and phpMyAdmin to create a database.
When I try to use my function to subscribe with a username and password, it seems that the db doesn't work: my site displays that it accepted the input, but in the user table on phpMyAdmin, I can't find the new user just added.
I don't know if the problem is the query or the connection to db itself.
Is there away to debug it?

This php function that is included on all pages that use the db:

<?php
    function db_connection() {
        $db = "mysql:dbname=movie;host=localhost";
        try {
            return new PDO($db, "root", "");
        } catch(PDOException $e) {
            print("Fail to access to db");
        }
    }

I have this php file for the login:

<?php include ("database.php");?>
<?php
        function new_user($name, $password)
        {
            $psw = md5($password);
            $db = db_connection();
            $qname=$db->quote($name);
            $qpassword=$db->quote($psw);
            $db -> query(" INSERT INTO Users (id, name, password) VALUES (null, $qname, $qpassword)");
        }

        function check_existent_user($name)
        {
           $db = db_connection();
           $qname=$db->quote($name);
           $rows= $db -> query("SELECT  name  FROM Users WHERE name = $qname");
           foreach ($rows as $row) {
                return $name == $row["name"];
           }
        }

        function check_password($name, $password)
        {
            $psw = md5($password);
            $db = db_connection();
            $qname = $db->quote($name);
            $rows = $db->query("SELECT password FROM Users WHERE name = $qname");
            if($rows)
            {
                foreach ($rows as $row) {
                    $correct_pass = $row["password"];
                    return $psw === $correct_pass;
                }
            } else {
                return FALSE;
            }
        }

来源:https://stackoverflow.com/questions/60001598/my-website-doesnt-recognize-my-database-on-localhost

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!