Jump to content

Login System


Alienn

Recommended Posts

Hello,

 

I am trying to create a login system, but my code isn't doing anything. (Not even showing a error message)

I guess my code can't connect to my database and its something with my mysqli statements.

Hope someone can help a php beginner.

 

My code:

 

 

 

<?php
$host="localhost";
$username="";
$password="";
$db_name="portfolio";
$tbl_name="users";


mysqli_connect("$host", "$username", "$password") or die ("Can't connect");
mysql_select_db("$db_name") or die ("Can't select database");

$myusername=$_POST['username'];
$mypassword=$_POST['pwd'];

$sql=("SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'");
$count = mysqli_num_rows($sql);



if($count==1){


   session_start();
   $_SESSION['username'] = $myusername;
   $_SESSION['pwd'] = $mypassword;
   header("location:Home.html");
}
else {
   echo "Wrong name or Password";
}
?>






<!doctype html>
<html>
   <head>
    <meta charset="utf-8" />
    <link rel="stylesheet" type="text/css" href="css.css"/>

    <title>Login</title>

   </head>



   <body>



    <div class ="wrapper">
	    <nav id="nav">
		    <ul>
			    <li> <a href="Home.html" >Home </a> </li>   
			    <li> <a href="OverMij.html" >Over mij </a> </li>   
			    <li> <a href="Overzicht.html" >Project overzicht </a> </li>
			    <li> <a href="Voegtoe.html">Project invoer</a> </li>
			    <li> <a href="Details.html" >Project details </a> </li>
			    <li> <a href="Contact.html" >Contact </a> </li>
		    </ul>
	    </nav>

	    <div class ="title"> Login </div>
	    <div id="add-content">  

		    <form method="post" action="login.php" id="form-add">
			    <p><label>Username:</label> <input type="text" id="Username"  name="username" autofocus/></p>
			    <p><label>Password:</label> <input type="password" id="Password" name="pwd"/> </p>
			    <p class="submit"><input type="submit" value="Login" name="submit" /></p>


		    </form>



	    </div>




    </div>
   </body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/272582-login-system/
Share on other sites

mysqli_connect() accepts 4 parameters: host, user, pass and database name. You only have 3.

 

I'd suggest using a prepared statement such as:

$conn = new mysqli("localhost","root","","portfolio") or die("Error: ".mysqli_error());

$stmt = $conn->prepare("SELECT * FROM users WHERE username=? AND password=?") or die("Error:".mysqli_error());
$stmt->bind_param("ss",$_POST['username'],$_POST['pwd']);
$stmt->execute();
$stmt->store_result();

$row = $stmt->num_rows;

if($row==1){


       session_start();
       $_SESSION['username'] = $_POST['username'];
       $_SESSION['pwd'] = $_POST['pwd'];
       header("location:Home.html");
}
else {
       echo "Wrong name or Password";
}

 

try this out and see what happens.

 

Kind regards,

 

L2c

Link to comment
https://forums.phpfreaks.com/topic/272582-login-system/#findComment-1402601
Share on other sites

Well i am not really learning object orientated at the moment. I changed my code, but it is still not working.

 

Code:

<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="portfolio"; // Database name
$tbl_name="users"; // Table name


mysqli_connect("$host", "$username", "", "$password") or die ("Can't connect");
mysql_select_db("$db_name") or die ("Can't select database");


$myusername=$_POST['username'];
$mypassword=$_POST['pwd'];


$results = mysqli_query("portfolio", "SELECT * FROM users WHERE username='$myusername' and password='$mypassword'" );
$count = mysqli_num_rows($results);



if($count==1){


   session_start();
   $_SESSION['username'] = $myusername;
   $_SESSION['pwd'] = $mypassword;
   header("location:Home.html");
}
else {
   echo "Wrong name or Password";
}




?>






<!doctype html>
<html>
   <head>
    <meta charset="utf-8" />
    <link rel="stylesheet" type="text/css" href="css.css"/>

    <title>Login</title>

   </head>



   <body>



    <div class ="wrapper">
	    <nav id="nav">
		    <ul>
			    <li> <a href="Home.html" >Home </a> </li>   
			    <li> <a href="OverMij.html" >Over mij </a> </li>   
			    <li> <a href="Overzicht.html" >Project overzicht </a> </li>
			    <li> <a href="Voegtoe.html">Project invoer</a> </li>
			    <li> <a href="Details.html" >Project details </a> </li>
			    <li> <a href="Contact.html" >Contact </a> </li>
		    </ul>
	    </nav>

	    <div class ="title"> Login </div>
	    <div id="add-content">  

		    <form method="post" action="login.php" id="form-add">
			    <p><label>Username:</label> <input type="text" id="Username"  name="username" autofocus/></p>
			    <p><label>Password:</label> <input type="password" id="Password" name="pwd"/> </p>
			    <p class="submit"><input type="submit" value="Login" name="submit" /></p>


		    </form>



	    </div>




    </div>
   </body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/272582-login-system/#findComment-1402721
Share on other sites

I fixed my problem it was because of this line:

mysql_select_db("$db_name") or die ("Can't select database");

 

I deleted this line and made one line like this:

$db = mysqli_connect($host, $username, $password, "portfolio") or die ("Can't connect");

 

Its connecting now and its working with my database.

Only getting this error now:

 

Notice: Undefined index: username in C:\xampp\htdocs\IMP-2\Portfolio\Login.php on line 13

 

Notice: Undefined index: pwd in C:\xampp\htdocs\IMP-2\Portfolio\Login.php on line 14

Wrong name or Password

Link to comment
https://forums.phpfreaks.com/topic/272582-login-system/#findComment-1402727
Share on other sites

Also, take a look here: http://php.net/manua...ysqli.query.php. When using the mysqli extension, I think you need to provide the mysqli_query() function a link to the connection, when coding procedural style.

 

You could do something like:

$conn = mysqli_connect($host, $username, "", $password) or die ("Can't connect");

$results = mysqli_query($conn,"portfolio", "SELECT * FROM users WHERE username='$myusername' and password='$mypassword'" );

 

Kind regards,

 

L2c.

Link to comment
https://forums.phpfreaks.com/topic/272582-login-system/#findComment-1402834
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.