单击提交按钮时如何重定向到主页?

时间:2015-11-26 12:21:22

标签: php html

在我的网站上,我有一份提交表格:

this is the picture of the form

当我按下此提交按钮时,名称将保存到我的数据库中,这很好,但我也希望该按钮将我带到主页。 现在它带我到一个页面说这样的致命错误

the error page

但是当我按下URL链接并按回车键时,它会带我到主页。如何在不选择/按下URL链接并按Enter键的情况下将页面重定向到主页?这是我的代码:

IMG: code

CODE:

<?php
  require_once "library/connection.php";
  $stmt = $conn->prepare("SELECT * FROM usernames ORDER BY scores DESC LIMIT 10");
  function createLeaderBoard($stmt)
  {
    $stmt->execute();
    $stmt->rowCount();
    $data = $stmt->fetchAll();
    foreach ($data as $row) 
    {
?>
       <tr>
          <td><?php echo $row['users']; ?></td>
          <td><?php echo $row['Scores']; ?></td>
       </tr>
<?php
    }
  }
  if (isset($_POST["firstname"])) 
  {
     $firstname = $_POST["firstname"];
     $score     = $_POST["score"];
     $stmt      = $conn->prepare("INSERT INTO `usernames` (`users`, `Scores`)     VALUES ('" . $firstname . "','" . $score . "');");
     $stmt->execute();
     $result = $stmt->fetch(PDO::FETCH_ASSOC);
  }
?>

希望我的英语很好,我也希望你能理解我。 所有的帮助将不胜感激:)

2 个答案:

答案 0 :(得分:2)

我认为这是你正在寻找的东西:

// your code

if(isset($_POST['firstname'])){
    $firstname = $_POST['firstname'];
    $score = $_POST['score'];
    $stmt = $conn->prepare("INSERT INTO username(users, scores) VALUES('{$firstname}', '{$score}'");
    if($stmt->execute()){
        // redirect the user to homepage.php
        header("Location: homepage.php");
        exit();
    }
}

// your code

答案 1 :(得分:0)

chnage this

if (isset($_POST["firstname"])) {
     $firstname = $_POST["firstname"];
     $score     = $_POST["score"];
     $stmt      = $conn->prepare("INSERT INTO `usernames` (`users`, `Scores`)     VALUES ('" . $firstname . "','" . $score . "');");
   $stmt->execute();
   $result = $stmt->fetch(PDO::FETCH_ASSOC);
}

     if (isset($_POST["firstname"])) {
             $firstname = $_POST["firstname"];
             $score     = $_POST["score"];
 $stmt      = $conn->prepare("INSERT INTO `usernames` (`users`, `Scores`)     VALUES ('" . $firstname . "','" . $score . "')");
    if($stmt->execute()){
            // redirect the user to homepage.php
            header("Location: homepage.php");
            exit();
        }

    }