im trying to create a form that redirects the user to anther page and also stores the information in the database.

here is the code

<?php
 mysql_connect("localhost", "DBname", "Password") or die(mysql_error());
	mysql_select_db("DBname") or die(mysql_error());
	
// Retrieve all the data from the table
$result = mysql_query("SELECT * FROM players")
or die(mysql_error());  

// store the record of the "player" table into $row
$row = mysql_fetch_array( $result );
	?>
      </div>
<table border="0" cellpadding="0" cellspacing="0" width="600">
  <tr>
    <td colspan="3"></td>
    <td></td>
  </tr>
  <tr>
    <td colspan="3"></td>
    <td></td>
  </tr>
  <tr>
    <td colspan="3"></td>
    <td></td>
  </tr>
  <tr>
    <td colspan=3 align=center>
    <form action="main.php" method="post" name="form">
	Enter Username: <input type="text" name="username" value="" ><br>
    Enter Ship Name <input type="text" name="shipName" value=""><br><br>
    <input type="submit" value="Im Ready">
    </form>
    <?php 
	if (isset($_POST['submit']))
	{
	$username = $_POST['username'];
	$shipname = $_POST['shipname'];
	$health = "150";
	$money = "1000";
	$id = $row['id'];
	mysql_query("INSERT INTO players_stats (
	player_id, username, shipname, char_health, ship_health, )VALUES('$id', '$username', '$shipname', '$health', '$health') ")
	or die(mysql_error());
	echo "Data Inserted";

 	}	?>
<br>
       
    <td></td>
  </tr>
  <tr>
    <td colspan=3 align=center><a href="#"><img src="images/mail_.gif"></a></td>
    <td></td>
  </tr>
  <tr>
  <td colspan=3 align=center><a href="faqs.html">FAQ's</a></td>
  </tr>
  </table></center><br><br><br><br>
<?php  				  
  	include("footer.php");
?>

Recommended Answers

All 9 Replies

Opps Forgot to name the submit button,

edit: but that aint done anything??

Hi,

Check our your parameters:
Enter Ship Name <input type="text" name="shipName" value=""><br><br>
and
$shipname = $_POST;

You Should Tell Us Your Problem With The Code To Detect the Reasons. Hatem

Is your connection is ok?
Error and in what line is your error is showing.

mysql_connect("localhost", "DBname", "Password") ?????????
What is 'DBname'? Is it a database name. But here it should be the mysql user name.

I am looking at your post here and I have a few thoughts / questions, and maybe the answer to your question.

First off if you want to redirect the user to another page, why not just have submit take them to that page and put the database information into the top of that file?

Second, if you changed your page up and saved all of the html to a variable (ie output) you could use a print command and speed up the way your pages loads. Also you can change the output with an if statement at that point.

The way you have this page coded will put an extra burden on your server. If you change your code to something like this:

<?php 

//contect to the database
mysql_connect("localhost", "DBname", "Password") or die(mysql_error());
mysql_select_db("DBname") or die(mysql_error());

//if the form was submitted go through and put the information in	
if (isset($_POST['submit']))
{
$username = $_POST['username'];
$shipname = $_POST['shipname'];
$health = "150";
$money = "1000";
$id = $row['id'];
mysql_query("INSERT INTO players_stats (player_id, username, shipname, char_health, ship_health, )VALUES('$id', '$username', '$shipname', '$health', '$health') ")
or die(mysql_error());
echo "Data Inserted";

};


// Retrieve all the data from the table
$result = mysql_query("SELECT * FROM players")
or die(mysql_error());  

// store the record of the "player" table into $row
$row = mysql_fetch_array( $result );

$output .='
</div>
<table border="0" cellpadding="0" cellspacing="0" width="600">
<tr>
<td colspan="3"></td>
<td></td>
</tr>
<tr>
<td colspan="3"></td>
<td></td>
</tr>
<tr>
<td colspan="3"></td>
<td></td>
</tr>
<tr>
<td colspan=3 align=center>
<form action="main.php" method="post" name="form">
Enter Username: <input type="text" name="username" value="" ><br>
Enter Ship Name <input type="text" name="shipName" value=""><br><br>
<input type="submit" value="Im Ready">
</form>
<br>
<td></td>
</tr>
<tr>
<td colspan=3 align=center><a href="#"><img src="images/mail_.gif"></a></td>
<td></td>
</tr>
<tr>
<td colspan=3 align=center><a href="faqs.html">FAQ\'s</a></td>
</tr>
</table></center><br><br><br><br>';		  

//inside your footer.php file you would also put a $output .= 'text';
include("footer.php");

//print the output
print $output
?>

It will speed up the page load time as well as allow you to do a couple of other things.
You can then use an IF Statement at the top to see if the form was filled out. IF it was, you could then change the content of the page or output a page redirect (like as follows)

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="refresh" content ="1; url=http://www.yoursite.com/newurl.php" />
<title>Some Page Title</title>
</head>
<body>
<p>Thank you for submitting your information, you will now be redirected to a different page. If for some reason you are not redirected please <a href="http://www.yoursite.com/newurl.php">click here.</a></p>
</body>
</html>

I hope that helps.

the data wont insert into the database i have tried submitting the form to aa diffrent page and inserting the data on that page but still nothing !!

Is it kicking you out an error of some sort? My hunch as to why it isn't working is because you have DBName instead of Username.

have u got this working ?
and if you have get some securate in there mysqlrealescapestring()

Found the error was trying to insert data that had allready been inserted. changed the INSERT to UPDATE and all is fine now

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.