4

我有一个 add.php 文件,它是一个收集数据的表单。当用户点击 enter.submit 按钮 add.php 会将信息添加到数据库中,然后它应该转到另一个页面。它应该重定向到一个页面(view.php)。我用过

header("位置: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

        $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>

库2.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
        }
    ?>
4

3 回答 3

3

你必须使用

header("Location: view.php");

在任何其他输出到页面之前。

在此处发布 add.php 的内容

这是 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>
于 2013-10-15T22:00:59.113 回答
3

您在标头(“位置:view.PHP”)之前输出了html...这意味着标头已经发送。

于 2013-10-15T22:04:01.603 回答
2

我测试你的代码,标题的位置(“位置:view.php”);没关系。

检查您的数据库命令?{maybe header("Location: view.php")} 永远不会运行并且您的数据库命令会引发错误!!!好锁

于 2013-10-15T23:06:11.723 回答