显示插入表单后的数组列表

时间:2013-10-14 16:49:51

标签: php html sql database

我使用HTML表单将数据插入数据库表。我想要做的是显示我使用HTML表单插入的数据。我使用关联数组从数据库中获取正确的数据。但我只能在表格中显示一行。我不知道为什么我的代码不显示我插入的其他数据。

这是我的代码:

 <?php
    $part_insert_message = "";
    enter code here
    session_start();

    $inserted_parts = array();
    $part_inserted_id;
if(isset($_POST['submit'])) {
    require "/Basics/Mysql_Connect/mysql_connect.php";

    $sql="INSERT INTO Parts (SKU, Part, Description, Quantity, Price)
    VALUES
    ('$_POST[sku]', '$_POST[categories]', '$_POST[description]', '$_POST[quantity]', '$_POST[price]')";

    $result = mysqli_query($con, $sql);
    if (!$result)
    {
        die('Error: ' . mysqli_error($con));
    }

    $part_inserted_id =  mysqli_insert_id($con);
    $inserted_parts[$part_inserted_id] = $part_inserted_id; 

    // store session data
    $_SESSION['views']= $inserted_parts;

    $part_insert_message = "ID: " . $part_inserted_id . " Part: " . $_POST['sku'];
    mysqli_close($con);
}
?>

<?php 
// This block grabs the whole list for viewing
$product_list = "";
if(isset($_SESSION['views'])){ 
    $inserted_parts = $_SESSION['views'];
}
if(!empty( $inserted_parts )) {
    require "/Basics/Mysql_Connect/mysql_connect.php";

    foreach($inserted_parts as $key => $value){
    $sql = "SELECT * FROM Parts WHERE PartID='$key'";

    $result = mysqli_query($con, $sql);

    if (!$result)
    {
        die('Error: ' . mysqli_error($con));
    }
    $productCount = mysqli_num_rows($result); // count the output amount

    if ($productCount > 0) {
    while($row = mysqli_fetch_array($result)){
            $partID = $row["PartID"];
            $sku = $row["SKU"];
            $part = $row["Part"];
            $description = $row["Description"];
            $quantity = $row["Quantity"];
            $price = $row["Price"];
            $product_list .= "Product ID: $partID - <strong>$sku</strong> - $$price - <em>Part $part</em> &nbsp; &nbsp; &nbsp;<a href='Add_Delete_Part.php?deleteid=$partID'>delete</a><br />";
        }
    }else {
    $product_list = "You have no products listed in your store yet";
    }

    }

    mysqli_close($con);
}
?>

<?php
require "/Basics/Mysql_Connect/mysql_connect.php";
// Delete Item Question to Admin, and Delete Product if they choose
if (isset($_GET['deleteid'])) {
    echo 'Do you really want to delete product with ID of ' . $_GET['deleteid'] . '? <a href="Add_Delete_Part.php?yesdelete=' . $_GET['deleteid'] . '">Yes</a> | <a href="Add_Delete_Part.php">No</a>';
    exit();
}
if (isset($_GET['yesdelete'])) {
    // remove item from system and delete its picture
    // delete from database
    $id_to_delete = $_GET['yesdelete'];

        $sql = "DELETE FROM Parts WHERE PartID='$id_to_delete'";
        $result = mysqli_query($con, $sql);
         if (!$result)
        {
            die('Error: ' . mysqli_error($con));
        }
    // unlink the image from server
//  // Remove The Pic -------------------------------------------
//    $pictodelete = ("../inventory_images/$id_to_delete.jpg");
//    if (file_exists($pictodelete)) {
//                  unlink($pictodelete);
//    }
    header("location: Add_Delete_Part.php"); 
    exit();
}
  mysqli_close($con);
?>

<html>
<body>

<div>
<h2>Part</h2>
<form action="Add_Delete_Part.php" enctype="multipart/form-data" name="myForm" id="myform" method="post">
SKU#: <input type="text" name="sku"><br>

Part: <select name="categories">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="fiat">Fiat</option>
<option value="audi">Audi</option>
</select><br>

Description: <input type="text" name="description"><br>
Quantity: <input type="text" name="quantity"><br>
Price: <input type="text" name="price"><br>
<input type="submit" name="submit" value="Submit">
</form>

<?php echo $part_insert_message; ?>

<h3>Fits:</h3>
<?php echo $product_list; ?>
<!--<form action="Add_Images.php" method="post">
Select images: <input type="file" name="img" multiple><input type="submit" value="Upload">
</form>
-->

</div>


</body>
</html> 

0 个答案:

没有答案