Jump to content

[SOLVED] no go back or if go back delete from database?


eaglelegend

Recommended Posts

How do I stop people going back on one of my pages (adopt.php) as if they click on a put then change there minds then go back to choose a pet then they cant adopt that pet because "they already have it" , technically yes, they have it, but it is nameless.

 

or is there a way that if they do go off the page ie going back then delete that pet they chose then go back from the database?

 

here is the code:

 

<?php
include("header.php");
include("members.php");

$a = $_GET['action'];

if($a == '') {
print "<h2>Adopt a Pet</h2><p>";

print "<table border=0 cellpadding=2 cellspacing=0 width=300>
<tr>";

$sql = mysql_query("SELECT * FROM `available_pets` WHERE `site`=\"$Z\" ORDER BY `species` ASC");
while($row = mysql_fetch_array($sql)) {
 	extract($row);

		if($img!="") {
		 	$i = "<img src='http://www.eaglelegend.com/images/pets/$img.png' border='0' alt='$species'>";
		}
		else {
			$i = "<img src='http://www.eaglelegend.com/images/pets/$species.png' border='0' alt='$species'>"; 
		}

	$x=$x+1;

	print "<td width=33 valign=top>
	 <table border=0 cellpadding=2 cellspacing=0 width=300>
	  <Tr>
	   <td valign=middle align=center>$i</td>
	   <td valign=middle class=other width=100%>$name<p><a href='http://www.eaglelegend.com/adopt.php?action=adopt&id=$id'>Adopt a $name</a></p></td>
	 </table>
	</td>";

	if($x%2==0) {
	 	print "</tr><tr>";
	}	 
} 

print "</table>";
}
else {
if($a == 'adopt') {
 	$id = $_GET['id'];

	$check = mysql_num_rows(mysql_query("SELECT * FROM `pets` WHERE `user`='{$_COOKIE['ELv2']}' AND `petID`='$id' AND `site`=\"$Z\""));

	if($check == 0) {
		$petDetails = mysql_query("SELECT * FROM `available_pets` WHERE `id`='$id' AND `site`=\"$Z\"");
		while($petRow = mysql_fetch_array($petDetails)) {
		 	$petName = $petRow["species"];
		 	$img = $petRow["img"];
		}

		$date = date("m/d/y");
		$status = 3;
		$insert = mysql_query("INSERT INTO `pets` (`site`, `img`, `status`, `user`, `petID`, `species`, `date`) VALUES(\"$Z\", \"$img\", '$status', '{$_COOKIE['ELv2']}', '$id', \"$petName\", '$date')");

		if($insert) {
			$sql = mysql_query("SELECT * FROM `pets` WHERE `user`='{$_COOKIE['ELv2']}' AND `petID`='$id' AND `site`=\"$Z\"");
			while($row = mysql_fetch_array($sql)) {
			 	$idd = $row["id"];
			}

		 	Header("Location: http://www.eaglelegend.com/adopt.php?action=name&id=$idd");
		}
		else {
		 	print mysql_error();
		} 
	}
	else {
	 	print "Sorry you already have this pet!";
	}	 
} 

if($a == 'name') {
 	$id = $_GET['id'];

 	if($id != '') {
	 	print "<h2>Name your New Pet!</h2><p>
	 	<form action='http://www.eaglelegend.com/adopt.php?action=_name&id=$id' method='post'>
 		Pet Name <input type='text' name='name' size='20' class='text_box' title='Name your new pet!' alt='Name your new pet!'><p>
 		<input type='submit' value='Name Pet' class='text_box' title='Accept your new pets name!' alt='Accept your new pets name!'></form>";
	}
}

if($a == '_name') {
 	$id = $_GET['id'];
 	$name = $_POST['name'];

 	if($id != '') {
 	 	if($name != '') {
	 		$update = mysql_query("UPDATE `pets` SET `petName`=\"$name\" WHERE `id`='$id' AND `site`=\"$Z\"");

			if($update) {
		 		print "<h2>Congratulations!</h2><p>
				You now have a new pet named $name!<p>
				<a href='http://www.eaglelegend.com/managepets.php'>Click here to manage your pets!</a>";
			}
			else {
			 	print mysql_error();
			}
		}
		else {
		 	print "You must choose a name for your pet!<p>
		 	<form action='http://www.eaglelegend.com/adopt.php?action=_name&id=$id' method='post'>
		 	Pet Name <input type='text' name='name' size='20'><p>
		 	<input type='submit' value='Name Pet'></form>";
		}	 	 
 	}
	else {
	 	print "No ID was selected";
	}
}	 	
}

include("footer.php");
?>

what you would do is this.

 

Once they have adopted the pet, use a META refresh to move them to another page you can show the results for a couple seconds then redirect them to a new page.

 

Another thing you can do is query the database first to look for a row based on the form(POST) data. And only run the insert query if nothing is found.

 

Ray

Well to be honest, you shouldn't be able to delete if they click back. if they don't want a pet there should be some kind of profile page to remove the pet if they don't want it. The page is not going to be able to tell if you clicked the back button or not.

 

Like I said if you put a check in to query the database based on the POSTED data you can give them an option to delete it if a match is found. So if they adopt a pet, the query will run, it won't find anything so it will add it, now if they click refresh they query will run again and this time it will find something so now you can give them an option to delete it.

 

Ray

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.