I’m trying to create an admin login using PHPMyAdmin, and I can’t seem to query to the database. I wanted to know if this would be an issue with my code, or if this is an issue with how I have set up my database (missing something to connect to database).
So I’ve been catching mistakes (such as PHP changing mysql to mysqli), but I cannot connect to the database properly. Here are some backend approaches I’ve done to set up my database:
- Database is downloaded
- PHPMyAdmin is accessible through localhost
- I’ve created a database on PHPMyAdmin called "pcc_website_login" (with a username and password to login)
- MySQL is currently active
- The errors are from mysqli_error/query/connect, and all state that I cannot connect to the database correctly.
Below is the code for process.php; I also have a login.php/style.css, but those don’t really pertain to the database.
<?php
// get values passed from form in login.php file
$username = $_POST['user'];
$password = $_POST['pass'];
$username = stripcslashes($username);
$password = stripcslashes($password);
$db = mysqli_connect("localhost","root","","pcc_website_login");
$result = mysqli_query($db, "select * from login where username = '$username' and password = '$password'") or die("Failed to query database " .mysqli_error());
$row = mysqli_fetch_array($result);
if ($row['username']== $username && $row['password'] == $password){
echo "Login success!!! Welcome " .$row['username'];
} else {
echo "Faild to login!";
}
?>
3
Answers
You can test connect to your database. See example.
Try with this to figure out your problem
Hey looks like your just starting out with PHP and MySQL… its a crazy adventure, good job trying on something new. There few nail biting things wrong, but their mistakes I’ve made myself and its a good way to learn so don’t let that stop you.
First for your connection problem, unless you removed it, you’re missing a password.
The same user name and password that you use with phpMyAdmin will work… because after all phpMyAdmin is ran by the same kind of php you are using.
Second
Don’t use
stripcslashes
, it will not protect you. usemysqli_real_escape_string
instead w3chools.com has a good exampleThird
Absolutely NEVER store raw passwords in a database for any reason… ever.
You could hash of the password with the user name and another secret through a cryptographic function and, then store that.
Better still is to use OAUTH2 "Sign in with google" or "Sign in with Facebook". To use that you will need a valid domain and proper SSL certificate, running it from your local computer won’t work either unless and its not worth the hassle as cheap as hosting is
Finally
Don’t be afraid to use somebody else’s PHP code in your project. Compare being a program developer to say a housing developer. In that case you’d start with similar floor plans that the client was looking for and just tweak them to fit. Usig a Framwork like Laravel or Symfony, or a CMS like Joomla or WordPress is far easier.
In any case have fun and keep playing, have fun with the discovery, make things that solve silly but useful problems for you or friends, and challenge yourself to write as little code as possible to make that happen.