title: I cant login with my SQL credentials used when registering
body: <p>Good evening, </p>
  
  <p>I have XAMPP setup on my home PC and i am learning to build my own websites. I have setup php scripting for user accounts.</p>
  
  <p>I have come accross a problem of which i have no idea how to solve. I will always attempt to solve issues on my own but i cant fathom what this is....</p>
  
  <p>I have my login/register php scripts all setup.</p>
  
  <p>I can register absolutely fine and my SQL database is updated with the registration details (username, password, email, phone, town).</p>
  
  <p>My issue is that when i try and login to the user account that i register with it sends me to my login_fail.php page and i cant understand why its doing this. <strong>I think it may be due to my ELSE statement at the bottom of the checklogin?</strong></p>
  
  <p>My checklogin.php script is shown below:</p>
  
  <pre><code>&lt;?php
  // setting the variables
  $host="localhost"; // Host name 
  $username="root"; // Mysql username 
  $password=""; // Mysql password 
  $db_name="test"; // Database name 
  $tbl_name="members"; // Table name 
  
  // Connect to server and select databse.
  mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
  mysql_select_db("$db_name")or die("cannot select DB");
  
  // username and password sent from form 
  $myusername=$_POST['myusername']; 
  $mypassword=$_POST['mypassword']; 
  
  // To protect MySQL injection 
  $myusername = stripslashes($myusername);
  $mypassword = stripslashes($mypassword);
  $myusername = mysql_real_escape_string($myusername);
  $mypassword = mysql_real_escape_string($mypassword);
  $sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
  $result=mysql_query($sql);
  
  // Mysql_num_row is counting table row
  $count=mysql_num_rows($result);
  
  // If result matched $myusername and $mypassword, table row must be 1 row
  if($count==1){
  
  // Register $myusername, $mypassword and redirect to file main login
  session_start();
  $_SESSION["loggedin"] = 'true';
  header("location: main_login.php");
  die();
  }
  
  else {
  header("location: login_fail.php");
  die();
  }
  ?&gt;
  </code></pre>
  
  <p>My register.php is below:</p>
  
  <pre><code>&lt;?php  
  // Connects to your Database   
  mysql_connect("localhost", "root", "") or 
  die(mysql_error());   
  mysql_select_db("test") or die(mysql_error());
  //This code runs if the form has been submitted  
  if (isset($_POST['submit']))     
   //This makes sure they did not leave any fields blank  
  if (!$_POST['username'] | !$_POST['password'] | !$_POST['email'] | !$_POST['phone'] | !$_POST['town'] ) {  
  header("location: login_fail.php");  
  die();
  }    
  // checks if the email is in use  
  if (!get_magic_quotes_gpc()) {  
  $_POST['email'] = addslashes($_POST['email']);  
  }  
  $emailcheck = $_POST['email'];  
  $check = mysql_query("SELECT email FROM members WHERE email = '$emailcheck'")   
  or die(mysql_error());  
  $check2 = mysql_num_rows($check);    
  //if the name exists it gives an error  
  if ($check2 != 0) {  
  header("location: login_fail.php");  
  die(); 
  }   
  // here we encrypt the password and add slashes if needed  
  $_POST['password'] = md5($_POST['password']);  
  if (!get_magic_quotes_gpc()) {  
  $_POST['password'] = addslashes($_POST['password']);  
  $_POST['username'] = addslashes($_POST['username']);  
  }    
  // now we insert it into the database  
  $insert = "INSERT INTO members (username, password, email, phone, town)  
  VALUES ('".$_POST['username']."', '".$_POST['password']."', '".$_POST['email']."', '".$_POST['phone']."', '".$_POST['town']."')";  
  $add_member = mysql_query($insert); 
  header("location: register_success.php");
  die();
  ?&gt;  
  </code></pre>
  
  <p>My login table (dont know if its of any use?</p>
  
  <pre><code>            &lt;table width="300" border="0" margin-bottom= "5%" align="left" cellpadding="0" cellspacing="1" bgcolor="none"&gt;
                          &lt;tr&gt;
                              &lt;form name="form1" method="post" action="checklogin.php"&gt;
                                      &lt;td&gt;
                                          &lt;table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF"&gt;
                                          &lt;tr&gt;
                                          &lt;td colspan="3"&gt;&lt;strong&gt;Member Login &lt;/strong&gt;&lt;/td&gt;
                                          &lt;/tr&gt;
                                          &lt;tr&gt;
                                          &lt;td width="78"&gt;Username&lt;/td&gt;
                                          &lt;td width="6"&gt;:&lt;/td&gt;
                                          &lt;td width="294"&gt;&lt;input name="myusername" type="text" id="myusername"&gt;&lt;/td&gt;
                                          &lt;/tr&gt;
                                          &lt;tr&gt;
                                          &lt;td&gt;Password&lt;/td&gt;
                                          &lt;td&gt;:&lt;/td&gt;
                                          &lt;td&gt;&lt;input name="mypassword" type="text" id="mypassword"&gt;&lt;/td&gt;
                                          &lt;/tr&gt;
                                          &lt;tr&gt;
                                          &lt;td&gt;&amp;nbsp;&lt;/td&gt;
                                          &lt;td&gt;&amp;nbsp;&lt;/td&gt;
                                          &lt;td&gt;&lt;input type="submit" name="Submit" value="Login"&gt;&lt;/td&gt;
                                          &lt;/tr&gt;
                                          &lt;/table&gt;
                                      &lt;/td&gt;
                              &lt;/form&gt;
                      &lt;/tr&gt;
              &lt;/table&gt; 
  </code></pre>
  
  <p>In my SQL database the passwords are encrypted still.</p>
  
  <p>If i change a user password in mySQL i can login fine, but just after registration (using the original credentials) i cant login...</p>
  
  <p>Any ideas?</p>
  
  <p>Much appreciated.</p>
  
  <p>Stan. </p>