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.

<?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.

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?

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..

What about including a mysql query.

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.

commented: Good job, keep up the good work! +3

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.

You are welcome.. keep reading the threads here. You will learn in no time ;)

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?

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>

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

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.

<?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

Hai;

Make sure that your actual database name is my_db

had the same issue thanks for helping

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.