I have code for registering (using database as well). When I input information in the form and submit, nothing happens. Any help would be much appreciated! Thanks.
-after I fix this hopefully I can log in!
register.php
<!doctype html>
<html>
<head>
<title>Register</title>
</head>
<body>
<p><a href="register.php">Register</a> | <a href="login.php">Login</a></p>
<h3>Registration Form</h3>
<form action="" method="POST">
Username: <input type="text" name="user"><br />
Password: <input type="password" name="pass"><br />
<input type="submit" value="Register" name="submit" />
</form>
<?php
if(isset($_POST["submit"])){
if(!empty($_POST['user']) && !empty($_POST['pass'])) {
$user=$_POST['user'];
$pass=$_POST['pass'];
$con=mysqli_connect('localhost','root','') or die(mysqli_error());
mysqli_select_db('database') or die("cannot select DB");
$query=mysqli_query("SELECT * FROM login WHERE username='".$user."'");
$numrows=mysqli_num_rows($query);
if($numrows==0)
{
$sql="INSERT INTO login(username,password) VALUES('$user','$pass')";
$result=mysqli_query($sql);
if($result){
echo "Account Successfully Created";
} else {
echo "Failure!";
}
} else {
echo "That username already exists! Please try again with another.";
}
} else {
echo "All fields are required!";
}
}
?>
</body>
</html>
Database info:
CREATE TABLE IF NOT EXISTS `login` (
`username` varchar(200) NOT NULL,
`password` varchar(200) NOT NULL,
PRIMARY KEY (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8_general_ci;
action=""I assume you want to send it to the same page, so I recommend usingaction="<?php echo htmlspecialchars($_SERVER[PHP_SELF]); ?>"and I highly recommend reading the manuals posted by Fred -ii-, given that your code is extremely hackable amongst other things.htmlspecialchars($_SERVER[PHP_SELF])is better.http://www.example.com/<script>alert('hello')</script>written in the url to enter the site would already cause a breach.