This question already has an answer here:
I have simple form to insert data into a MySQL table. I want user after click submit to go back to index.php I have 2 files index.php and insert.php
insert.php
<?php
$con = mysql_connect("xxxxxx", "xxxxxx", "");
mysql_select_db("foster", $con);
if(isset($_POST['submit']))
{
$ID=$_POST['ID'];
$firstName=$_POST['firstName'];
$lastName=$_POST['lastName'];
$query = mysql_query("insert into customers(ID, firstName, lastName) values ('$ID', '$firstName', '$lastName')");
}
?>
<form action="index.php" method="post">
ID: <input type="text" name="ID"/>
<br/>
First Name: <input type="text" name="firstName"/>
<br/>
Last Name: <input type="text" name="lastName"/>
<br/>
<input name="submit" type="submit" value="INSERT CUSTOMER"/>
</form>
My problem is every time I change action to index.php table not change but if use action="#" is working.
</div>
Change <form action="index.php" method="post">
to <form action="insert.php" method="post">
as you want to insert your data in insert.php
.
After inserted, jump your page to index.php.
Just add this after the INSERT query:
header("Location: index.php");
exit;
It will redirect to index.php
after the insertion