I have a add.php file, its a form which collects data. When the user hits enter.submit button add.php will add the info to a database, then it should go to another page. It should redirect to a page(view.php). I have used
header("Location: view.php");
My code:
<html>
<head>
<meta charset="UTF-8">
<?php
include "library2.php";
printHeader();
?>
<title>Punch Electronics Inc.</title>
<link rel="stylesheet" type="text/css" media="screen" href="styles.css">
<nav>
<ul>
<li>
<a href="add.php"> Add</a>
</li>
<li>
<a href="view.php">View</a>
</li>
</ul>
</nav>
</head>
<body>
<?php
$itemNameErr ="";
$descriptionErr ="";
$suppCodeErr="";
$costErr="";
$sellingPriceErr="";
$numberOnHandErr="";
$reorderPointErr="";
$dataValid = true;
// If submit with POST
if ($_POST) {
$itemName = $_POST['itemName'];
$description = $_POST['description'];
$suppCode = $_POST['suppCode'];
$cost = $_POST['cost'];
$sellingPrice = $_POST['sellingPrice'];
$numberOnHand = $_POST['numberOnHand'];
$reorderPoint = $_POST['reorderPoint'];
//Check if empty, if not then check if it's valid.
if ($itemName== "") {
$itemNameErr = "Error! Please enter an item name.";
$dataValid = false;
}
else if (!preg_match("/^[a-zA-Z :;\-,'0-9]{3,40}$/",$itemName))
{
$itemNameErr = "Error! Please enter a valid item.";
$dataValid = false;
}
if ($description == "") {
$descriptionErr = "Error! Please enter a description.";
$dataValid = false;
}
else if (!preg_match("/^[a-zA-Z 0-9.,'\-\r\n]{4,2000}$/",$description))
{
$descriptionErr = "Error! Please enter a valid item.";
$dataValid = false;
}
if ($suppCode == "") {
$suppCodeErr = "Error! Please enter a supplier code.";
$dataValid = false;
}
else if (!preg_match("/^[a-zA-Z \-0-9]{3,40}$/",$suppCode))
{
$suppCodeErr = "Error! Please enter a valid supplier code.";
$dataValid = false;
}
if ($cost == "") {
$costErr = "Error! Please enter a cost";
$dataValid = false;
}
else if (!preg_match("/^\d{1,10}[.][0-9][0-9]$/",$cost))
{
$costErr = "Error! Please enter a valid cost.";
$dataValid = false;
}
if ($sellingPrice == "") {
$sellingPriceErr = "Error! Please enter a selling price";
$dataValid = false;
}
else if (!preg_match("/^\d{1,10}[.][0-9][0-9]$/",$sellingPrice))
{
$sellingPriceErr = "Error! Please enter a valid cost.";
$dataValid = false;
}
if ($numberOnHand == "") {
$numberOnHandErr = "Error! Please enter number on hand.";
$dataValid = false;
}
else if (!preg_match("/^\d{1,}$/",$numberOnHand))
{
$numberOnHandErr = "Error! Please enter a valid cost.";
$dataValid = false;
}
if ($reorderPoint == "") {
$reorderPointErr = "Error! Please enter reorder point.";
$dataValid = false;
}
else if (!preg_match("/^\d{1,}$/",$reorderPoint))
{
$reorderPointErr = "Error! Please enter a valid cost.";
$dataValid = false;
}
}
//If submit with POST and Valid data.
if ($_POST && $dataValid) {
$itemName = $_POST['itemName'];
$description = $_POST['description'];
$suppCode = $_POST['suppCode'];
$cost = $_POST['cost'];
$sellingPrice = $_POST['sellingPrice'];
$numberOnHand = $_POST['numberOnHand'];
$reorderPoint = $_POST['reorderPoint'];
if($_POST['backOrder']==="yes")
{
$backOrder = "y";
}
else $backOrder = "n";
$link = connectMysql();
$sql_query = "INSERT INTO inventory VALUES ('','$itemName', '$description', '$suppCode', '$cost','$sellingPrice', '$numberOnHand','$reorderPoint', '$backOrder', 'n');";
$result = runQuery($link, $sql_query);
//Change to view.php after successfully posted and inserted into database.
header("Location: view.php");
?>
<?php
// If no submit or data is invalid, print form, repopulating fields and print error msg
} else {
?>
</br></br></br>
<div id="requirement">
All fields in <span>*</span> are mandatory.
</div>
<div id="form">
<form action="" id= "add" method="post">
<table>
<tr>
<td>Item Name: </td>
<td><input id="itemInput" name="itemName" type="text" value="<?php if (isset($_POST['itemName'])) echo $_POST['itemName']; ?>"><span >* <?php echo $itemNameErr;?></span></td>
</tr>
<tr>
<td>Description:</td>
<td><textarea rows="10" cols="40" name="description" type="text"><?php if (isset($_POST['description'])) echo $_POST['description']; ?></textarea><span>* <?php echo $descriptionErr;?></></td>
</tr>
<tr>
<td>Supplier Code:</td>
<td><input id="suppInput" name="suppCode" type="text" value="<?php if (isset($_POST['suppCode'])) echo $_POST['suppCode']; ?>"><span >* <?php echo $suppCodeErr;?></span></td>
</tr>
<tr>
<td>Cost:</td>
<td><input name="cost" type="text" value="<?php if (isset($_POST['cost'])) echo $_POST['cost']; ?>"><span >* <?php echo $costErr;?></span></td>
</tr>
<tr>
<td>Selling Price:</td>
<td><input name="sellingPrice" type="text" value="<?php if (isset($_POST['sellingPrice'])) echo $_POST['sellingPrice']; ?>"><span >* <?php echo $sellingPriceErr;?></span></td>
</tr>
<tr>
<td>Number On Hand:</td>
<td><input name="numberOnHand" type="text" value="<?php if (isset($_POST['numberOnHand'])) echo $_POST['numberOnHand']; ?>"><span >* <?php echo $numberOnHandErr;?></span></td>
</tr>
<tr>
<td>Reorder Point:</td>
<td><input name="reorderPoint" type="text" value="<?php if (isset($_POST['reorderPoint'])) echo $_POST['reorderPoint']; ?>"><span >* <?php echo $reorderPointErr;?></span></td>
</tr>
<tr>
<td>On Back Order:</td>
<td>
<input name="backOrder" type="checkbox" value="yes" <?php if (isset($_POST['backOrder'])) echo 'checked'; ?> />
</tr>
<tr>
<td></td>
<td><input name="submit" type="submit"></td>
</table>
</tr>
</form>
</div>
<?php
}
?>
</body>
<footer>
<?php
printFooter();
?>
</footer>
library2.php:
<?php
//Function to connect to MYSQL server
function connectMysql(){
$link = mysqli_connect('localhost', 'bti-mysql', 'mysqlrocks!', 'bti320')
//$link = mysqli_connect('localhost', 'root', '', 'bti320')
or die('Could not connect: ' . mysqli_error($link));
return $link;
}
?>
<?php
//Function to run the given query, requires $link from connect and SQL query
function runQuery($link, $sql_query){
$result = mysqli_query($link, $sql_query) or die('query failed'. mysqli_error($link));
return $result;
}
?>
<?php
//Function to display logo/header.
function printHeader()
{
?>
<img id="logo" src="logo.png" alt="openclipart.org CC creative commons" height="275" width="420">
<?php
}
?>
<?php
//Function to display footer.
function printFooter()
{
?>
Copyright © 2015 Shashank Inc.
<?php
}
?>
You have to use
header("Location: view.php");
before any other output to the page.
Post here content of add.php
This is new content of add.php:
<?php
include "library2.php";
$itemNameErr ="";
$descriptionErr ="";
$suppCodeErr="";
$costErr="";
$sellingPriceErr="";
$numberOnHandErr="";
$reorderPointErr="";
$dataValid = true;
// If submit with POST
if ($_POST) {
$itemName = $_POST['itemName'];
$description = $_POST['description'];
$suppCode = $_POST['suppCode'];
$cost = $_POST['cost'];
$sellingPrice = $_POST['sellingPrice'];
$numberOnHand = $_POST['numberOnHand'];
$reorderPoint = $_POST['reorderPoint'];
//Check if empty, if not then check if it's valid.
if ($itemName== "") {
$itemNameErr = "Error! Please enter an item name.";
$dataValid = false;
}
else if (!preg_match("/^[a-zA-Z :;\-,'0-9]{3,40}$/",$itemName))
{
$itemNameErr = "Error! Please enter a valid item.";
$dataValid = false;
}
if ($description == "") {
$descriptionErr = "Error! Please enter a description.";
$dataValid = false;
}
else if (!preg_match("/^[a-zA-Z 0-9.,'\-\r\n]{4,2000}$/",$description))
{
$descriptionErr = "Error! Please enter a valid item.";
$dataValid = false;
}
if ($suppCode == "") {
$suppCodeErr = "Error! Please enter a supplier code.";
$dataValid = false;
}
else if (!preg_match("/^[a-zA-Z \-0-9]{3,40}$/",$suppCode))
{
$suppCodeErr = "Error! Please enter a valid supplier code.";
$dataValid = false;
}
if ($cost == "") {
$costErr = "Error! Please enter a cost";
$dataValid = false;
}
else if (!preg_match("/^\d{1,10}[.][0-9][0-9]$/",$cost))
{
$costErr = "Error! Please enter a valid cost.";
$dataValid = false;
}
if ($sellingPrice == "") {
$sellingPriceErr = "Error! Please enter a selling price";
$dataValid = false;
}
else if (!preg_match("/^\d{1,10}[.][0-9][0-9]$/",$sellingPrice))
{
$sellingPriceErr = "Error! Please enter a valid cost.";
$dataValid = false;
}
if ($numberOnHand == "") {
$numberOnHandErr = "Error! Please enter number on hand.";
$dataValid = false;
}
else if (!preg_match("/^\d{1,}$/",$numberOnHand))
{
$numberOnHandErr = "Error! Please enter a valid cost.";
$dataValid = false;
}
if ($reorderPoint == "") {
$reorderPointErr = "Error! Please enter reorder point.";
$dataValid = false;
}
else if (!preg_match("/^\d{1,}$/",$reorderPoint))
{
$reorderPointErr = "Error! Please enter a valid cost.";
$dataValid = false;
}
}
if ($_POST && $dataValid) {
$itemName = $_POST['itemName'];
$description = $_POST['description'];
$suppCode = $_POST['suppCode'];
$cost = $_POST['cost'];
$sellingPrice = $_POST['sellingPrice'];
$numberOnHand = $_POST['numberOnHand'];
$reorderPoint = $_POST['reorderPoint'];
if($_POST['backOrder']==="yes")
{
$backOrder = "y";
}
else $backOrder = "n";
$link = connectMysql();
$sql_query = "INSERT INTO inventory VALUES ('','$itemName', '$description', '$suppCode', '$cost','$sellingPrice', '$numberOnHand','$reorderPoint', '$backOrder', 'n');";
$result = runQuery($link, $sql_query);
//Change to view.php after successfully posted and inserted into database.
header("Location: view.php");
}
?>
<html>
<head>
<meta charset="UTF-8">
<?php
//include "library2.php";
printHeader();
?>
<title>Punch Electronics Inc.</title>
<link rel="stylesheet" type="text/css" media="screen" href="styles.css">
<nav>
<ul>
<li>
<a href="add.php"> Add</a>
</li>
<li>
<a href="view.php">View</a>
</li>
</ul>
</nav>
</head>
<body>
<?php
if ($_POST && $dataValid) {
// If no submit or data is invalid, print form, repopulating fields and print error msg
} else {
?>
</br></br></br>
<div id="requirement">
All fields in <span>*</span> are mandatory.
</div>
<div id="form">
<form action="" id= "add" method="post">
<table>
<tr>
<td>Item Name: </td>
<td><input id="itemInput" name="itemName" type="text" value="<?php if (isset($_POST['itemName'])) echo $_POST['itemName']; ?>"><span >* <?php echo $itemNameErr;?></span></td>
</tr>
<tr>
<td>Description:</td>
<td><textarea rows="10" cols="40" name="description" type="text"><?php if (isset($_POST['description'])) echo $_POST['description']; ?></textarea><span>* <?php echo $descriptionErr;?></></td>
</tr>
<tr>
<td>Supplier Code:</td>
<td><input id="suppInput" name="suppCode" type="text" value="<?php if (isset($_POST['suppCode'])) echo $_POST['suppCode']; ?>"><span >* <?php echo $suppCodeErr;?></span></td>
</tr>
<tr>
<td>Cost:</td>
<td><input name="cost" type="text" value="<?php if (isset($_POST['cost'])) echo $_POST['cost']; ?>"><span >* <?php echo $costErr;?></span></td>
</tr>
<tr>
<td>Selling Price:</td>
<td><input name="sellingPrice" type="text" value="<?php if (isset($_POST['sellingPrice'])) echo $_POST['sellingPrice']; ?>"><span >* <?php echo $sellingPriceErr;?></span></td>
</tr>
<tr>
<td>Number On Hand:</td>
<td><input name="numberOnHand" type="text" value="<?php if (isset($_POST['numberOnHand'])) echo $_POST['numberOnHand']; ?>"><span >* <?php echo $numberOnHandErr;?></span></td>
</tr>
<tr>
<td>Reorder Point:</td>
<td><input name="reorderPoint" type="text" value="<?php if (isset($_POST['reorderPoint'])) echo $_POST['reorderPoint']; ?>"><span >* <?php echo $reorderPointErr;?></span></td>
</tr>
<tr>
<td>On Back Order:</td>
<td>
<input name="backOrder" type="checkbox" value="yes" <?php if (isset($_POST['backOrder'])) echo 'checked'; ?> />
</tr>
<tr>
<td></td>
<td><input name="submit" type="submit"></td>
</table>
</tr>
</form>
</div>
<?php
}
?>
</body>
<footer>
<?php
printFooter();
?>
</footer>
You have html being output before the header("Location: view.PHP")... That means headers are already sent.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With