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

cwxwcias  于 2021-06-25  发布在  Mysql
关注(0)|答案(2)|浏览(347)

我试图重定向到感谢网页后,在mysql上使用存储的数据 header("Location: success.html"); exit; 但是当我打开这个链接时,它会自动进入success.html页面,而不需要在表单和mysql上输入或存储任何数据。

  1. <?php
  2. $con=mysqli_connect("localhost","root","","vdl");
  3. // Check connection
  4. if (mysqli_connect_errno())
  5. {
  6. echo "Failed to connect to MySQL: " . mysqli_connect_error();
  7. }
  8. if(isset($_POST['submit'])) // Fetching variables of the form which travels in URL
  9. {
  10. $company_name = $_POST['company_name'];
  11. $since = $_POST['since'];
  12. $strength = $_POST['strength'];
  13. $head_quarter = $_POST['head_quarter'];
  14. if($company_name !=''||$since !='')
  15. {
  16. mysqli_query($con,"insert into digital_library(company_name, since, strength, head_quarter) values ('$company_name', '$since', '$strength', '$head_quarter')");
  17. echo "<br/><br/><span>Data Inserted successfully...!!</span>";
  18. mysqli_close($con);
  19. }
  20. else
  21. {
  22. echo "<p>Insertion Failed <br/> Some Fields are Blank....!!</p>";
  23. }
  24. }
  25. header("Location: success.html");
  26. exit;
  27. ?>
gdx19jrr

gdx19jrr1#

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

  1. $con=mysqli_connect("localhost","root","","vdl");
  2. $i = 0;
  3. // Check connection
  4. if (mysqli_connect_errno())
  5. {
  6. echo "Failed to connect to MySQL: " . mysqli_connect_error();
  7. }
  8. if(isset($_POST['submit'])){ // Fetching variables of the form which travels in URL
  9. $company_name = $_POST['company_name'];
  10. $since = $_POST['since'];
  11. $strength = $_POST['strength'];
  12. $head_quarter = $_POST['head_quarter'];
  13. if($company_name !=''||$since !=''){
  14. mysqli_query($con,"insert into digital_library(company_name, since, strength, head_quarter) values ('$company_name', '$since', '$strength', '$head_quarter')");
  15. echo "<br/><br/><span>Data Inserted successfully...!!</span>";
  16. mysqli_close($con);
  17. }
  18. else{
  19. $i++;
  20. }
  21. }
  22. if($i==0){
  23. header("Location: success.html");
  24. }else{
  25. echo "Error msg";
  26. }
展开查看全部
ttisahbt

ttisahbt2#

  1. $con=mysqli_connect("localhost","root","","libro");
  2. // Check connection
  3. if (mysqli_error($con))
  4. {
  5. echo "Failed to connect to MySQL: " . mysqli_error($con);
  6. exit();
  7. }
  8. if(isset($_POST['submit'])){ // Fetching variables of the form which travels in URL
  9. $company_name = $_POST['company_name'];
  10. $since = $_POST['since'];
  11. $strength = $_POST['strength'];
  12. $head_quarter = $_POST['head_quarter'];
  13. if($company_name !== ''||$since !== ''){
  14. mysqli_query($con,"insert into digital_library(company_name, since, strength, head_quarter) values ('$company_name', '$since', '$strength', '$head_quarter')");
  15. echo "<br/><br/><span>Data Inserted successfully...!!</span>";
  16. header("Location: success.html");
  17. exit();
  18. }
  19. else{
  20. echo "<p>Insertion Failed <br/> Some Fields are Blank....!!</p>";
  21. exit();
  22. }
  23. }
展开查看全部

相关问题