I have an html form with two buttons. One is to log in using a mysql query, and the other is to redirect to a registration page. The login button works as it should, but the register button is a little odd. It will run the function that it's supposed to, but it also returns an error regarding the login button portion of the if statement.
HTML:
<html><head><title> Sign-In </title></head>
<body>
<h1> Login Form </h1>
<div id="Sign-In">
<form method="POST" action="connectivity.php">
Username: <input type='text' name='user'> <br><br>
Password: <input type='password' name='password'> <br><br>
<input type="submit" class="button" name="login_button" value="Login"> <br>
<input type="submit" class="button" name="register_button" value="Register">
</form>
</div>
</body>
</html>
PHP:
<?php
session_start(); //starting the session for user profile page
define('DB_HOST', 'localhost');
define('DB_NAME', 'project2');
define('DB_USER','root');
define('DB_PASSWORD','password');
if($_POST['login_button']){
signin();
}
else if($_POST['register_button']){
register();
}
function signin()
{
$con=mysql_connect(DB_HOST,DB_USER,DB_PASSWORD) or die("Failed to connect to MySQL: " . mysql_error());
$db=mysql_select_db(DB_NAME,$con) or die("Failed to connect to MySQL: " . mysql_error());
$ID = $_POST["user"];
$Password = $_POST["password"];
if(!empty($_POST['user'])) //checking the 'user' name which is from Sign-In.html, is it empty or have some text
{
$query =("SELECT * FROM Customers where userName = '" .$ID. "' AND pass = '" .$Password. "';");
$result = mysql_query($query, $con);
if (mysql_num_rows($result) == 0)
{
echo "Invalid username or password.";
}
else
{
echo "Successfully logged in!";
}
}
else
{
echo "Username field is empty!";
}
}
function register()
{
echo "please register";
}
?>
The error I get is:
Notice: Undefined index: login_button in C:\xampp\htdocs\project2\connectivity.php on line 8
please register
As you can see, the echo for the register()
function is working fine, but I guess it's also trying the signin()
function. I'm needing only one of the functions to execute, depending on the button clicked. Thanks in advance!