PHP页面中的表仅显示表中的一行

时间:2019-03-30 11:52:32

标签: php

这是我更新的代码,当我转到phpmyadmin时,我添加的所有数据都被记录下来,但是当我转到实际页面时,它仅显示一行数据。我认为我的错误来自$ sql或num_rows。这是我更新的代码,当我转到phpmyadmin时,会记录我添加的所有数据,但是当我转到实际页面时,它仅显示一行数据。我认为我的错误来自$ sql或num_rows。

<tbody>
<?php                                                                   
$sql = "SELECT tbl_items.id, tbl_items.prod_name, 
tbl_items.cat_name, tbl_items.prod_desc, tbl_items.prod_reorder_lvl AS 
reorder_lvl, tbl_items.prod_price, tbl_inventory.qty AS qty FROM tbl_items 
join tbl_inventory ON tbl_items.prod_name = tbl_inventory.prod_name join 
tbl_cat ON tbl_items.cat_name = tbl_cat.cat_name";
                $result = $conn->query($sql);
            if ($result->num_rows > 0) {
              while($row = $result->fetch_assoc()) {
                $id = $row['id'];
                $prod_name = $row['prod_name'];
                $prod_desc = $row['prod_desc'];
                $cat_name = $row['cat_name'];
                $reorder_lvl = $row['reorder_lvl'];
                $qty = $row['qty'];
                $prod_price = $row['prod_price'];

              if($qty == 0){
                $alert = "<div class='alert text-danger'>
                          <strong>$qty</strong> No Stock
                          </div>";
                }else if($reorder_lvl >= $qty){
                $alert = "<div class='alert text-warning'>
                          <strong>$qty</strong> (Re-order)
                          </div>";
                }else {
                  $alert = $qty;
                  }
              ?>
              <tr>  
                <td>
                    <?php echo $prod_name; ?>
                </td>
                <td>
                    <?php echo $prod_desc; ?>
                </td>
                <td>
                    <?php echo $cat_name; ?>
                </td>
                <td>
                    <?php echo $alert; ?>
                </td>
                <td>
                    <?php echo $reorder_lvl; ?>
                </td>
                <td>
                    ₱<?php echo $prod_price; ?>
                </td>
          <td><div class="dropdown">
            <button class="btn btn-primary dropdown-toggle btn-sm" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
              Action
            </button>
            <div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
              <a class="dropdown-item small" href="#add<?php echo $id;?>" data-toggle="modal">Stock In</a>
              <a class="dropdown-item small" href="#edit<?php echo $id;?>" data-toggle="modal">Update</a>
              <a class="dropdown-item small" href="#delete<?php echo $id;?>" data-toggle="modal">Delete</a>
            </div>
          </div></td>
                <!-- In Stock Modal HTML -->
                <div id="add<?php echo $id; ?>" class="modal fade">
                  <div class="modal-dialog">
                    <div class="modal-content">
                      <form method="post">
                        <div class="modal-header">            
                          <h4 class="modal-title font-weight-light">Add Stock</h4>
                          <button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button>
                        </div>
                        <div class="modal-body font-weight-normal">
                          <div class="form-group">
                            <label>Name</label>
                            <input type="hidden" name="add_stocks_id" value="<?php echo $id; ?>">
                            <input type="text" class="form-control" id="prod_name" name="prod_name" required readonly value="<?php echo $prod_name; ?>">
                          </div>
                          <div class="form-group">
                            <label>Quantity:</label>
                            <input type="number" class="form-control" id="quantity" name="quantity" autocomplete="off" required min="1" autofocus>
                          </div>
                          <div class="modal-footer">
                            <input type="button" class="btn btn-default" data-dismiss="modal" value="Cancel">
                            <input type="submit" class="btn btn-primary" name="add_inventory" value="Add">
                          </div>
                        </div>
                      </form>
                    </div>
                  </div>
                </div>

                <!-- Edit Modal HTML -->
                <div id="edit<?php echo $id; ?>" class="modal fade">
                  <div class="modal-dialog">
                    <div class="modal-content">
                      <form method="post">
                        <div class="modal-header">            
                          <h4 class="modal-title font-weight-light">Update Product</h4>
                        </div>
                        <div class="modal-body font-weight-normal">
                          <input type="hidden" name="edit_prod_id" value="<?php echo $id; ?>">
                          <div class="row">
                            <div class="col-md-12">
                              <div class="form-group">
                                <label>Name</label>
                                <input type="text" class="form-control" id="prod_name" name="prod_name" value="<?php echo $prod_name; ?>" autocomplete="off" required>
                              </div>
                              <div class="form-group">
                                <label>Descripion</label>
                                <input class="form-control" id="prod_desc" name="prod_desc" value="<?php echo $prod_desc; ?>" autocomplete="off" required>
                              </div>
                              <div class="form-group">
                                <label>Category</label>
                                <select class="form-control" id="cat_name" name="cat_name" autocomplete="off" required>
                                  <?php

                                $sql = "SELECT cat_name FROM tbl_cat";
                                $result = $conn->query($sql);
                                  while($row = $result->fetch_assoc()) {
                                  ?>
                                    <option value="<?php echo $row["cat_name"]; ?>"><?php echo $row["cat_name"]; ?></option>
                                  <?php }?>
                                </select>
                              </div>
                              <div class="form-group">
                                <label>Price</label>
                                <input type="number" class="form-control" id="prod_price" name="prod_price" value="<?php echo $prod_price; ?>" autocomplete="off" required>
                              </div>
                              <div class="form-group">
                                <label>Re-order Level</label>
                                <input type="number" class="form-control" id="prod_reorder_lvl" name="prod_reorder_lvl" value="<?php echo $reorder_lvl; ?>" autocomplete="off" required>
                              </div>
                            </div>
                          </div>
                        </div>
                        <div class="modal-footer">
                          <input type="button" class="btn btn-default" data-dismiss="modal" value="Cancel">
                          <input type="submit" class="btn btn-primary" name="update_item" value="Save">
                        </div>
                      </form>
                    </div>
                  </div>
                </div>
                <!-- Delete Modal HTML -->
                <div id="delete<?php echo $id; ?>" class="modal fade">
                  <div class="modal-dialog">
                    <div class="modal-content">
                      <form method="post">
                        <div class="modal-header">            
                          <h4 class="modal-title font-weight-light">Delete Product</h4>
                          <button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button>
                        </div>
                        <div class="modal-body font-weight-normal">
                          <input type="hidden" name="delete_id" value="<?php echo $id; ?>">
                          <p>Are you sure you want to delete this?</p>
                          <p><small>This action cannot be undone.</small></p>
                        </div>
                        <div class="modal-footer">
                          <input type="button" class="btn btn-default" data-dismiss="modal" value="Cancel">
                          <input type="submit" name="delete" class="btn btn-danger" value="Delete" autofocus>
                        </div>
                      </form>
                    </div>
                  </div>
                </tr>


                    <?php
                    }
                    if(isset($_POST['update_item'])){
                        $edit_prod_id = $_POST['edit_prod_id'];
                        $prod_name = $_POST['prod_name'];
                        $prod_desc = $_POST['prod_desc'];
                        $cat_name = $_POST['cat_name'];
                        $prod_reorder_lvl = $_POST['prod_reorder_lvl'];
                        $prod_price = $_POST['prod_price'];
                        $sql = "UPDATE tbl_items SET 
                            prod_name='$prod_name',
                            prod_desc='$prod_desc',
                            cat_name='$cat_name',
                            prod_reorder_lvl='$prod_reorder_lvl',
                            prod_price='$prod_price'
                            WHERE id ='$edit_prod_id'";
                        if ($conn->query($sql) === TRUE) {
                        $update_inventory_query = "UPDATE tbl_inventory SET 
                            prod_name='$prod_name'
                            WHERE id ='$edit_prod_id'";
                        if ($conn->query($update_inventory_query) === TRUE) {
                            echo '<script>window.location.href="product.php"</script>';
                        } else {
                            echo "Error updating record: " . $conn->error;
                        }
                    } else {
                        echo "Error updating record: " . $conn->error;
                    }
                  }

                    if(isset($_POST['delete'])){
                        $delete_id = $_POST['delete_id'];
                        $sql = "DELETE FROM tbl_items WHERE id='$delete_id'";
                        if ($conn->query($sql) === TRUE) {
                            $sql = "DELETE FROM tbl_inventory WHERE id='$delete_id'";
                            if ($conn->query($sql) === TRUE) {
                                $sql = "DELETE FROM tbl_inventory WHERE id='$delete_id'";
                                echo '<script>window.location.href="product.php"</script>';
                            } else {
                                echo "Error deleting record: " . $conn->error;
                            }
                        } else {
                            echo "Error deleting record: " . $conn->error;

                        }
                    }
                }

                if(isset($_POST['add_item'])){
                    $prod_name = $_POST['prod_name'];
                    $prod_desc = $_POST['prod_desc'];
                    $cat_name = $_POST['cat_name'];
                    $prod_reorder_lvl = $_POST['prod_reorder_lvl'];
                    $prod_price = $_POST['prod_price'];
                    $sql = "INSERT INTO tbl_items (prod_name,prod_desc,cat_name,prod_reorder_lvl,prod_price,prod_date) VALUES ('$prod_name','$prod_desc','$cat_name','$prod_reorder_lvl','$prod_price','$date')";
                    if ($conn->query($sql) === TRUE) {
                        $add_inventory_query = "INSERT INTO tbl_inventory(prod_name,qty,date) VALUES ('$prod_name','0','$date')";
                        if ($conn->query($add_inventory_query) === TRUE) {
                            echo '<script>window.location.href="product.php"</script>';
                        } else {
                            echo "Error: " . $sql . "<br>" . $conn->error;
                        }
                    } else {
                        echo "Error: " . $sql . "<br>" . $conn->error;
                    }
                }

                if(isset($_POST['add_inventory'])){
                    $add_stocks_id = clean($_POST['add_stocks_id']);
                    $quantity = clean($_POST['quantity']);
                    $sql = "INSERT INTO tbl_issuance (prod_name,qty,date) VALUES ('$prod_name','$quantity','$date_time')";
                    if ($conn->query($sql) === TRUE) {
                        $add_inv = "UPDATE tbl_inventory SET qty = (qty + '$quantity') WHERE id = '$add_stocks_id'";
                        if ($conn->query($add_inv) === TRUE) {
                            echo '<script>window.location.href="product.php"</script>';
                        } else {
                            echo "Error updating record: " . $conn->error;
                        }
                    } else {
                        echo "Error: " . $sql . "<br>" . $conn->error;
                    }
                }
                ?>
              </tbody>

1 个答案:

答案 0 :(得分:0)

您的tr甚至没有在while循环中设置。

 while($row = $result->fetch_assoc()) {
                $id = $row['id'];
                $prod_name = $row['prod_name'];
                $prod_desc = $row['prod_desc'];
                $cat_name = $row['cat_name'];
                $reorder_lvl = $row['reorder_lvl'];
                $qty = $row['qty'];
                $prod_price = $row['prod_price'];

              if($qty == 0){
                $alert = "<div class='alert text-danger'>
                          <strong>$qty</strong> No Stock
                          </div>";
                }else if($reorder_lvl >= $qty){
                $alert = "<div class='alert text-warning'>
                          <strong>$qty</strong> (Re-order)
                          </div>";
                }else {
                  $alert = $qty;
                  }

              echo "<tr><td>".$prod_name."</td>";
              echo "<td>".$prod_desc."</td>";
              echo "<td>".$cat_name."</td>";
              echo "<td>".$alert."</td>";
              echo "<td>".$reorder_lvl."</td>";
              echo "<td>".$prod_price."</td></tr>";

} // close while loop