Redirect page after inserting value into a table problem

[php] <?php require_once("includes/functions.php"); ?>

<?php require_once("includes/constance.php"); ?> <?php $menu_name = $_POST['menu_name']; $position = $_POST['position']; $visible = $_POST['visible']; try{ $conn = connection(HOST,DB_NAME,DB_USER,DB_PASSWORD); $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); }catch(PDOException $e){ echo 'Error:'. $e->getMessage(); } if(!empty($menu_name) || !empty($position) || !empty($visible)){ $stmt = $conn->prepare("INSERT INTO subjects(menu_name,position,visible) VALUES(:menu_name,:position,:visible)"); $success = $stmt->execute(array( 'menu_name' => $menu_name, 'position' => $position, 'visible' => $visible )); } [/php] I like to redirect page after inserting value. If I use code like bellwo [php]if ($success) { header('Location:http://localhost/php/widget_corp/content.php'); } ?>[/php]

It showing warning message like bellow.

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\php\widget_corp\create_subject.php:1) in C:\xampp\htdocs\php\widget_corp\create_subject.php on line 31

How can I fix this ?

It means that somewhere in one of your files, you’re already using the header(‘Location: page.php’);

Also, you’re opening and closing PHP brackets for no reason:

[PHP]

<?php require_once("includes/functions.php"); $menu_name = $_POST['menu_name']; $position = $_POST['position']; $visible = $_POST['visible']; try{ $conn = connection(HOST,DB_NAME,DB_USER,DB_PASSWORD); $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); }catch(PDOException $e){ echo 'Error:'. $e->getMessage(); } if(!empty($menu_name) || !empty($position) || !empty($visible)){ $stmt = $conn->prepare("INSERT INTO subjects(menu_name,position,visible) VALUES(:menu_name,:position,:visible)"); $success = $stmt->execute(array( 'menu_name' => $menu_name, 'position' => $position, 'visible' => $visible )); } [/PHP] What's on line 31 of create_subject.php?

Thanks for your reply. I got solution from your post.

[php]<?php

//value that I like to insert
$menu_name = ‘New Product’;
$position = 5;
$visible = 1;

try{
$conn = new PDO(‘mysql:host=localhost;dbname=widget_corp’,‘root’,’’);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

}catch(PDOException $e){
echo ‘Error:’. $e->getMessage();
}

if(!empty($menu_name) || !empty($position) || !empty($visible)){
$stmt = $conn->prepare(“INSERT INTO subjects(menu_name,position,visible) VALUES(:menu_name,:position,:visible)”);

$success = $stmt->execute(array(
	'menu_name' => $menu_name,
	'position'  => $position,
	'visible'   => $visible
));

}

if($success){
header(“Location:content.php”);
exit;
}

?>[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service