php mysql code eror

after i press register on line 157  It like it log out and ask me to sign in a gain how can i FIX IT




<?php
session_start();
header('Content-Type: text/html; charset=utf-8');
error_reporting(E_ALL);
include "./common_db.inc";
$register_script = "./register.php";

function auth_user($userid, $userpassword) {
   global $default_dbname1, $user_tablename;
  
   $link_id = db_connect1($default_dbname1);
   $query = "SELECT nickname FROM $user_tablename 
                             WHERE nickname = '$userid' 
                             AND password = '$userpassword' AND stockad = 'y' ";
   $result = mysql_query($query);
   if(!mysql_num_rows($result)) return 0;
   else {
      $query_data = mysql_fetch_row($result);
      return $query_data[0];
   }
}

function login_form() {
   global $PHP_SELF;
?>
<HTML>
<HEAD>
<TITLE>Login</TITLE>
</HEAD>
<BODY>
<FORM METHOD="POST" ACTION="<?php echo $PHP_SELF ?>">
   <DIV ALIGN="CENTER"><CENTER>
      <H3>Please log in to access the page you requested.</H3>
   <TABLE BORDER="1" WIDTH="200" CELLPADDING="2">
      <TR>
         <TH WIDTH="18%" ALIGN="RIGHT" NOWRAP>ID</TH>
         <TD WIDTH="82%" NOWRAP>
            <INPUT TYPE="TEXT" NAME="userid" SIZE="8">
         </TD>
      </TR>
      <TR>
         <TH WIDTH="18%" ALIGN="RIGHT" NOWRAP>Password</TH>
         <TD WIDTH="82%" NOWRAP>
            <INPUT TYPE="PASSWORD" NAME="userpassword" SIZE="8">
         </TD>
      </TR>
      <TR>
         <TD WIDTH="100%" COLSPAN="2" ALIGN="CENTER" NOWRAP>
            <INPUT TYPE="SUBMIT" VALUE="LOGIN" NAME="Submit">
         </TD>
      </TR>
   </TABLE>
   </CENTER></DIV>
</FORM>
</BODY>
</HTML>
<?php
}




if(!isset($_POST['userid'])) {
   login_form();
   exit;
}
// main else when have log in then process
else {
// session_register("userid", "userpassword");
    $userid =  ($_POST['userid']);
  $userpassword=  ($_POST['userpassword']);
  $_SESSION['userid'] = $userid;
  $_SESSION['userpassword'] = $userpassword  ;
   $username = auth_user($userid, $userpassword);
   if(!$username) {
      session_unregister("userid");
      session_unregister("userpassword");
      echo "Authorization failed. " .
           "You must enter a valid userid and password combo. " .
           "Click on the following link to try again.<BR>\n";
      echo "<A HREF=\"$PHP_SELF\">Login</A><BR>";
      echo "If you're not a member yet, click " .
           "on the following link to register.<BR>\n";
      echo "<A HREF=\"$register_script\">Membership</A>";
      exit;
   }
// incase get correct user Id  
    else
    {
		$_SESSION['username'] = $userid;
	
	echo "Welcome, $username!";
     
	 
	 

	 if (isset($_POST['id']))    
		{
		$id2 = $_POST['id'];
		$idtype2 = $_POST['idtype'] ;
		

		include "user4.inc";
		$db = db_connect($default_dbname);
	
		$result = mysql_query( "SELECT  preselRef FROM sale  WHERE secid =  '$id2' 
AND invoicetype  =  '$idtype2' ",$db );

       

	
		$myrow = mysql_fetch_array($result);
		$onbook = $myrow["preselRef"] ;
		
         $_SESSION['invoiceno'] = $onbook ;

		  $result2 = mysql_query( "INSERT INTO `reprint` (`saleid`, `user`) VALUES ('$onbook', '$username') ",$db );
         
          if ($idtype2 == 'i' )
			{   echo "<meta http-equiv=\"refresh\" content=\"0;url=../stockadjust/invoice.php\" />\n"; }
		  if ($idtype2 == 'r' )
			{   echo "<meta http-equiv=\"refresh\" content=\"0;url=../stockadjust/presalefromS32.php\" />\n"; }
		  if ($idtype2 == 'd')
			{   echo "<meta http-equiv=\"refresh\" content=\"0;url=../stockadjust/presalefromS27.php\" />\n"; }


		
		?>
		

		</table>
		<input type="Submit" name="update" value="Update information">
	    </form>  
			<?php
		}
	else 
	{
	?>
	<html>
	<head>
	<title>Adjust Stock </title>
	
	<body>
 
	<h1>Enter Bill No </h1>

	  <form  method="post" action="<?php echo $_SERVER['PHP_SELF']?>"> 
	   <table border="0">
     
		 <tr>
       
	       <td><input name="id" type="text" size="10" maxlength="70"><br /></td>
		   <td><input name="idtype" type="text" size="2" maxlength="70"><br /></td>
	    </tr>
	 
	      <tr>
        <td colspan="2"><input type="submit" value="Register"></td>
      </tr>
	   </table>
	 </form>
	</body>
	</html>
	<?php
	} 
	 
		 }
	}
?>
<html>
	<head>
	<title>Stock Check mini</title>
	</head>
</html>

Open in new window

teeraAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
I don't think you can fix it.  I think it would be best to start over.  There are just too many things wrong, and it would be best to refactor.

Here are some good learning resources.  Go through this before you start trying to work on the script again.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html

Here is the general design for client authentication:
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_2391-PHP-login-logout-and-easy-access-control.html

PHP is doing away with MySQL support.  Here is how to handle that change:
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_11177-PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html

You cannot use session_register() any more.

Here is an article on coding standards that you really want to take to heart.
http://pear.php.net/manual/en/standards.php

Best of luck with your project, ~Ray
0
GaryCommented:
Why log someone out if they are logged in by clicking Register - makes no sense.
Anyway give the submit button a name then you can check the post value of the name and if set then do something - do what I don't know because there is nothing in your code to indicate you are even storing login details.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.