I am getting a strange effect with my code, all I am trying
to do is check to see if a user is in my database and if
not, then add them. My code if below, I will explain the
problem in the code.
//----------------------------------------------
//Begin HTML Header
<html><head><title>Register</title></head>
//----------------------------------------------
//The start of the body
<body bgcolor='#C0C0C0' background='Background.jpg'
vlink='#008080'>
//----------------------------------------------
//Start PHP code
<?php
//----------------------------------------------
//My posted variables from the previous page
//Username, password and confirmed password
$user=$POST['USERNAME'];
$pass=$POST['PASSWORD'];
$confirm=$_POST['CONFIRM_PASSWORD'];
//----------------------------------------------
//The MySQL connection variables.
$host = "localhost";
$account = "Nikolay";
$password = "abc";
$dbname = "admininfo";
//-----------------------------------------------
//Here I check to make sure that the username and password
//are not empty and that the password equals the
//comfirmed password.
if($user != "" and $pass != "" and $confirm !="" and
$pass==$confirm)
{
//Great, we have got to here, we need to make
sure that the Username is not in the database
//----------------------------------------------
//Make a connection to my database
$connect = mysql_connect($host,$account,$password)
or die("Connection Failure to Database");
//select the data base
$db = mysql_select_db($dbname,$connect)
or die ($dbname . " Database not found. " .
$_POST['USERNAME']);
//----------------------------------------------
//This is my query that counts the number of user ID's
//in order to find if the username is in the database.
$query_count="SELECT COUNT(User_ID) FROM users WHERE
User_Name = '$user'"; //Count the number of Nikolay
//Run the query
$result1 = mysql_query($query_count,$connect)
or die("Failed Query of " . $query_count);
//----------------------------------------------
// Fetch the number of records that have the same
//username as the posted one from the previous page
$row = mysql_fetch_array($result1);
$num=$row[0];
//Now if the username is not in the database, then
//$num will be equal to zero, in that case I can add
//them to the data base.
//---------------------------------------------
//Now this is were things get strange
//
if($num==0)
{
//If we made it here, then the user is not it the database
//and I want to add them
$today=date("y/m/d"); //I just want to add todays date
//---------------------------------------------
//This is the INSERT query
$query_Reg="INSERT INTO users
(User_Name,Password,Date_Joined)
VALUES ('$user','$pass','$today')";
//This is were I have the problem, if I rem this query out
// the code executes as you would expect, the only thing
//is that the user is not enterd in the database.
//But when I leave this code in, the script seems
//to behave stange. It it like the whole script is run again
//and the ELSE part of this IF statement is run, it is a bit
//hard to explain, I doesn't make no sense at all.
mysql_query($query_Reg,$connect)
or die("Failed Query of " . $query_Reg);
//---------------------------------------------
//
echo "<H3>Your registration has be submitted.
You may now go back. </H3>";
echo "<p
align='center'><b><a href='Index.htm'><font size='4'>BACK TO
MAIN PAGE</font></a></b></p> ";
}
else //This is the else part
{
//If we are here, then the user is already in the database
//If so, we go back to the previous page.
//This is the code that always seems to be run.
//If I create a user that I know is not in the data base
//This code should never be run, but every time it is.
//The we I check the data base, the user becomes entered.
//The only way I can explain it is that this page is
//seems to be running twice, the first time the
//new users gets entered in the database, and the
//second time this part of the code is executed as you
//would expect seeing there is a person in the database.
//You might have to try the code out yourself to see
//the effect, if there is one on your machine, it
//just might be only my machine with the problem. ????
//----------------------------------------
//Here the user is in the database so inform
//the user and post user and password back
//go back to the previous page
echo " <form name='InDatabase' action='NewRegister.php'
TARGET = _top method='POST'>
<h3>That username is taken, please choose another one
</h3><br>
<input type='hidden' name='USERNAME' value=''><br></p>
<input type='hidden' name='PASSWORD' value='$pass'></p>
<input type='submit' value='Go Back'
onClick='InDatabase.submit();'></p>
</form>";
} // end if($num==0)
//Close database connection.
mysql_close($connect);
}//end if($user != "" and $pass != "" and
?>
//Please try is code out, I don't know what is going on.
</body>
</html>