Hi guys I'm trying to write some code for a login, I've got it all but whenever I try login it says Could not select database. I can't figure it out, I've got hosting with One.com and when I go into the PHP & MySQL settings it says the database name is "c343_co_uk", which is what I've used in the code below:
UPDATE: It's detecting the database but whenever I try and log in with the exact same username and password that's on MySQL it says invalid login
Here is my Connection.php
<?php
$username = "c343_co_uk";
$password = "abc";
$hostname = "c343.co.uk.mysql";
//connection to the database
$connection = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
$selected = mysql_select_db("c343_co_uk",$dbhandle)
or die("Could not select Database");
?>
Loginform.php
<!DOCTYPE html>
<html>
Home
</font>
<head>
<style>
body {
font-size: 14px;
}
</style>
<link rel="stylesheet" type="text/css" href="website.css" />
</head>
<body>
<div id="loginform" style="font-family: 'ClearSans-Thin'; color: Black">
Please enter your login details<br /><br />
Username:<br />
<form method="post" action="loginsubmit.php">
<input type="text" name="username" />
<br />
Password:<br />
<input type="password" name="password" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>
</center>
</div>
</body>
</html>
Loginsubmit.php
<?php
session_start();
?>
<font face="ClearSans-Thin">
<font color="lightgray">
<?php
include 'connection.php';
include 'loginform.php';
?>
<center>
<?php
if (isset($_POST['submit']))
{
$user = $_POST['username'];
$pass = $_POST['password'];
//Counts up how many matches there are in the database
$query = "SELECT COUNT(*) AS cnt FROM users WHERE Username='" . mysqli_real_escape_string($connection, $user) . "' && Password='" . mysqli_real_escape_string($connection, $pass). "'";
$result = mysqli_query($connection, $query);
$row = mysqli_fetch_assoc($result);
$queryadmin = "SELECT COUNT(*) AS cnt FROM admin WHERE Username='" . mysqli_real_escape_string($connection, $user) . "' && Password='" . mysqli_real_escape_string($connection, $pass). "'";
$resultadmin = mysqli_query($connection, $queryadmin);
$rowadmin = mysqli_fetch_assoc($resultadmin);
//If count is more than 0, log user in.
if ($row["cnt"] > 0)
{
$_SESSION["userlogged"] = $user;
echo "Logged in - Press the home button to return to the homepage";
}
//count for user table is 0, if there are more than 0 matches in the admin database, start admin session
else if ($rowadmin["cnt"] > 0 )
{
$_SESSION["adminlogged"] = $user;
echo "Logged in - Press the home button to return to the homepage";
}
else
{
echo 'Not a valid login';
}
}
?>
</center>
try these:
$dbname = "c343_co_uk"
$username = "c343_co_uk";
$password = "abc";
$hostname = "c343.co.uk.mysql";
and replace
$selected = mysql_select_db("c343_co_uk",$connection)
or die("Could not select Database");
to this:
$selected = mysql_select_db($dbname,$connect)
or die("Could not select Database");
i hope that work! have a nice day!
try to replace
$selected = mysql_select_db("c343_co_uk",$dbhandle)
or die("Could not select Database");
with
$selected = mysql_select_db("c343_co_uk",$connection)
or die("Could not select Database");
you can try by this way
<?php
define("DB_HOST", "c343.co.uk.mysql");
define("DB_USER", "c343_co_uk");
define("DB_PASSWORD", "abc");
define("DB_DATABASE", "c343_co_uk");
$conn = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD,DB_DATABASE );
?>
Use this for connection :-
$username = "c343_co_uk";
$password = "abc";
$hostname = "c343.co.uk.mysql";
$connection = mysql_connect($hostname, $username, $password);
#mysql_select_db("c343_co_uk",$connection);
you haven't do the host with mysql, just do c343.co.uk:
<?php
$username = "c343_co_uk";
$password = "abc";
$hostname = "c343.co.uk";
//connection to the database
$connection = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
$selected = mysql_select_db("c343_co_uk",$dbhandle)
or die("Could not select Database");
?>
I hope this work! other i'll take a look again
Related
this is my log in page. I can log in when I put the relevant username and password and can't log in if they do not match. but the thing is I still have the privilege of logging in when I do not enter any username, password and click on the log in button.
this is the index page
<html>
<head>
<title>Login</title>
<link rel="stylesheet" type="text/css" href="login.css">
</head>
<body>
<div class="login-page">
<form method="post" class="form" action="login.php">
<input type="text" id="user" name="user" placeholder="username"/>
<input type="password" id="pass" name="pass" placeholder="password"/>
<button type="submit" name="submit" id="btn">login</button>
</form>
</div>
</body>
</html>
this is login.php
<?php
//get values passe from form in login.php file
$username = $_POST['user'];
$password = $_POST['pass'];
//to prevent sql injection
$username = stripslashes($username);
$password = stripslashes($password);
$username = mysql_real_escape_string($username);
$password = mysql_real_escape_string($password);
//connect to the server and select database
mysql_connect("localhost","root","");
mysql_select_db('laboursalary');
//query the database for user
$result = mysql_query("SELECT * FROM login WHERE username= '$username' and password='$password'")
or die("Failed to query database" .mysql_error());
$row = mysql_fetch_array($result);
if ($row['username']==$username && $row['password']==$password) {
header("Location: ../projectdetails/index.php");
} elseif ($row['username']=="" && $row['password']==""){
echo "Failed to login";
} else {
echo "Failed to login";
}
?>
<?php
if(isset($_POST['submit']))
{
//get values passe from form in login.php file
$username = mysql_real_escape_string($_POST['user']);
$password = mysql_real_escape_string($_POST['pass']);
//connect to the server and select database
mysql_connect("localhost","root","");
mysql_select_db('laboursalary');
//query the database for user
$result = mysql_query("SELECT * FROM login WHERE username= '$username' and password='$password'");
$row = mysql_num_rows($result);
if($row == 1)
{
header("Location: ../projectdetails/index.php");
} else {
echo "Failed to login";
}
}
?>
Which means you are having a row with username and password empty. Delete those rows in the table and update the below code as well.
if ($username != "" && $password !=""){
//query the database for user
$result = mysql_query("SELECT * FROM login WHERE username= '$username' and password='$password'")
or die("Failed to query database" .mysql_error());
$row = mysql_fetch_array($result);
if ($row['username']==$username && $row['password']==$password) {
header("Location: ../projectdetails/index.php");
} else {
echo "Failed to login";
}
} else
echo "Failed to login";
}
Note: Also change your mysql connection to mysqli or PDO. Because it is deprecated in the latest PHP versions.
I am fiddling around with mysql, PHP, and phpMyAdmin and I am making a short little test login and register system. Only problem is for some reason, the register button takes me to the login page, which it's supposed to, but localhost crashes for some reason. Any help?
Edit: You can test it out too if you would like. My Site: http://localhost/
index.php
<head>
<meta charset="utf-8">
<title>Test Site</title>
<link rel="stylesheet" type="text/css" href="css/styles.css">
</head>
<body>
<form action="login/logreg.php" method="post" accept-charset="utf-8">
<label>Username: </label><input type="text" name="username" value="" placeholder="Username">
<br><br>
<label>Password: </label><input type="password" name="password" value="" placeholder="Password">
<br><br>
<input type="submit" name="login" value="Login">
<input type="submit" name="register" value="Register">
</form>
</body>
logreg.php
<?php
$cookie_name = "loggedin";
$servername = "localhost";
$username = "root";
$password = "H2124130E63C8D14871";
$database = "webserver";
$conn = mysqli_connect($servername, $username, $password $database);
if (!$conn) {
die("Database Connection Failed: ".mysqli_connect_error());
}
if (isset($_POST['login']))
{
$user = $_POST['username'];
$pass = $_POST['password'];
$phash = sha1(sha1($pass."salt")."salt");
$sql = "SELECT * FROM users 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);
}
?>
personal.php
<?php
$cookie_name = "loggedin";
if (isset($_COOKIE[$cookie_name]))
{
$cookie_value = $_COOKIE[$cookie_name];
echo "Welcome To Your Personal Area $cookie_value!";
echo 'Logout';
}
?>
logout.php
<?php
setcookie("loggedin", "val", time() - (120), "/");
header("Location: index.php");
?>
You missed a comma here:
$conn = mysqli_connect($servername, $username, $password $database);
I am working on login system. But, i cannot log in. I have set my database table.
login.php
<?php
session_start();
if(isset($_POST['login'])) {
include_once("db.php");
$username = strip_tags($_POST['username']);
$password = strip_tags($_POST['password']);
$username = stripslashes($username);
$password = stripslashes($password);
$username = mysqli_real_escape_string($db, $username);
$password = mysqli_real_escape_string($db, $password);
$password = md5($password);
$sql = "SELECT * FROM users WHERE username='$username' LIMIT 1";
$query = mysqli_query($db, $sql);
$row = mysqli_fetch_array($query);
$id = $row['id'];
$db_password = $row['password'];
if($password == $db_password) {
$_SESSION['username'] = $username;
$_SESSION['id'] = $id;
header("Location: av_pocetna.html");
} else {
echo "You didn't enter the correct details!";
}
}
?>
<html>
<head>
<title>Login</title>
</head>
<body>
<h1 style="font-family: Tahoma;">Login</h1>
<form action="login.php" method="post" enctype="multipart/form-data">
<input placeholder="Username" name="username" type="text" autofocus>
<input placeholder="Password" name="password" type="password">
<input name="login" type="submit" value="Login">
</form>
</body>
</html>
and this is db.php
<? php
$db=mysqli_connect('192.168.1.113:8080','root','hidden','av');
?>
connent of users table
id
username
password
Edit Edit
Copy Copy
Delete Delete
1
a
0cc175b9c0f1b6a831c399e269772661
Your form code look right. Just change like below your login.php code:-
<?php
session_start();
error_reporting(E_ALL);
ini_set('display_errors',1);
$conn = mysqli_connect('host-name','user-name','password','database-name');
if($conn){
if(isset($_POST['username']) && isset($_POST['password'])) {
$username = $_POST['username'];
$password = $_POST['password'];
$username = mysqli_real_escape_string($db, $username);
$password = mysqli_real_escape_string($db, $password);
$password = md5($password);
$sql = "SELECT * FROM users WHERE username='$username' LIMIT 1";
$query = mysqli_query($db, $sql);
if($query){
$row = mysqli_fetch_array($query);
$id = $row['id'];
$db_password = $row['password'];
if($password == $db_password) {
$_SESSION['username'] = $username;
$_SESSION['id'] = $id;
header("Location: av_pocetna.html");
} else {
echo "You didn't enter the correct details!";
}
}else{
echo "query not executed because".mysqli_error($conn);
}
}
}else{
echo "db connection error".mysqli_connect_error();
}
?>
Note:- i have added connection code here only,so change the credentials there. And use this same code to check working or not?
Also if you are working on your local then change ip address to localhost and check. If it will work then it will work with include("db.php") too.I mean to say try with $conn = mysqli_connect('localhost','root','aleksandar','av');
Here is the working login.php
<?php
session_start();
error_reporting(E_ALL);
ini_set('display_errors',1);
$conn = mysqli_connect('localhost','root','aleksandar','av');
$db = new mysqli('localhost','root','aleksandar','av');
if($conn){
if(isset($_POST['username']) && isset($_POST['password'])) {
$username = $_POST['username'];
$password = $_POST['password'];
$username = mysqli_real_escape_string($db, $username);
$password = mysqli_real_escape_string($db, $password);
$password = md5($password);
$sql = "SELECT * FROM users WHERE username='$username' LIMIT 1";
$query = mysqli_query($db, $sql);
if($query){
$row = mysqli_fetch_array($query);
$id = $row['id'];
$db_password = $row['password'];
if($password == $db_password) {
$_SESSION['username'] = $username;
$_SESSION['id'] = $id;
header("Location: av_pocetna.html");
} else {
echo "You didn't enter the correct details!";
}
}else{
echo "query not executed because".mysqli_error($conn);
}
}
}else{
echo "db connection error".mysqli_connect_error();
}
?>
<html>
<head>
<title>Login</title>
</head>
<body>
<h1 style="font-family: Tahoma;">Login</h1>
<form action="login.php" method="post" enctype="multipart/form-data">
<input placeholder="Username" name="username" type="text" autofocus>
<input placeholder="Password" name="password" type="password">
<input name="login" type="submit" value="Login">
</form>
</body>
</html>
Oh Okay.
Lets try debugging one step at a time then.
In your db.php file, use this:
// Connecting to mysql database
$db = new mysqli('192.168.1.113:8080','root','hidden','av');
// Check for database connection error
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
If you get any error, please dump it here for debugging.
Updated.
// Connecting to mysql database
$db = new mysqli('localhost','root','hidden','av');
// Check for database connection error
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
I think I am successfully connecting to my database by:
<?php
$user = 'root';
$pass = '9KSroMDjEqNmEYY4';
$db = 'chatservice';
$host = '127.0.0.1';
$conn = new mysqli($host, $user, $pass, $db, 3306) or die("Unable to connect");
if ($conn->connect_error){
die("Connection failed: " . $conn->connect_error);
}
?>
My question is how I would use the registration code to successfully add a user to the database. When entering the form I press register I do not get any error messages stating that the registration didn't succeed. It seems that the php code is not being reached after the initial connection. I am new to php and mySQL so any tips on formatting would be nice too!
<?php
require('connect.php');
if(isset($_POST['user']) && isset($_POST['password'])){
$user = $_POST['user'];
$id = $_POST['IDNUM'];
$password = $_POST['password'];
$query = "INSERT INTO 'users' (user ,IDNUM ,password) VALUES('$user', '$id', '$password')";
$result = mysqli_query($query);
if($result){
$msg = "Registered Sussecfully";
echo $msg;
}
else
$msg = "Error Registering";
echo $msg;
}
?>
<div class="register-form">
<title>Chat Page Start</title>
<form action="" methods="POST">
<p>
<label>Username: </label>
<input id="user" type="text" name="user" placeholder="user" />
</p>
<p>
<label>ID: </label>
<input id="IDNUM" type="text" name="IDNUM" placeholder="ID number" />
</p>
<p>
<label>Password: </label>
<input id="password" type="password" name="password" placeholder="password" />
</p>
<a class="btn" href="login.php">Login</a>
<input class="btn register" type="submit" value="Register" />
</form>
</div>
Another thing is how would I check the status of my database connection and where I should be checking this status?
your database connection is mysqli_connect and you execute the query in mysql_query is not proper.
<?php
require('connect.php');
if(isset($_POST['user']) && isset($_POST['password'])){
$user = $_POST['user'];
$id = $_POST['IDNUM'];
$password = $_POST['password'];
$query = "INSERT INTO 'users' (user ,IDNUM ,password) VALUES('$user', ' $id ', '$password')";
$result = mysqli_query($query,$conn);
if($result){
$msg = "Registered Sussecfully";
}
else
$msg = "Error Registering";
}
?>
You are connecting database using mysqli:
$conn = new mysqli('localhost', $user, $pass, $db, 3306) or die("Unable to connect");
And executing query using mysql:
$query = "INSERT INTO 'users' (user ,IDNUM ,password) VALUES('$user', '$IDNUM', '$password')";
$result = mysql_query($query);
I know how noobish this question is, but I'm really new to PHP and MySQL and I got this code not working...
I'd appreciate if anyone tell me what's the problem:
<?php
$hostname = "localhost";
$username = "root";
$password = "";
$database = "lab";
$con = mysqli_connect($hostname, $username, $password, $database);
$error = "";
$success = "";
$pass = false;
if (isset($_POST["submit"])) {
$query = mysqli_query($con, "SELECT * FROM accounts WHERE username='" . $_POST["username"] . "' password=''");
if (!$query || mysqli_num_rows($query) <= 0) {
$error = "Wrong username or password!";
$success = "";
} else {
while ($row = mysqli_fetch_assoc($query)) {
$pass = password_verify($_POST["password"], $row["password"]);
}
if ($pass) {
$error = "";
$success = "Welcome, " . $_POST['username'] . "!";
}
}
}
?>
<html>
<head>
<title>Lab</title>
<style>
#error {
color: red;
}
#success {
color: green;
}
</style>
</head>
<body>
<fieldset>
<legend>Log into your account</legend>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
<span id="error"><?php echo $error; ?></span>
<br/>
<span id="error"><?php echo $success; ?></span>
<br/>
Username:
<input type="text" name="username" placeholder="Username"/>
<br/>
<br/>
Password:
<input type="password" name="password" placeholder="Password"/>
<br/>
<br/>
<input type="submit" name="submit" value="Login"/>
</form>
</fieldset>
</body>
</html>
But when I write a right username and a right password, it always shows me the error.
"SELECT * FROM accounts WHERE username='" . $_POST["username"] . "' password=''"
This is an invalid query. You shouldn't add password='' and even if you had to, you forgot an and in your query, so it wouldn't work.
Change the query to this:
"SELECT * FROM accounts WHERE username='" . $_POST["username"] . "'"
Please notice that your code is vulnerable to SQL injection. You should not use this code on a public website, until you've fixed your securite hole.
First of all I noticed this.. is this really you intention... I think this means it will query around only those without password setted.
SELECT * FROM accounts WHERE username='" . $_POST["username"] . "' password=''
second what do you mean by (im not sure what you are doing at these point..
!$query
third, you don have password on your database?
$password = "";
I got this from w3schools and I modified it a bit to suit you needs
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "lab";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = mysqli_query($con, "SELECT * FROM accounts WHERE username='" .
$_POST["username"] . "'and password=''");
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "Welcome"
}
} else {
echo "Failed to login";
}
$conn->close();
?>