2

On my website i have a submit form:

this is the picture of the form

When i press this submit button the name is being saved on to my database, which is fine but i also want that button to take me to the homepage. Right now it take me to a page saying Fatal error like this

the error page

But when i press on the URL link and press enter it take me to the homepage. How can i redirect the page to homepage without selecting/pressing on the URL link and hitting enter? This is my code:

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);
  }
?>

Hope my English is good and i also hope you can understand me. All the help will be appreciated :)

Worvast
  • 279
  • 2
  • 16
LPB
  • 319
  • 1
  • 3
  • 11
  • Not sure of your structure but I guess: `header("Location: /home")`might do the trick? – Naruto Nov 26 '15 at 12:25
  • your insert query is wrong please check and call a execution function in if condition so if execution work than you can able to fetch data – Parth Chavda Nov 26 '15 at 12:32
  • 1
    put your code so we can easily copy your code and make change and post on ans – Parth Chavda Nov 26 '15 at 12:34
  • hi @ParthChavda i have added my code to the question – LPB Nov 26 '15 at 13:28
  • @LybinPeterBabu accept my ans if it's worked for you – Parth Chavda Nov 26 '15 at 13:36
  • @LybinPeterBabu You're *near* a SQL-injection-proof code, you should also `bind_param` before executing. Take a look at this function: http://php.net/manual/en/mysqli-stmt.bind-param.php (and [read How can I prevent SQL-injection in PHP?](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php)). – Qirel Nov 26 '15 at 14:51

2 Answers2

2

I think this is what you're looking for:

// 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
Rajdeep Paul
  • 16,887
  • 3
  • 18
  • 37
  • hi.. it came up with Parse error: syntax error, unexpected 'INTO' (T_STRING) in C:\Users\lybin.babu\Documents\hangman project\Hangman.php on line 27 – LPB Nov 26 '15 at 13:25
  • @LybinPeterBabu Oh, I forgot the colons(`"`) there. I've updated the code. – Rajdeep Paul Nov 26 '15 at 13:41
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);
}

to

     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();
        }

    }
Parth Chavda
  • 1,819
  • 1
  • 23
  • 30
  • it came up with a white page saying Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'jack' for key 'PRIMARY'' in C:\Users\lybin.babu\Documents\hangman project\Hangman.php:26 Stack trace: #0 C:\Users\lybin.babu\Documents\hangman project\Hangman.php(26): PDOStatement->execute() #1 {main} thrown in C:\Users\lybin.babu\Documents\hangman project\Hangman.php on line 26 – LPB Nov 26 '15 at 13:36
  • sorry it didn't work. it does the same thing.. i have to select the URL link and press enter to take me to the homepage would it be something to do with the submit button? – LPB Nov 26 '15 at 13:37
  • this error occur because you enter duplicate content – Parth Chavda Nov 26 '15 at 13:38
  • 1
    on submit button click you are redirected with specific file define in action="" whatever so....on action file you can take a selected url from any html element and pass to header("Location".$url); so it's redirect to selected or passed url – Parth Chavda Nov 26 '15 at 13:41