Alienn Posted January 1, 2013 Share Posted January 1, 2013 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> Quote Link to comment Share on other sites More sharing options...
Love2c0de Posted January 1, 2013 Share Posted January 1, 2013 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 Quote Link to comment Share on other sites More sharing options...
Alienn Posted January 2, 2013 Author Share Posted January 2, 2013 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> Quote Link to comment Share on other sites More sharing options...
Alienn Posted January 2, 2013 Author Share Posted January 2, 2013 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 Quote Link to comment Share on other sites More sharing options...
MDCode Posted January 2, 2013 Share Posted January 2, 2013 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. Quote Link to comment Share on other sites More sharing options...
Alienn Posted January 2, 2013 Author Share Posted January 2, 2013 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 Quote Link to comment Share on other sites More sharing options...
Love2c0de Posted January 2, 2013 Share Posted January 2, 2013 (edited) 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 January 2, 2013 by Love2c0de Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.