-3

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.

maxitos
  • 1
  • 1

2 Answers2

1

Just add this after the INSERT query:

header("Location: index.php");
exit;

It will redirect to index.php after the insertion

Oleg Dubas
  • 2,320
  • 1
  • 10
  • 24
0
  1. Change <form action="index.php" method="post"> to <form action="insert.php" method="post"> as you want to insert your data in insert.php.

  2. After inserted, jump your page to index.php.

harrrrrrry
  • 13,643
  • 2
  • 23
  • 28