我尝试使用header("Location: success.html");
exit;
但是当我打开该链接时,它将自动进入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;
?>
答案 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();
}
}