Can't get registration/login

Hi all

I’ve just started learning php, I’m one of those who prefers to start coding a project straight away instead of learning it and then coding. I’ve run into loads of problems, though. I can’t set up registering/logging in, everything I try doesn’t seem to work :frowning:

So far I have the login form, enter your details, validation works (prevents users entering certain number of letters/numbers as well as leaving blank etc) but it stops at a certain line. var_dump shows everything should be fine too.

$register1 = mysqli_query($con,“SELECT ‘id’ FROM ‘users’ WHERE ‘username’=’$username’”) or die(mysql_error());

Here’s my full registration code.

[php]

Register

USERNAME:
EMAIL-ID:
PASSWORD:

<?php require('connect.php'); require('functions.php'); (var_dump($_POST)); if(isset($_POST['submit'])){ $username = protect($_POST["username"]); $password = protect($_POST['password']); $email = protect($_POST['email']); if($username == "" || $password == "" || $email == ""){ echo "Please supply all fields!"; }elseif(strlen($username) > 20){ echo "Username must be less than 20 characters!"; }elseif(strlen($email) > 100){ echo "E-mail must be less than 100 characters!"; }else{ $register1 = mysqli_query($con,"SELECT 'id' FROM 'users' WHERE 'username'='$username'") or die(mysql_error()); echo "test"; $register2 = mysqli_query($con,"SELECT `id` FROM `users` WHERE `email`='$email'") or die(mysql_error()); if(mysqli_num_rows($register1) > 0){ echo "That username is already in use!"; }elseif(mysqli_num_rows($register2) > 0){ echo "That e-mail address is already in use!"; }else{ $ins1 = mysqli_query($con,"INSERT INTO `stats` (`gold`,`attack`,`defense`,`food`,`income`,`farming`,`turns`) VALUES (100,10,10,100,10,11,100)") or die(mysql_error()); $ins2 = mysqli_query($con,"INSERT INTO `units` (`worker`,`farmer`,`warrior`,`defender`) VALUES (5,5,0,0)") or die(mysql_error()); $ins3 = mysqli_query($con,"INSERT INTO `users` (`username`,`password`,`email`) VALUES ('$username','".md5($password)."','$email')") or die(mysql_error()); $ins4 = mysqli_query($con,"INSERT INTO `weapon` (`sword`,`shield`) VALUES (0,0)") or die(mysql_error()); $ins5 = mysqli_query($con,"INSERT INTO `ranking` (`attack`,`defense`,`overall`) VALUES(0,0,0)") or die(mysql_error()); echo "You have registered!"; } } } echo "test"; ?>[/php]

This is the first and biggest problem:

I'm one of those who prefers to start coding a project straight away instead of learning it and then coding.

What is the exact error you are getting. If you get no error message, turn on error reporting. Google is your friend if you dont know how to turn it on…

I don’t get any errors, that’s the problem :frowning:

I’ll quickly explain as best I can but it’s complicated, by playing around I managed to get the registration working, as well as a certain degree of logging in. But the “sessions” aren’t working, for example. Any randomly inputted username can log in, the code isn’t looking at what is in the database.

I had a certain page that could only be accessed if you had a session but no matter how many times I logged in with accounts from the database, I couldn’t access the page. There’s a clear issue and I think it is this: “Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean”

Here’s the line it is specific to

[php]$rows = mysqli_num_rows($query);[/php]

Now, I have very limited experience but I think the query is giving the code a “boolean” rather than the correct result. Here’s the full login code.

[php]<?php
session_start(); // Starting Session
include(‘functions.php’);
$error=’’; // Variable To Store Error Message
if (isset($_POST[‘login’])) {
$username = $_POST[‘username’];
if ($username == “”) {
echo “Username field is empty!”;
header(“Location: incorrectlogin.php”);
}else{
// Define $username and $password
$username=$_POST[‘username’];
$password=$_POST[‘password’];
// To protect MySQL injection for Security purpose
$username = stripslashes($username);
$password = stripslashes($password);
$username = mysqli_real_escape_string($con,$username);
$password = mysqli_real_escape_string($con,$password);
// Establishing Connection with Server by passing server_name, user_id and password as a parameter
$connection = mysqli_connect(“localhost”, “root”, “”, “game”);
// Selecting Database
$db = mysqli_select_db($con,“game”);
// SQL query to fetch information of registerd users and finds user match.
$query = mysqli_query($con,“select * from login where password=’$password’ AND username=’$username’”);
$rows = mysqli_num_rows($query);
if ($rows == 1) {
$_SESSION[‘login_user’]=$username; // Initializing Session
header(“location: main.php”); // Redirecting To Other Page
} else {
$error = “Username or Password is invalid”;
}
mysqli_close($connection); // Closing Connection
}
}

?>

You have logged in


</table>

[/php]

I know it’s a pretty bad idea to go into coding without actually learning it first but I have my reasons :slight_smile:

Hope you can help!

Welcome <?php echo $username; ?>
Click here to go forward - <?php print( 'Enter' ); ?>
Sponsor our Newsletter | Privacy Policy | Terms of Service