插入到 MySQL 表中 PHP 重定向


Insert into MySQL Table PHP Redirect

我有一个简单的形式将数据插入MySQL表中。我希望用户在点击提交后返回索引.php我有 2 个文件索引.php并插入.php

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

我的问题是每次我将操作更改为索引时.php表不会更改,但如果使用 action="#" 正在工作。

只需在 INSERT 查询后添加以下内容:

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

插入后将重定向到index.php

  1. <form action="index.php" method="post">更改为<form action="insert.php" method="post">,因为您要将数据插入insert.php 中。

  2. 插入后,跳转页面以索引.php。