0

Hello!

Could anybody please help.

I need to be able to insert a set of user inputed form values to a database and then redirect to the same form with the values still in the fields.

I tried to use "$_SESSION" in the form action. It redirects me to the same form page and the values are present. But because I already put something in the form action, I couldn't use it to call the "insert.php" file anymore.

You suggestions are very much appreciated. Thanks in advance.

5
Contributors
14
Replies
35
Views
9 Years
Discussion Span
Last Post by janetkissho
0
<?php
if(isset($_POST['submit'])){
	$name = $_POST['name'];
	$age = $_POST['age'];
	//insert into table
}
?>
<html>
<body>
<form name="test" method="post" action="samepage.php">
Name: <INPUT TYPE="text" NAME="name" value="<?php echo $name ?>"><br />
Age: <input type="text" name="age" value="<?php echo $age ?>"><br />
<INPUT TYPE="submit" name="submit" value="submit">
</form>
</body>
</html>

This is just an example program. Submit to the same page, do all the operation with your form variables, print those values in respective form elements.

0

First of all, thank you for the reply.

Here's a little something of what I did:

<html>
<body>
<form name="test" method="post" action="<?php $_SESSION['PHP_SELF']?>">

Name: <INPUT TYPE="text" NAME="name" value="<? if(isset($_POST['name'])){ echo "$_POST['name']";} ?>"><br />

Age: <input type="text" name="age" value="<? if(isset($_POST['age'])){ echo "$_POST['age']";} ?>"><br />

<INPUT TYPE="submit" name="submit" value="submit">
</form>
</body>
</html>

It does redirect me to the same page and with the form entries within the fields. But since I already wrote something in the form action, I couldn't call the "insert.php" page anymore.

Is there any method on how I can insert the data to the database?

0

you can call the same page to do the insert operation. As in my example, I am calling the same page to insert the values to the table..

1

yeah.. you can do that as well..

<?php
if(isset($_POST['submit'])){
$conn=mysql_connect("localhost","root","password");
mysql_select_db("test");
	$name = $_POST['name'];
	$age = $_POST['age'];
	$query="insert into table (name,age) values ('$name','$age')";
$result = mysql_query($query);
}
?>
<html>
<body>
<form name="test" method="post" action="samepage.php">
Name: <INPUT TYPE="text" NAME="name" value="<?php echo $name ?>"><br />
Age: <input type="text" name="age" value="<?php echo $age ?>"><br />
<INPUT TYPE="submit" name="submit" value="submit">
</form>
</body>
</html>

:) yep.. thats all.

Votes + Comments
Good job, keep up the good work!
0

thank you very much for your help sir! it is very much appreciated. i've been trying to figure that one out for the last few days. i'm not really a hardcore php programmer. thank you.

0

I have one more question. What if the user presses the refresh button? Wouldn't it re-insert the entries present in the fields to the database?

0

It will. If you don't want it to insert a duplicate value, you can initialise a session, add these values to the session, redirect to the same page using header function and then print the respective values using the session variables.

Eg.

<?php
session_start();
$conn=mysql_connect("dbhost","dbuser","dbpass");
mysql_select_db("database");
if(isset($_POST['submit'])){
	$field1=$_POST['field1'];
	$field2=$_POST['field2'];
	$_SESSION['field1']=$field1;
	$_SESSION['field2']=$field2;
	$query="insert into table (field1, field2) values (".$field1.",".$field2.")";
	mysql_query($query);
	 header("location: thispage.php");
}
?>
<html>
<body>
<FORM METHOD=POST ACTION="thispage.php">
	<INPUT TYPE="text" NAME="field1" value="<?php echo $_SESSION['field1'] ?>"><br />
	<INPUT TYPE="text" NAME="field2" value="<?php echo $_SESSION['field2'] ?>"><br />
	<INPUT TYPE="submit" name="submit" value="submit">
</FORM>
</body>
</html>
0

I tried what you said but for some reason, it still creates duplicates.

0

Have you initialized the session ? Have you added respective variables to the session ? Are you redirecting the page back to itself ? Show us what you have done. We can see if there is anything wrong with your code.

0
<?php
$con = mysql_connect("localhost","testform","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("my_db", $con);

$sql="INSERT INTO Persons (FirstName, LastName, Age)
VALUES
('$_POST[firstname]','$_POST[lastname]','$_POST[age]')";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added";

mysql_close($con);
?> 

i have try to connect the db but there is some issue.(no database selected) how can i solve it

Edited by cetric.selva

0

Hai;

Make sure that your actual database name is my_db

Edited by Bachu: correction

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.