0

So I have a form with action insert.php

My insert.php looks like this

<?php
$con = mysql_connect("localhost","database_user","password","database");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("watchdl1_database", $con);

$sql="INSERT INTO nametable (firstname, lastname)
VALUES
('$_POST[firstname]','$_POST[lastname]')";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added";

mysql_close($con)
?>
</body>
</html>

How can I get the user to be redirected to a url after the html form submits to the database using this php code?

Thanks.

EDIT: I get this error Parse error: syntax error, unexpected 'header' (T_STRING) in insert.php

when ever I try to add header command.

Zak
  • 1
  • 1
  • Use `header("Location: your-redirect-file.php");` For eg: `header("Location: success.php");` – BEingprabhU Feb 09 '18 at 19:55
  • where do I put the header code? I tried putting it everywhere and I always get an error. – Zak Feb 09 '18 at 20:05
  • Once you finish inserting the data successfully to the database. In your case after `mysqli_close()` – BEingprabhU Feb 09 '18 at 20:06
  • Parse error: syntax error, unexpected 'header' (T_STRING) in – Zak Feb 09 '18 at 20:27
  • Your script is at risk of [SQL Injection Attack](//stackoverflow.com/questions/60174) Have a look at what happened to [Little Bobby Tables](http://bobby-tables.com/) Even [if you are escaping inputs, its not safe!](//stackoverflow.com/questions/5741187) Use [prepared parameterized statements](https://php.net/manual/en/mysqli.quickstart.prepared-statements.php). – John Conde Feb 09 '18 at 20:34
  • You don't know what's wrong because you don't check for errors in your code. Never assume the code is always going to work flawlessly. Use [`mysqli_error()`](http://php.net/manual/en/mysqli.error.php) to get a detailed error message from the database. – John Conde Feb 09 '18 at 20:35

2 Answers2

0
  1. php

    header('Location: example.php');

  2. javascript

    window.location="example.php";

Natus
  • 13
  • 1
  • 3
  • where would header('Location: example.php'); go? I've tried this and I get an error. Also, can I use an url in place of example.php? – Zak Feb 09 '18 at 20:06
  • when the record ends put the header if dosn't work, try javascript – Natus Feb 09 '18 at 20:17
  • Don't use JavaScript or Meta Tags to redirect a web page. Use [`Location: header`](//stackoverflow.com/questions/768431/how-to-make-a-redirect-in-php) instead. Check [PHP the Right Way](//phptherightway.com/) for more up-to-date advice. – John Conde Feb 09 '18 at 21:13
0

in php : header("Location: redirect_page");

in javascript: window.location.href ="redirect_page";

  • Don't use JavaScript or Meta Tags to redirect a web page. Use [`Location: header`](//stackoverflow.com/questions/768431/how-to-make-a-redirect-in-php) instead. Check [PHP the Right Way](//phptherightway.com/) for more up-to-date advice. – John Conde Feb 09 '18 at 21:13