-1

I have a file called inser_product.php which is where I have the function to insert products into the database table.

The function to insert the products looks like this:

<?php
include 'db.php';
function insert_product(){
    try{
        global $conn;

        //prepare sql and bind parametes

        $statement = $conn->prepare("insert into products (product_name, product_price, product_description) value (:product_name, :product_price, :product_description)");
        $statement ->bindParam(':product_name', $product_name);
        $statement ->bindParam(':product_price', $product_price);
        $statement ->bindParam(':product_description', $product_description);

        // executing the statement

        $product_name = $_POST['product_name'];
        $product_price = $_POST['product_price'];
        $product_description = $_POST['product_description'];

        $statement->execute();

        header('Location: index.php');
    }
    catch(PDOException $e){
        echo $query . "<br>" . $e->getMessage();
    }
    $conn = null;
}
?>

Then I used the post method for the button and call the insert_product function:

<?php
if(isset($_POST['submit-button'])){
    insert_product();
}
?>

All the insertion works fine but, How can I redirect the page to my index.php after all the data is inserted?

As you can see I used the code below but it does not work.

echo "<script>window.open('index.php','_self')<script>";
Ragmah
  • 413
  • 3
  • 12
  • 4
    `header('Location: index.php');` If you do it in PHP – RiggsFolly Jun 09 '17 at 12:52
  • where and how exactly do I put the script? – Ragmah Jun 09 '17 at 12:53
  • @RiggsFolly As I remember, he won't be able to use `header()` if he echoed something in page? Or is my memory broken? – Paul Karam Jun 09 '17 at 12:53
  • 2
    If in doubt, you could always try reading the manual now you know what to look for – RiggsFolly Jun 09 '17 at 12:53
  • @PaulKaram Very true – RiggsFolly Jun 09 '17 at 12:55
  • I accept rudeness when people help. If you are not willing to help you should not even comment. I had searched and did not find the answer I was looking. Whether or not I made a bad research this is another matter. – Ragmah Jun 09 '17 at 12:57
  • @RiggsFolly Thanks it works and I appreciate the support – Ragmah Jun 09 '17 at 12:57
  • Your script is at risk of [SQL Injection Attack](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) Have a look at what happened to [Little Bobby Tables](http://bobby-tables.com/) Even [if you are escaping inputs, its not safe!](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) Use [prepared parameterized statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) – RiggsFolly Jun 09 '17 at 13:02
  • Thanks. Am looking at it right now. – Ragmah Jun 09 '17 at 13:07
  • @RiggsFolly , I just edited my insert_product function using prepared statements. Is there a better to do it? if so I hope you can send a reference. – Ragmah Jun 09 '17 at 13:38
  • Sorry nobody can answer now as the question has been put on hold – RiggsFolly Jun 09 '17 at 13:52
  • @RiggsFolly some have and one won't... as in "work". – Funk Forty Niner Jun 09 '17 at 15:01

3 Answers3

0
try{
    $query = "insert into products(product_name, product_price, product_description) values ('$product_name','$product_price','$product_description')";
    $conn->exec($query);
    header('Location: index.php');
}
  • `echo "New record created succssefully"; header('Location: index.php');` - Really? That will fail and whoever upvoted this, doesn't know what that will do and *not* do. "Do", as in `headers sent` and "not do", redirect. – Funk Forty Niner Jun 09 '17 at 14:59
0

You can use header() of php. But make sure there must be not echo or print before it

<?php
    if(isset($_POST['submit-button'])){
        insert_product();

        header('Location: index.php');
    }
?>
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
B. Desai
  • 16,414
  • 5
  • 26
  • 47
-1

For php use:

header('location: index.php');

For JavaScript use

window.location.href='index.php';
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
The Nerdy Geek
  • 342
  • 5
  • 16