问题
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