This is my first PHP script, to add a user to my website but i keep getting these errors
Cannot send session cache limiter - headers already sent
Cannot modify header information - headers already sent
mysql_num_rows expects parameter 1 to be resource boolean given
I have a basic understanding of what these error messages mean but i just can't place them in my script.
Here's the entire code for the page:
<?php
$username = "";
$password = "";
$errorMessage = "";
$num_rows = 0;
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
//get data from forms
$username = $_POST['newusername'];
$password = $_POST['newpassword'];
//check for unwanted tags
$username = htmlspecialchars($username);
$password = htmlspecialchars($password);
//get length of username and passowrd
$ulength = strlen($username);
$plength = strlen($password);
//Validate user lengths against required lengths and print apporpriate error messages
if($ulength <= 20){
$errorMessage = "";
}
else {
$errorMessage = $errorMessage. "User name must contain at most 20 characters". "<br/>";
}
if($plength <= 16){
$errorMessage = "";
}
else{
$errorMessage = $errorMessage. "Password must contain at most 16 characters". "<br/>";
}
//if there are no errors then connect to database
if($errorMessage == ""){
$uname = "root";
$pword = "root";
$database = "emusalesdb";
$server = "localhost";
$db_handle = mysql_connect($server, $uname, $pword);
$db_found = mysql_select_db($database,$db_handle);
if($db_found){
//insert injection prevention code here
//check if username already exists
$SQL = "SELECT * FROM tbl_user WHERE username = $username";
$result = mysql_query($SQL);
$num_rows = mysql_num_rows($result);//error here
if($num_rows > 0){
$errorMessage = "Username already exists";
}
else{
$SQL = "INSERT INTO tbl_user (username, password) VALUES ($username, md5($password))";
$result = mysql_query($SQL);
mysql_close($db_handle);
session_start();//error here
$_SESSION['login'] = "1";
header("Location: register_page.php");//error here
}
}
else{
$errorMessage = "Database not found";
}
}
}
?>
<head>
<title> Login - Register</title>
</head>
<!-- BEGIN body -->
<body>
<!-- BEGIN #main -->
<div id="main">
<!-- BEGIN .content -->
<div class="content">
<div class="login_right">
<div class="registerlogo"></div>
<div class="question1 ha" style="margin-top:5px;">Register</div>
<div class="question2 ha" style="margin-top:5px;">Now</div>
<div class="login_text2">Dont have an account? Register NOW it only takes a few minutes</div>
//This is the form i'm working with
<form action= "login-register-page.php" method="POST">
<input type="text" placeholder="Username" name="newusername" value="<?php print $username;?>" class="register_name"/>
<br />
<input type="password" placeholder="Password" name="newpassword" value="<?php print $password;?>" class="register_password"/>
<br />
<input value="Register" name="submitregister" class="register_account" type="submit"/>
</form>
<?php print $errorMessage;?>
</div>
<!-- END .content -->
</div>
<!-- END #main -->
</div>
<!-- END body -->
</body>
<!-- END html -->
</html>