jtnn97
jtnn97

Reputation: 1

Trying to redirect after entering in info in database with a php script

I am trying to redirect my insert script when it's done to go back to the same page so the next input can be added.

Here is what I have right now that works but doesn't redirect:

<?php

$item = $_POST['item'];
$description = $_POST['description'];
$price = $_POST['price'];
$category = $_POST['category'];

if (!isset($_POST['submit'])){
    try {
        $db = new PDO("mysql:host=localhost;dbname=peppermill;port:8889;","root","root");
        $db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
        $db->exec("SET NAMES 'utf8'");
    }Catch(Exception $e){
        echo "Could not connect to the database!";
        echo 'ERROR: ' . $e->getMessage();
        exit;
    }

    try {
        $stmt = $db->prepare("INSERT INTO peppermill.Menu(item,description,price,category) VALUES('$item','$description','$price','$category')");
        $stmt->execute();              
        //echo "Data has been entered!  -".$affected_rows = $stmt->rowCount();
    } Catch(Exception $e){
        echo 'ERROR: ' . $e->getMessage();
    }
} Else{
    echo "Did not work!!";
}

$db = null;

header("Location: ../insert_menu_items.php");
?>

Upvotes: 0

Views: 50

Answers (2)

BigBlast
BigBlast

Reputation: 387

You need to put an absolute URI as argument to Location when using it in header() function. (http://www.php.net/manual/en/function.header.php)

Additionaly, if for some reason your script runs into the more external ELSE clause, an warning will be sent because you'll be trying to send a header after sent data to the output. None data can be sent before you try to send your headers.

Upvotes: 0

Kevin
Kevin

Reputation: 1240

from http://www.php.net/manual/en/function.header.php:

"HTTP/1.1 requires an absolute URI as argument to » Location: including the scheme, hostname and absolute path, but some clients accept relative URIs. You can usually use $_SERVER['HTTP_HOST'], $_SERVER['PHP_SELF'] and dirname() to make an absolute URI from a relative one yourself."

Upvotes: 3

Related Questions