I have a simple html form which takes in the username and password and registers it in a database. However it executes the following php if statement once as soon as the page loads even before the form is submitted. The following is that piece of code.
if (($_POST['reg_username']=="") || ($_POST['reg_password']==""))
{ ?>
<script> alert("please enter both email and password"); </script>
<? }
The full php code is down below. This is not the final code so I still did not encrypt the passwords and everything but I am just testing it out. Thanks in advance guys.
if (($_POST['reg_username']=="") || ($_POST['reg_password']==""))
{ ?>
<script> alert("please enter both email and password"); </script>
<? }
$reg_username=mysql_real_escape_string($_POST['reg_username']);
$reg_username=htmlspecialchars($reg_username);
$reg_password=mysql_real_escape_string($_POST['reg_password']);
$reg_password=htmlspecialchars($reg_password);
if(!empty($reg_username) && !empty($reg_password))
{
if (isset($reg_username) && isset($reg_password))
{
mysql_select_db($db, $dbconnect);
mysql_query("INSERT into students(username,password) VALUES
('$reg_username','$reg_password')");
}
}
?>