Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

mysql f5 peculiarity

Status
Not open for further replies.

max2474

Programmer
May 10, 2012
40
GB
Hi. I am developing a site with two databases - one is for customers to apply to join as members, the other is a members database. Everything seemed to be working well, until I tried "bug hunting".

When a new member signs up, they are deleted from the first db and added to the second. If that member then uses the back button, hits F5, or calls the php page directly, problems start occurring.

I am convinced that the problem lies here :
Code:
...
			$_SESSION[exists] = "0";
			mysql_select_db("applydb", $con);
			$result = mysql_query("SELECT email FROM apply
				WHERE email = '$_SESSION[email]' LIMIT 1");
			while($row = mysql_fetch_array($result))
			{
				$_SESSION[exists] = "1";
			}
			mysql_close($con);
...
this code checks weather the person has applied or not. If they are found (exists=1) they are added to the new db and deleted from this one.

This is working perfectly in regular use. however, even after they have been deleted, recalling the page makes $_SESSION[exists] = "1" even when they dont, resulting in duplicate entries to the members database.

I have checked the applydb and it is empty.

adding this:
Code:
echo "registered exists is ".$_SESSION[exists];
to both the top and the bottom of the script, returns 0 then 1, saying it found the non-existent member :/

Again, just to iterate, this works perfectly with normal use. It is only when recalled that the error occurs.

This is what I used to delete the member:
Code:
<?php
				require("connectdb.php");
				mysql_select_db("applydb", $con);
				mysql_query("DELETE FROM apply WHERE email = '$_SESSION[email]' LIMIT 1");
				if (!$con)
				{  die('Error: ' . mysql_error());}
				mysql_close($con);
?>

I can include more script if wanted, but cannot see that it is relevant; the database is empty.

Is this perhaps just a peculiarity?

Hope this makes sense, and that you can help.
 
Yup.

Forum: PHP is best suited for this question.

As a starter, I would question the need for 2 databases over an applicants table and a members table in the same DB?

Secondly, I suspect you are not checking your members db for the existence of the user you are trying to add to the applicant DB and subsequently to the members DB.

Thirdly and most importantly: It seems either you are not deleting the members off of the apply DB correctly or they are getting added back in. This:

Code:
while($row = mysql_fetch_array($result))
            {
                $_SESSION[exists] = "1";
            }

Should not run if there are no rows returned, which means you are getting a row back based on your query.

With that said, there is no point in looping through at most a single row and then simply discarding the contents of the row. Instead using mysql_num_rows() to determine the amount of rows returned should be enough if you don't need to do anything with the row contents.

And finally, you should always condition your insertions so that you get no duplicates in either DB. Check for the existence of the user in both before inserting. And don't set the SESSION variable to 0 without checking before if its already set to something else. That would also prevent any unwanted insertions.

For more details on the PHP side, you can post this in Forum: PHP making sure you link to this thread so people there know what has been suggested already.



----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
thanks for the tips. I am going to move over to the php forum as strange things are afoot :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top