页面刷新后如何停止我的php表单重新提交输入

时间:2018-08-14 09:45:02

标签: php

 <?php

$connection = mysqli_connect("host", "username", "password")
or die("Couldn't connect to the server"); //no issue 

$db = mysqli_select_db($connection, "dbname")
or die("Couldn't select database"); //no issue
$sql = "INSERT INTO vendor-tbl(company_name, industry, details, website, data_type, start_date) VALUES ('".$_POST['company_name']."','".$_POST['industry']."','".$_POST['details']."','".$_POST['website']."','".$_POST['data_type']."','".$_POST['start_date']."')";

// mysqli_query(($connection,$sql);
 if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
} 

$conn->close(); //closes the connection       
?>      
<form autocomplete="on" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post" role="form">

<label id="company_name">Company Name:</label><br/>
<input type="text" name="company_name" id="company_name" autocomplete='organization'><br/>

<label id="industry">Industry:</label><br/>
<input type="text" name="industry" id="industry" autocomplete='industry'><br/>

<label id="details">Details:</label><br/>
<input type="text" name="details" id="details" autocomplete='details'><br/>

<label id="website">Website:</label><br/>
<input type="text" name="website" id="website" autocomplete='website'><br/>

<label id="data_type">Data Type:</label><br/>
<input type="text" name="data_type" id="data_type" autocomplete='datatype'><br/>

<label id="start_date">Start Date:</label><br/>
<input type="text" name="start_date" id="start_date" autocomplete='startdate'><br/>

<button id="submitButton" value="submit" name="submit">Submit</button><br/>


</form>

在刚提交数据后刷新表单时,似乎将数据两次添加到phpmyadmin中,如果我再次加载表单(空白表单),则会第三次添加数据,等等。有什么方法可以防止这没有使用会话变量? 我对php缺乏经验,但是我尝试使用会话变量,但似乎没有使其正常工作。

3 个答案:

答案 0 :(得分:0)

您要执行的是“发布/重定向/获取”,为此您使用303。通过这种方式,您可以发布信息,然后被“强制”进行获取。当您按f5键时,您将重新加载该get,而不是您的帖子。.

请参阅:

答案 1 :(得分:-1)

使用isset()检查是否输入了按钮

if (isset($_POST['submit'])) {
     $connection = mysqli_connect("host", "username", "password")
or die("Couldn't connect to the server"); //no issue 

$db = mysqli_select_db($connection, "dbname")
or die("Couldn't select database"); //no issue
$sql = "INSERT INTO vendor-tbl(company_name, industry, details, website, data_type, 
start_date) VALUES ('".$_POST['company_name']."','".$_POST['industry']."','".$_POST['details']."','".$_POST['website']."','".$_POST['data_type']."','".$_POST['start_date']."')";

// mysqli_query(($connection,$sql);
 if ($conn->query($sql) === TRUE) {
     echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
  } 
}

答案 2 :(得分:-1)

解决方案:

<?php
 if($_POST){
$connection = mysqli_connect("host", "username", "password")
or die("Couldn't connect to the server"); //no issue 

$db = mysqli_select_db($connection, "dbname")
or die("Couldn't select database"); //no issue
$sql = "INSERT INTO vendor-tbl(company_name, industry, details, website, data_type, start_date) VALUES ('".$_POST['company_name']."','".$_POST['industry']."','".$_POST['details']."','".$_POST['website']."','".$_POST['data_type']."','".$_POST['start_date']."')";
// mysqli_query(($connection,$sql);
 if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;

} 
$conn->close(); //closes the connection       
Header("Location: " . $_SERVER['REQUEST_URI']);
 }

?>