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
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
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
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
Share on other sites

You aren't checking if the form is submitted, which is why you are getting undefined variables for username and pwd. You need to first submit the form, then (check if it was submitted) it will work.

 

Yea its working now. Thanks :)

Link to comment
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.

Edited by Love2c0de
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.