如何在mysql上存储数据后重定向页面

时间:2018-05-05 11:13:54

标签: php html mysql

我尝试使用header("Location: success.html"); exit;

存储在mysql上的数据后重定向到感谢网页

但是当我打开该链接时,它将自动进入success.html页面,而无需在表单和mysql上输入或存储任何数据。

<?php
$con=mysqli_connect("localhost","root","","vdl");
// Check connection
if (mysqli_connect_errno())
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if(isset($_POST['submit'])) // Fetching variables of the form which travels in URL
{
    $company_name = $_POST['company_name'];
    $since = $_POST['since'];
    $strength = $_POST['strength'];
    $head_quarter = $_POST['head_quarter'];
    if($company_name !=''||$since !='')
    {
        mysqli_query($con,"insert into digital_library(company_name, since, strength, head_quarter) values ('$company_name', '$since', '$strength', '$head_quarter')");
        echo "<br/><br/><span>Data Inserted successfully...!!</span>";
        mysqli_close($con);
    }
    else
    {
        echo "<p>Insertion Failed <br/> Some Fields are Blank....!!</p>";
    }
}
header("Location: success.html");
exit;
?>

2 个答案:

答案 0 :(得分:0)

请使用PDO或mysqli。我刚刚编辑了您现有的代码,以便在成功插入后重定向到您的成功页面。

    $con=mysqli_connect("localhost","root","","vdl");
    $i = 0;
    // Check connection
    if (mysqli_connect_errno())
    {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
    if(isset($_POST['submit'])){ // Fetching variables of the form which travels in URL
        $company_name = $_POST['company_name'];
        $since = $_POST['since'];
        $strength = $_POST['strength'];
        $head_quarter = $_POST['head_quarter'];
        if($company_name !=''||$since !=''){
            mysqli_query($con,"insert into digital_library(company_name, since, strength, head_quarter) values ('$company_name', '$since', '$strength', '$head_quarter')");
            echo "<br/><br/><span>Data Inserted successfully...!!</span>";
            mysqli_close($con);
        }
        else{
            $i++;
        }
    }
    if($i==0){
    header("Location: success.html");
    }else{
        echo "Error msg";
    }

答案 1 :(得分:0)

$con=mysqli_connect("localhost","root","","libro");

// Check connection
if (mysqli_error($con))
  {
      echo "Failed to connect to MySQL: " . mysqli_error($con);
      exit();
  }

if(isset($_POST['submit'])){ // Fetching variables of the form which travels in URL

        $company_name = $_POST['company_name'];
        $since = $_POST['since'];
        $strength = $_POST['strength'];
        $head_quarter = $_POST['head_quarter'];

        if($company_name !== ''||$since !== ''){

        mysqli_query($con,"insert into digital_library(company_name, since, strength, head_quarter) values ('$company_name', '$since', '$strength', '$head_quarter')");
    echo "<br/><br/><span>Data Inserted successfully...!!</span>";

            header("Location: success.html");
            exit();

        }
    else{

        echo "<p>Insertion Failed <br/> Some Fields are Blank....!!</p>";
        exit();

    }
}