Php表单提交和页面重定向

Php表单提交和页面重定向,php,mysql,mysqli,Php,Mysql,Mysqli,我试图在用户点击提交按钮后将一个页面重定向到一个新的php页面。我已经成功地将表单信息发送到MySQL数据库,但是无法成功重定向 然后我更改了一些代码,使其成功重定向,但没有将表单信息发送到数据库。我的另一个php文件名为nextForm.php,我尝试用nextForm.php文件的路径替换action=“$\u SERVER[php\u SELF]”,并尝试使用require nextForm.php;代码中要重定向的行 以下是我目前拥有的代码: <?php

我试图在用户点击提交按钮后将一个页面重定向到一个新的php页面。我已经成功地将表单信息发送到MySQL数据库,但是无法成功重定向

然后我更改了一些代码,使其成功重定向,但没有将表单信息发送到数据库。我的另一个php文件名为nextForm.php,我尝试用nextForm.php文件的路径替换action=“$\u SERVER[php\u SELF]”,并尝试使用require nextForm.php;代码中要重定向的行

以下是我目前拥有的代码:

<?php
    
        //establish a connection to the MySQL db or terminate if ther is an error
        $conn = mysqli_connect("localhost","root","mysql","covid_tech",3306) or die(mysqli_connect_error());
        
        //HTML form to prompt user input
        print <<<_HTML_
        <FORM style="text-align:center" method="POST" action="$_SERVER[PHP_SELF]">
    
        <div class="Customer_Name">
        Enter Customer Name:  <input type="text" name="Customer_Name" class="textbox">
        </div>
        <br/>
        <div class="Contact_Name">
        Enter Contact Name:    <input type="text" name="Contact_Name" class="textbox">
        </div>
        <br/>
        <div class="Contact_Phone">
        Enter Contact Phone Number:   <input type="text" name="Contact_Number" class="textbox">
        <br/>
        </div>
        
        
        <button class="btn btn-1" style="text-align:center" onclick='disappear(this)' name="name_submit" method="POST" type="submit" value="find_cusName"><span>Enter Customer Name</span></button>
        </FORM>
        _HTML_;


        //check to make sure the POST request was sent and check to make sure that there is a vlaue in the System POST variable
        if($_SERVER['REQUEST_METHOD'] == "POST" and isset($_POST['Customer_Name'])){
            

            //SQL string to find the name that was input on the page
            $find_name_sql = "SELECT cusName,cusID from customer where cusName = '$_POST[Customer_Name]'";

            //run the query on the db
            $result_find_name = mysqli_query($conn, $find_name_sql);

            //Check to see if the query returned any rows
            if(mysqli_num_rows($result_find_name) > 0){
                
                //If it did, it should only be 1 row and we fetch it
                $row = mysqli_fetch_row($result_find_name);
                
                //set our current_id variable to the value in $row[1] which is the cusID attribute from the db
                $current_id = $row[1];
                
            }
            else{
                //sql statment to insert a new customer into the customer table of the db
                $insert_first_customer = "INSERT INTO customer (cusName,contactName,contactNo)  values('$_POST[Customer_Name]','$_POST[Contact_Name]','$_POST[Contact_Number]')";
                
                //run the insert query
                $add = mysqli_query($conn,$insert_first_customer);  
            }
            
            //redirect to next form page here   
        }
            
        
        mysqli_close($conn);
    ?>

操作
属性只是一种引导GET/POST请求的方式。如果要在运行PHP代码后重定向,应使用
header()
函数或使用
meta
标记

例如:

header('Location:'.$\u SERVER['SERVER\u NAME'.]./nextForm.php');

echo';

并使用
exit()
函数完成代码,这样攻击者就不能绕过您的重定向。

警告:使用
mysqli
时,您应该使用和向查询中添加任何数据。不要使用字符串插值或串联来完成此操作,因为您已经创建了严重的错误。切勿将
$\u POST
$\u GET
或任何类型的数据直接放入查询中,如果有人试图利用您的错误,这可能是非常有害的。如果您希望表单发布到生成该表单的同一URL,则可以将
操作
忽略不计。当试图调试类似的东西时,总是“查看源代码”,并检查HTML是否如您所期望的那样出现。