|
Posted by Jim Michaels on 02/23/06 02:21
"Adam Frankel" <r@f.com> wrote in message
news:QnvKf.15517$z%5.1719@twister.nyroc.rr.com...
> Hey guys,
> I just started working with PHP about a week ago. I read most of two books
> on it, and I consider myself very much a newbie. I have been working with
> MySQL for about 6 months, and I would not consider myself a veteran by any
> means.
>
> I am working on a registration page for a site that I am building. The
> registration page, if successful, should create two entities - one "user"
> entity (contains information about the users Name, email, age, etc), and
> one "registration" (contains information about users ip, date, time, and
> confirmation number) entity.
>
> The weird thing that is occuring that if the user and registration tables
> are empty, then the first created user gets two entries made in each table
> instead of one, the only difference being is that the primary key (the
> email) for the user is blank in the database (yes I did set it to NOT
> NULL). For each additional user, it seems to only make one entry per
> success. I fear that this is a symptom of some coding logic failure on my
> part. I have come to conclude that my "createUser()" function and
> "createRegistration()" functions are either getting called twice or
> executing the Insert statement twice, I'm not sure which.
The only thing I can see is maybe your page is getting refreshed or the back
button is being used or something. a refresh would re-submit the data. if
you are using GET, using the back button would resubmit the data.
If you want to prevent this from happening, put a UNIQUE INDEX on one of
your table's columns. how about email? That one should in fact be your
table's PRIMARY KEY anyway shouldn't it? making it your PRIMARY KEY would
automatically give it a UNIQUE INDEX and make finding data faster for that
column.
if on your other table duplicates are OK, or it's difficult to identify a
row with a WHERE clause, then make sure you can delete at least one of them
later by adding an id column and make it auto_increment, and make that the
PRIMARY KEY.
If you intend to do a lot of searching on a column, add an INDEX to it. it
doesn't necessarily have to be a UNIQUE INDEX (unless duplicates in that
column are a bad thing).
>
> Again, this only happens to the first entry in the database when the
> tables are empty.
>
> Here is my code (sorry if it looks ugly, this is my very first php page):
> [quote] <?php
> function sql_error()
> {
> global $MYSQL_ERRORNO, $MYSQL_ERROR;
> if (empty ($MYSQL_ERROR)) {
> $MYSQL_ERRNO = mysql_errno();
> $MYSQL_ERROR = mysql_error();
> }
> return "$MYSQL_ERRNO: $MYSQL_ERROR";
> }
> function in_use($email)
> {
> $query = "SELECT email FROM user WHERE email = '$email'";
> $result = mysql_query($query);
> //echo mysql_num_rows($result) . " = mysql_num_rows(result)<br>.";
> if(!mysql_num_rows($result)) { return 0; }
> else{ return 1; }
> }
> function assignValues()
> {
> global $firstname, $lastname, $email, $user_number, $age, $password,
> $confirmationNumber;
> $firstname = $_POST[firstname];
> $lastname = $_POST[lastname];
> $email = $_POST[email];
> $age = $_POST[age];
> $password = $_POST[password];
> $user_number = $_POST[user_number];
> $confirmationNumber = getConfirmationNumber();
> }
> function listValues()
> {
> global $firstname; global $lastname; global $email; global $user_number;
> global $age; global $password;
> echo "Here is the information you submitted:<br>";
> echo "Name : '$firstname' '$lastname' <br>";
> echo "E-mail: '$email'<br>";
> echo "Age : '$age'<br>";
> echo "user_number : '$user_number'<br>";
> }
> function createUser()
> {
> global $firstname; global $lastname; global $email; global $user_number;
> global $age; global $password;
> //echo "<br>INSERT INTO user (email, firstname, lastname, user_number,
> age, password) VALUES($email, $firstname, $lastname, $user_number, $age,
> $password)<br>";
> $query = "INSERT INTO user (email, firstname, lastname, user_number, age,
> password) VALUES('$email', '$firstname', '$lastname', '$user_number',
> '$age', '$password')";
> $result = mysql_query($query);
> if(!$result){
> echo "<br>There was an error in creating user entity because: <br>".
> sql_error() . "<br>";
> }
> else
> {
> //echo "User entity created successfully!<br>";
> }
> return $result;
> }
> function createRegistration()
> {
> echo "Trying to create a registration.<br>";
> global $email, $confirmationNumber;
> $userip = $_ENV[REMOTE_ADDR];
> $curdate = date('m\-d\-y');
> $curtime = date('G\:i\:s');
> //echo "INSERT INTO registration (registrationID, ip, date, time, email)
> VALUES(NULL, $userip, $curdate, $curtime, $email)<br>";
> $query = "INSERT INTO registration (registrationID, ip, date, time, email,
> confirmation_num) VALUES('NULL', '$userip', '$curdate', '$curtime',
> '$email', '200')";
> $result = mysql_query($query);
> if(!$result){
> echo "<br>There was an error in creating registration entity because:
> <br>". sql_error() . "<br>";
> }
> else
> {
> //echo "User entity created successfully!<br>";
> }
> return $result;
> }
> function generateReturnLink($linkname)
> {
> global $firstname; global $lastname; global $email; global $user_number;
> global $age; global $password;
> return '<form method="POST" action="register.php" name="goback"><input
> type="hidden" name="firstname" value=' . $firstname . '"><input
> type="hidden" name="lastname" value="' . $lasttname . '"><input
> type="hidden" name="email" value="' . $email . '"><input type="hidden"
> name="user_number" value="' . $user_number . '"><input type="hidden"
> name="age" value="' . $age . '"><input type="hidden" name="password"
> value="' . $password . '"><input type="submit" class="submitLink" value="'
> . $linkname . '"></form>';
> }
>
> function areValuesSet()
> {
> if(isset($_POST['email']))
> if(isset($_POST['firstname']))
> if(isset($_POST['lastname']))
> if(isset($_POST['password']))
> return("none");
> else
> return("password");
> else
> return("last name");
> else
> return("first name");
> else
> return("E-mail");
> return("input");
> }
> function getConfirmationNumber()
> {
> srand((double)microtime()*1000000);
> return (rand(0,1000000)+900);
> }
> global $firstname, $lastname, $email, $user_number, $age, $password,
> $confirmationNumber;
> $link_id = mysql_connect("localhost", "db_user", "db_pass");
> if(mysql_select_db("db_selection", $link_id))
> {
> echo "Connected to DB...<br>";
> $improper_value = areValuesSet();
> if($improper_value = "none")
> {
> assignValues();
> if(!in_use("$email"))
> {
> listValues();
> echo "E-mail not in use, attempting to create user.<br>";
> if (createUser())
> {
> if (createRegistration())
> {
> echo "Created User and Registration successfully.";
> //Generate confirmation number and e-mail it to user.
> echo "The Confirmation number is " . $confirmationNumber . ".";
> }
> else //If registration entity creation failed
> {
> echo "We were unable to fulfill your registration request because there
> was invalid information in your registration entity.";
> echo "If you believe you are receiving this message in error, please
> contact us with a bug report. Thank you.";
> echo generateReturnLink("Click here to go back and modify your values.");
> mysql_query("DELETE FROM user WHERE email ='$email'");
> }
> }
> else //If user creation failed
> {
> echo "User Creation failed most likely due to invalid or missing
> input.<br>";
> echo generateReturnLink("Click here to go back and modify your values.");
> }
> }
> else //If e-mail is already in use...
> {
> echo "We were unable to fulfill your registration request because you
> provided the e-mail address " . $email . " and it is already in use.<br>";
> echo "<a href=\"/forgotpassword.php?email=$email\">Click here to have your
> password e-mailed to you.</a><br>";
> }
> }
> else //if Certain data was not set correctly
> {
> echo "We were unable to fullfill your registration request because you
> provided incomplete data.";
> echo generateReturnLink("Go back and fix your " . $improper_value . ".");
> }
> }
> if(!mysql_close($link_id))
> {
> echo "Web Server failed to close connection to database properly.";
> }
> ?>[/quote]
>
Navigation:
[Reply to this message]
|