3 Replies - 773 Views - Last Post: 13 January 2011 - 06:05 PM Rate Topic: -----

#1 thomasbomb  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 29
  • Joined: 10-January 11

Not redirect to database.php?

Posted 13 January 2011 - 04:35 PM

I got my guess the number game to work, I just don't want it to be at www.domain.com/database.php. Does anyone know how to fix this?

<?php     
	//MySQL Variables     
	$server = "mysql4.000webhost.com";     
	$databaseUser = "a2108336_numgame";     
	$databasePassword = "*******";     
	$databaseName = "a2108336_numgame";     
	$databaseTable = "Numbergame";     
    
    
	$con = mysql_connect($server,$databaseUser,$databasePassword);     
	if(!$con){     
		die(mysql_error());     
	}     
	mysql_select_db($databaseName, $con);  

	//Deletes rows that don't have a secret word
	mysql_query ("DELETE from Numbergame WHERE Secret_word=''");
    
	//Form Variables     
	$numberguessed = mysql_real_escape_string($_POST["guess"]);     
	$secretword = mysql_real_escape_string($_POST["Secret_word"]);     
    
	//Game Variables     
	$high = 100;
	$low = 1;
	$guesses = 1;
	$random = rand (1,100 );

	//Gets info from MySQL
	$query = mysql_query("SELECT * FROM Numbergame WHERE Secret_word='$secretword'");  
	$array = mysql_fetch_array($query);  
	if ($array['Random_number'] != 0){
		$random = $array['Random_number'];  
		$high = $array['High'];   
		$low = $array['Low'];  
		$guesses = $array['Total_guesses'];
		}
	switch ($numberguessed)
	{
		case 0:
			echo "Welcome to the guess the number game!<br>";
			break;
		case $numberguessed == $random:
			echo "You'r right! The number was " . $random ." It took you " . $guesses ." tries. Would you like to play again?<br>";
			mysql_query ("DELETE from Numbergame WHERE Secret_word='$secretword'");
			$high = 100;
			$low = 1;
			break;
		case $numberguessed < $random:
			if ($numberguessed > $low)
				$low = $numberguessed + 1;
			echo "Nope, guess higher than that.<br>";
			break;
		case $numberguessed > $random:
			if ($numberguessed < $high)
				$high = $numberguessed - 1;
			echo "Nope, guess lower that that.<br>";
			break;
		default:
			echo "Sorry we experienced an error.<br>";
	}
	//Updates highest number, lowest number, and amount of guesses
	if ($numberguessed != 0){
		$guesses++;
		mysql_query("UPDATE Numbergame SET High='$high', Low='$low', Total_guesses='$guesses' WHERE Secret_word='$secretword'");     
	}     
	if ($array['Random_number'] == 0)
		mysql_query("INSERT INTO Numbergame (ID,Random_number,Guessed_number,Total_guesses,High,Low,Secret_word) VALUES (null, '$random','$guess','$guesses','$high','$low','$secretword')") or die(mysql_error());     
	mysql_close($con);     
    
?>     
<HTML>     
	<?php
		echo "Guess an number between " . $low . " and " . $high . " and enter a word that will be used to identify your game. Be creative!";     
	?>     
	<form action="database.php" method="post">     
		<input type="text" name="guess" value="">     
		<input type="text" name="Secret_word" value="<?=$secretword?>">     
		<input type="submit">     
	</form>     
</HTML> 

This post has been edited by thomasbomb: 13 January 2011 - 04:36 PM


Is This A Good Question/Topic? 1
  • +

Replies To: Not redirect to database.php?

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: Not redirect to database.php?

Posted 13 January 2011 - 04:48 PM

Set the form's action to the name of the PHP file to which you intend to post the data. If it's the same file, you should be able to do:

<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">

Was This Post Helpful? 2
  • +
  • -

#3 thomasbomb  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 29
  • Joined: 10-January 11

Re: Not redirect to database.php?

Posted 13 January 2011 - 04:53 PM

Thanks. When I went to edit it I saw that it went to database.php. That makes sense.
Was This Post Helpful? 0
  • +
  • -

#4 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 544
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: Not redirect to database.php?

Posted 13 January 2011 - 06:05 PM

It's also worth noting that, at least some of the time, you can set the action to an empty string (action="") and it'll work on the originating script as well. I know for a fact Firefox supports this, but offhand, I'm not certain which browsers, if any, do not.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1